Spring boot 验证器valid必须后面紧跟着BindResult

遇到一个坑的问题,BindResult exception始终捕获不到,查了半天资料,原来BindResult必须紧跟着@Valid后面

 

例如

 

@RequestMapping(value="/", method=RequestMethod.POST)
public String checkPersonInfo(@Valid Person person, BindingResult bindingResult, Model model) {
    if (bindingResult.hasErrors()) {
        return "form";
    }
    return "redirect:/results";
}

原因是: 

 

    The BindingResult has to follow the object that is bound. The reason is that if you have more objects that are bound you must know which BindingResult belongs to which object.

 

https://stackoverflow.com/questions/28887780/why-does-bindingresult-have-to-follow-valid

 

分类: java 标签: 发布于: 2020-09-16 09:50:10, 点击数: