【问题标题】:Spring 4 - HTTP Status 400, Required parameter is not presentSpring 4 - HTTP 状态 400,必需参数不存在
【发布时间】:2015-02-12 03:54:03
【问题描述】:

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

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

【问题讨论】:

    标签: java spring jsp spring-mvc mapping


    【解决方案1】:

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

    【讨论】:

    • 有了@RequestBody,我得到了HTTP Status 415,描述为The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.
    【解决方案2】:

    您可以尝试@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

    【讨论】:

    • 谢谢,它可以工作 :) 但是它可以用于 AJAX 请求吗?几个月前我看到喜欢使用@SomeSpringAnnotation EmployeeDTO employeeDTO,他可以将所有请求正文明确映射到DTO。
    • @Pneumokok:是的,它也适用于 ajax 请求。我使用 $.post() 函数来处理 ajax。
    【解决方案3】:

    【讨论】:

      【解决方案4】:

      正如前面的答案中提到的,@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>
      

      【讨论】:

      • 谢谢 :) 我忘记了,但现在有了名字就可以了
      【解决方案5】:

      如果您使用的是 Spring MVC,请确保您的 JSP 控制器中的“ModelAndView”具有“@RequestMapping”。 并在 jsp 页面中检查您的 ajax 调用的语法。 这个网址可能会对您有所帮助

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

      【讨论】:

        猜你喜欢
        • 2015-02-12
        • 2018-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多