【问题标题】:Spring MVC Form Validation - The request sent by the client was syntactically incorrectSpring MVC 表单验证 - 客户端发送的请求在语法上不正确
【发布时间】:2013-02-06 18:21:40
【问题描述】:

我正在尝试将表单验证添加到工作应用程序。我首先在登录表单中添加了一个 NotNull 检查。我正在使用 Bean Validation api 的 Hibernate impl。

这是我写的代码

@Controller
@RequestMapping(value="/login")
@Scope("request")
public class LoginController {

  @Autowired
  private CommonService commonService;

  @Autowired
  private SiteUser siteUser;

  @InitBinder
  private void dateBinder(WebDataBinder binder) {
      SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
      CustomDateEditor editor = new CustomDateEditor(dateFormat, true);
      binder.registerCustomEditor(Date.class, editor);
  }

  @ModelAttribute
  protected ModelMap setupForm(ModelMap modelMap) {
    modelMap.addAttribute("siteUser", siteUser);
    return modelMap;
  }

  @RequestMapping(value="/form", method = RequestMethod.GET)
  public ModelAndView form(ModelMap map){
    if (siteUser.getId() == null){
      map.addAttribute("command",new SiteUser());
      return new ModelAndView("login-form",map);
    }else {
      return new ModelAndView("redirect:/my-dashboard/"+siteUser.getId());
    }
  }

  @RequestMapping(value="/submit", method=RequestMethod.POST)
  public ModelAndView submit(@Valid SiteUser user, ModelMap map, BindingResult result){
    if (result.hasErrors()) {
      map.addAttribute("command", user);
      System.out.println("Login Error block");
      return new ModelAndView("login/form",map);
    }
    else {
      User loggedInUser = commonService.login(user.getEmail(), user.getPassword());
      if (loggedInUser != null) {
        siteUser.setId(loggedInUser.getId());
        siteUser.setName(loggedInUser.getName());
        System.out.println("site user attr set");
      }
      return new ModelAndView("redirect:/my-dashboard/"+loggedInUser.getId());
    }
  }
}

模型是

@Component
@Scope("session")
public class SiteUser {
private Integer id = null;
@NotNull
private String name = null;
private String email = null;
private String password = null;
private List<String> displayPrivList = null;
private List<String> functionPrivList = null;
    // And the getters and setters
}

JSP 是

    <c:url var="loginSubmitUrl" value="/login/submit"/>
    <form:form method="POST" action="${loginSubmitUrl}">
        <form:errors path="*" />
        <div class="row">
            <div class="span4">
            </div>
            <div class="span4">
                <h3>Please Login</h3>
                <label><span style="color:red">*</span>Email</Label><form:input path="email" type="text" class="input-medium" />
                <label><span style="color:red">*</span>Password</Label><form:input path="password" type="password" class="input-medium" />
                <br/>       
                <button type="submit" class="btn btn-primary">Login</button>
                <button type="button" class="btn">Cancel</button>
            </div>
        </div>          
    </form:form>

我在上下文 xml 中添加了 messages.properties 和注释驱动的 bean def。 关于该主题的其他答案谈论未发布的表单字段。就我而言,这是预期的行为 - 如果我提交一个空白表单,我应该会收到一个错误。

请告知我缺少什么?

【问题讨论】:

  • 发布日志消息会有所帮助。Spring 通常会以非常详细的方式报告所有这些类型的错误。
  • 在这种情况下,没有日志消息。即使是“登录错误块”系统输出也不会打印出来。有趣的是,如果我从方法签名中删除“ModelMap map”参数,代码确实会进入错误块,但随后会显示“Bean name 'command' 的 BindingResult 和普通目标对象都不能用作请求属性”
  • 我不确定,但是将@ModelAttribute 与Valid 一起添加到您的命令对象中并尝试

标签: validation spring-mvc tomcat7 hibernate-validator


【解决方案1】:

我认为这个问题和你的问题一样

Syntactically incorrect request sent upon submitting form with invalid data in Spring MVC (which uses hibernate Validator)

这只是指出

您必须修改参数的顺序。将 BindingResult 结果参数始终直接放在带有 @Value 注释的参数之后

【讨论】:

  • +1,试试这个方法不会有什么坏处,而且肯定对我的问题有帮助!
【解决方案2】:

你需要这个:&lt;form:errors path="email" cssClass="errors" /&gt; 对具有相同“路径”名称的每个输入使用标签形式:错误。

不放路径也可以同时列出所有的错误。

在这里,查看带有示例代码的完整示例,您可以下载该示例代码以了解如何操作: http://www.mkyong.com/spring-mvc/spring-3-mvc-and-jsr303-valid-example/

【讨论】:

    【解决方案3】:

    你可以尝试通过像这样包含 commandName 来更改&lt;form:form&gt;

         <form:form method="POST" action="${loginSubmitUrl}" commandName="user"> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多