【问题标题】:Spring validator never calledSpring验证器从未调用过
【发布时间】:2011-07-19 16:20:42
【问题描述】:

我正在尝试验证一些信息,所以我添加了一个验证器,并在 post 方法的参数中使用了@Valid:

@Controller
@RequestMapping("/user.htm")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping(method = RequestMethod.GET)
    public String userInfo(Model model) {
       ....
        return "user";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String userInfoResult(@Valid @ModelAttribute UserForm userForm, BindingResult result, Model model ) {

        UserInfo stat = userService.getStatitisque(userForm.getSearchCritera());
        userForm.setListeExpediteur(listeExpediteur);

        userForm.setUserInfo(stat);
        model.addAttribute("userForm", userForm);
    }    
}

public class UserFormValidator implements Validator {

    @Override
    public boolean supports(Class<?> type) {
        return UserForm.class.equals(type);
    }

    @Override
    public void validate(Object o, Errors errors) {
        UserForm userForm = (User) o;
        ...
    }
}

当我调试时,我从不进入 UserFormValidator 类。

我需要在这些文件中添加一些东西吗?

  • web.xml
  • applicationContext.xml
  • dispatcher-servlet.xml

【问题讨论】:

    标签: java spring spring-mvc validation


    【解决方案1】:

    您需要在@InitBinder 方法中添加验证器

     @InitBinder
     protected void initBinder(WebDataBinder binder) {
         binder.setValidator(new UserFormValidator ());
     }
    

    【讨论】:

      【解决方案2】:

      您需要在@InitBinder 方法中添加验证器:

      @InitBinder(value="YourFormObjectName")
      protected void initBinder(WebDataBinder binder) {
          binder.setValidator(new FooValidator());
      }
      

      或通过 XML 全局:

      <mvc:annotation-driven validator="globalValidator"/>
      

      参考:

      【讨论】:

      • 我觉得很奇怪,Spring 要求您手动将每个验证器类添加到每个控制器,而它几乎可以自动执行所有其他操作。选择这种特殊的 API 有什么原因吗?
      • @LordOfThePigs 您不必手动添加它 - 您可以编写类似“ValidatorAdvice”服务,它将使用 @ControllerAdvice 注释。它会将您的验证器添加到每个控制器。示例:@ControllerAdvice public class ValidatorAdvice { @Autowired protected LocalValidatorFactoryBean validator; @InitBinder public void initBinder(WebDataBinder binder) { binder.addValidators(new CollectionValidator(validator)); } }
      • @thorinkor 是的,但是在我写这个答案时这种机制并不存在
      • @SeanPatrickFloyd 哦,我不知道这一点,但无论如何值得一提 - 像我这样的人仍在寻找答案 :) 更重要的一件事 - @InitBinder 应该指定 value 参数- 如果你不指定它,验证器将用于每个参数:)
      【解决方案3】:
      @Controller
      @RequestMapping("/user.htm")
      public class UserController {
      @Autowired
      private IUserService userService;
      
      @RequestMapping(method = RequestMethod.GET)
      public String userInfo(Model model) {
         ....
          return "user";
      }
      
      @RequestMapping(method = RequestMethod.POST)
      public String userInfoResult(Model model,
                                   @Valid @ModelAttribute UserForm userForm,
                                   BindingResult result) {
          /*
             add custom validation check to standard validation error
             (if not registry UserFormValidator in @InitBinder block)
          */
          new UserFormValidator().validate(userForm, error); 
      
          // check all validation errors   
          if (errors.hasErrors()) {
              // go back
              return userInfo(model);
          }
      
          ...
        } 
      }
      

      【讨论】:

      • 这是第三天,但@InitBinder 在配置自定义验证器的所有可能方法中更可取
      • error 变量从何而来?它是如何实例化的?
      【解决方案4】:

      5.7.4.3 配置 JSR-303 验证器以供 Spring MVC 使用

      使用 JSR-303,单个 javax.validation.Validator 实例通常会验证所有声明验证约束的模型对象。要使用 Spring MVC 配置 JSR-303 支持的验证器,只需将 JSR-303 提供程序(例如 Hibernate Validator)添加到您的类路径。 Spring MVC 会检测到它并自动在所有控制器中启用 JSR-303 支持。

      <mvc:annotation-driven/>
      

      使用这个最小配置,只要遇到@Valid @Controller 输入,JSR-303 提供程序就会对其进行验证。反过来,JSR-303 将强制执行针对输入声明的任何约束。任何 ConstraintViolations 都会自动暴露为 BindingResult 中的错误,该错误可通过标准 Spring MVC 表单标签呈现。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-15
        • 1970-01-01
        • 1970-01-01
        • 2023-03-18
        • 1970-01-01
        • 2014-06-03
        • 2012-08-09
        • 1970-01-01
        相关资源
        最近更新 更多