Spring 4 - HTTP Status 400,不存在必需参数

时间:2014-12-13 15:38:41

标签: java spring jsp spring-mvc mapping

我在 index.jsp

中有Spring表单
<%@ 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>

WorkController.java 中,我尝试映射表单提交(此时,它不会对数据执行任何操作):

@Controller
public class WorkController {

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

但我获得了HTTP 400状态:Required EmployeeDTO parameter 'employeeDTO' is not present,其中包含说明:The request sent by the client was syntactically incorrect.

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());
    }
}

如果我从@RequestParam EmployeeDTO employeeDTO方法签名中删除save - 它可以正常工作,它会重定向到saved.jsp文件。之前,我使用@RequestParam String name, @RequestParam String surname etc从HTML表单中捕获数据。是否有任何解决方案&#34; catch&#34; Spring形式的数据作为DTO对象?如果abyody决定帮助我,我会很高兴 - 提前谢谢你。

5 个答案:

答案 0 :(得分:3)

使用@RequestBody将所有正文内容(例如:JSON)映射到DTO对象。使用@ModelAttribute将所有表单发布参数映射到DTO对象。

答案 1 :(得分:3)

您可以尝试@ModelAttribute(访问ModelAttribute question in SO以便清楚了解它)

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

我使用了这个in spring mvc 3.1

答案 2 :(得分:2)

如前面的答案中所述, @ModelAttirube 是修复程序的一部分,但是,要使值实际绑定到模型属性,您必须在您的修补程序中添加名称属性形式,像这样

<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>

答案 3 :(得分:0)

答案 4 :(得分:0)

如果您正在使用spring MVC,请确保您的控制器中的“ModelAndView”具有“@RequestMapping”。 并在jsp页面中检查你的ajax调用synatax。 这个网址可以帮到你

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