4

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.

Radek Anuszewski
  • 1,812
  • 8
  • 34
  • 62

5 Answers5

3

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

kamoor
  • 2,909
  • 2
  • 19
  • 34
  • With @RequestBody I got `HTTP Status 415` with description `The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.` – Radek Anuszewski Dec 13 '14 at 15:49
3

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

Community
  • 1
  • 1
sarwar026
  • 3,821
  • 3
  • 26
  • 37
  • Thank you, it works :) But will it works with AJAX requests? I saw few month ago like use `@SomeSpringAnnotation EmployeeDTO employeeDTO` and he could map all requests body explicity to DTO. – Radek Anuszewski Dec 13 '14 at 15:55
  • @Pneumokok: Yes, it works with ajax request too. I used `$.post()` function to work with ajax. – sarwar026 Dec 13 '14 at 15:57
2

As mentioned in the previous answers, @ModelAttirube is a part of your fix, but, to have the values actually bind to the model attribute, you'll have to add the name attributes on your form, like this

<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>
Master Slave
  • 27,771
  • 4
  • 57
  • 55
0

If u are using spring MVC then make sure you have "@RequestMapping" for "ModelAndView" in your controller for the JSP. and check your ajax call in jsp page for synatax. and this url may help you

HTTP Status 400 - Required String parameter 'xx' is not present

Community
  • 1
  • 1