【问题标题】:HTTP Status 400 The request sent by the client was syntactically incorrectHTTP 状态 400 客户端发送的请求语法错误
【发布时间】:2015-12-21 19:25:09
【问题描述】:

这是我的控制器

@RequestMapping("/offercreated")

 public String offerCreated(@Valid Offer offer, Model model, BindingResult  result) {

 if (result.hasErrors()) {

 return "createoffer";

 } else {

 System.out.println("form validated");

 return "offercreated";

 }

我的豆子是

@Size(min = 5, max = 45)
    private String name;

当我给出 5 到 45 个字符之间的名称时,表格得到验证。但是,当表单未经过验证时,我会收到 400 状态错误报告。我不知道为什么我会收到错误。请在这里需要帮助

【问题讨论】:

  • 您的要求如何?你有 -- @Valid Offer offer -- 在你的方法调用中,这意味着 Spring 将首先验证 Offer。
  • BindingResult 应该直接出现在它所关注的参数之后。切换Model 和BindingResult 参数,你的Offer 应该用@ModelAttribute 注释。

标签: spring spring-mvc model-view-controller http-status-code-400


【解决方案1】:

BindingResult 参数必须跟在要立即验证的参数之后。这里有描述:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html

org.springframework.validation.Errors / org.springframework.validation.BindingResult 对前面的命令或表单对象(前面的方法参数)的验证结果。

【讨论】:

    【解决方案2】:

    当我将控制器参数更改为模型然后提供它的工作时哇!

    @RequestMapping("/offercreated")

    public String offerCreated(Model model, @Valid Offer offer, BindingResult result) {

    if (result.hasErrors()) {

    返回“创建报价”;

    } 否则 {

    System.out.println("表单验证");

    返回“offercreated”;

    }

    有人可以解释为什么会这样吗?我很困惑!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-22
      • 1970-01-01
      • 2015-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多