【问题标题】:Spring + JSR 303 Validation group is ignoredSpring + JSR 303 验证组被忽略
【发布时间】:2012-11-24 09:13:03
【问题描述】:

我们有一个带有 JSR 注释的简单 bean

公共类 CustomerDTO { 私有静态最终长序列版本UID = 1L; 私人整数 id; @NotEmpty(message = "{customer.firstname.empty}") 私人字符串名; @NotEmpty(message = "{customer.lastname.empty}") 私人字符串姓氏; @NotEmpty(groups={PasswordChange.class}, message="{password.empty}") 私人字符串密码; @NotEmpty(groups={PasswordChange.class}, message="{confirmation.password.empty}") 私人字符串密码2; }

我们有一个 Spring 控制器

@RequestMapping(value="/changePassword", 方法 = RequestMethod.POST) public String changePassword(@Validated({ PasswordChange.class }) @ModelAttribute("customerdto") CustomerDTO customerDTO, BindingResult 结果, Locale locale) { logger.debug("更改密码已提交信息:" + customerDTO.toString()); 尝试 { passwordStrengthPolicy.checkPasswordStrength(locale, customerDTO.getPassword()); if (result.hasErrors()) { 返回“更改密码”; } logger.debug("调用客服更改密码:" + customerDTO); customerOnlineNewService.changePassword(customerDTO); } 捕捉(PasswordNotChangedException e){ logger.error("无法更改密码 PasswordNotChangedException:" + customerDTO.toString()); 返回“更改密码”; } 捕捉(PasswordNotSecureException e){ 返回“更改密码”; } return createRedirectViewPath("changePassword"); }

我们的问题是,当调用 changePassword 时,验证器会忽略组(PasswordChange.class)并仅验证不在组中的 firstName 和 lastName。

有什么想法吗? 非常感谢您的宝贵时间。

【问题讨论】:

  • 我觉得不错。你打开调试跟踪了吗?
  • 没有奇怪的信息。 changePassword 方法运行并且 result.hasErrors() 为真,因为它验证了整个 bean。

标签: spring spring-mvc bean-validation


【解决方案1】:

可能与验证顺序有关? (http://docs.oracle.com/cd/E19798-01/821-1841/gkahp/index.html)-->自定义组验证顺序 听起来绝对像!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 2013-11-23
    • 2013-12-27
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 2011-05-10
    相关资源
    最近更新 更多