Spring 4 - HTTP Status 400, no required parameter

I have a Spring form in index.jsp :

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<body>
<form:form action="save" name="employeeDTO" method="POST">
        <label for="name">Name</label><input id="name" type="text" required><br>
        <label for="surname">Surname</label><input id="surname" type="text" required><br>
        <label for="email">E-mail</label><input id="email" type="email" required><br>
        <label for="salary">Salary</label><input id="salary" type="number" required><br>
        <input type="submit" value="Save">
</form:form>
</body>
</html>

      

In WorkController.java I am trying to render a submit form (it does nothing with the data at the moment):

@Controller
public class WorkController {

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(@RequestParam EmployeeDTO employeeDTO){
        return "saved";
    }
}

      

But I got HTTP 400 Status: Required EmployeeDTO parameter 'employeeDTO' is not present

with description:The request sent by the client was syntactically incorrect.

There is EmployeeDTO.java :

public class EmployeeDTO implements Serializable, DTO {
    private Long id;
    private String name;
    private String surname;
    private String email;
    private Double salary;

    public EmployeeDTO(){}

    public EmployeeDTO(Long id, String name, String surname, String email, Double salary){
        this.id = id;
        this.name = name;
        this.surname = surname;
        this.email = email;
        this.salary = salary;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Double getSalary() {
        return salary;
    }

    public void setSalary(Double salary) {
        this.salary = salary;
    }

    @Override
    public Serializable toEntity() {
        return new Employee(getId(), getName(), getSurname(), getEmail(), getSalary());
    }
}

      

If I remove @RequestParam EmployeeDTO employeeDTO

from the save

method signature - it works, it gets redirected to a file saved.jsp

. I used @RequestParam String name, @RequestParam String surname etc

to use HTML to collect data from forms. Is there any solution to "grab" data from a Spring form as a DTO object? I'll be happy if Anbeid decides to help me - thanks in advance.

+3


source to share


5 answers


You can try @ModelAttribute

(Visit the ModelAttribute Question on SO for a clear understanding of this)

@RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(@ModelAttribute("employeeDTO") EmployeeDTO employeeDTO){
    return "saved";
}

      



I used this in spring mvc 3.1

+3


source


Use @RequestBody to map the entire body content (example: JSON) to your DTO object. Use @ModelAttribute to display all form post parameters for a DTO object.



+3


source


As mentioned in previous answers, @ModelAttirube is part of your fix, but in order for the values ​​to actually bind to the model attribute, you will need to add name attributes in your form, for example

<form:form action="save" name="employeeDTO" method="POST">
    <label for="name">Name</label><input id="name" name="name" type="text" required><br>
    <label for="surname">Surname</label><input id="surname" name="surname" type="text" required><br>
    <label for="email">E-mail</label><input id="email" type="email" name="email" required><br>
    <label for="salary">Salary</label><input id="salary" type="number" name="salary" required><br>
    <input type="submit" value="Save">
</form:form>

      

+2


source


0


source


If u is using spring MVC make sure you have "@RequestMapping" for "ModelAndView" in your controller for JSP. and check your ajax call on jsp page for syntax. and this url might help you

HTTP Status 400 - Required String parameter 'xx' is missing

0


source







All Articles