Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 4 - HTTP Status 400, Required parameter is not present

I have 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 try to map form submit (at this moment, it doesn't do anything with data):

@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 save method signature - it works, it redirects to saved.jsp file. Earlier, I uses @RequestParam String name, @RequestParam String surname etc to catch data from HTML forms. Is there any solution to "catch" data from Spring form as DTO object? I wolud be happy if anbyody decides to help me - thank you in advance.

like image 918
Radek Anuszewski Avatar asked Feb 12 '23 08:02

Radek Anuszewski


1 Answers

You may try with @ModelAttribute (Visit ModelAttribute question in SO to get clear understanding about it)

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

I used this in spring mvc 3.1

like image 127
sarwar026 Avatar answered Feb 13 '23 21:02

sarwar026