Adding multiple validators using initBinder

后端 未结 8 1448
傲寒
傲寒 2020-12-04 18:04

I\'m adding a user validator using the initBinder method:

@InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.setVa         


        
相关标签:
8条回答
  • 2020-12-04 18:56

    You can add multiple validators by iterating over all org.springframework.validation.Validator in an ApplicationContext and set up suitable ones in @InitBinder for each request.

    @InitBinder
    public void setUpValidators(WebDataBinder webDataBinder) {
        for (Validator validator : validators) {
            if (validator.supports(webDataBinder.getTarget().getClass())
                    && !validator.getClass().getName().contains("org.springframework"))
                webDataBinder.addValidators(validator);
        }
    }
    

    See my project for examples and simple benchmarks. https://github.com/LyashenkoGS/spring-mvc-and-jms-validation-POC/tree/benchamark

    0 讨论(0)
  • 2020-12-04 18:58

    Declare request as

    (... , Model model,HttpServletRequest request)

    and change

    model.addAttribute(customerPayment);
    

    to

    request.setAttribute("customerPayment",customerPayment);
    
    0 讨论(0)
提交回复
热议问题