Java Spring 4 - HTTP 状态 400,所需参数不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27460556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:24:22  来源:igfitidea点击:

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

javaspringjspspring-mvcmapping

提问by Radek Anuszewski

I have Spring form in index.jsp:

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

In WorkController.javaI try to map form submit (at this moment, it doesn't do anything with data):

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

@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 presentwith description: The request sent by the client was syntactically incorrect.

但我得到了 HTTP 400 状态:Required EmployeeDTO parameter 'employeeDTO' is not present带有描述:The request sent by the client was syntactically incorrect.

There is EmployeeDTO.java:

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 employeeDTOfrom savemethod signature - it works, it redirects to saved.jspfile. Earlier, I uses @RequestParam String name, @RequestParam String surname etcto 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.

如果我@RequestParam EmployeeDTO employeeDTOsave方法签名中删除- 它有效,它会重定向到saved.jsp文件。早些时候,我使用@RequestParam String name, @RequestParam String surname etc从 HTML 表单中捕获数据。是否有任何解决方案可以从 Spring 表单中“捕获”数据作为 DTO 对象?如果 anbyody 决定帮助我,我会很高兴 - 在此先感谢您。

采纳答案by sarwar026

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

您可以尝试使用@ModelAttribute(访问SO 中的 ModelAttribute 问题以清楚了解它)

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

I used this in spring mvc 3.1

我用过这个 in spring mvc 3.1

回答by kamoor

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.

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

回答by Master Slave

As mentioned in the previous answers, @ModelAttirubeis 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

正如前面的答案中提到的,@ ModelAttrube是您修复的一部分,但是,要将值实际绑定到模型属性,您必须在表单上添加名称属性,如下所示

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

回答by Nandita Rasam

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

如果您使用的是 spring MVC,请确保您在 JSP 的控制器中为“ModelAndView”设置了“@RequestMapping”。并在jsp页面中检查您的ajax调用是否有synatax。这个网址可能对你有帮助

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

HTTP 状态 400 - 所需的字符串参数“xx”不存在