我在春天控制器下面的代码:转换JSR-303验证错误Spring的BindingResult
@Autowired
private javax.validation.Validator validator;
@RequestMapping(value = "/submit", method = RequestMethod.POST)
public String submitForm(CustomForm form) {
Set<ConstraintViolation<CustomForm>> errors = validator.validate(vustomForm);
...
}
是否有可能errors
映射到Spring的BindingResult
对象没有通过所有的错误,手动去,并将它们添加到BindingResult
?事情是这样的:
// NOTE: this is imaginary code
BindingResult bindingResult = BindingResult.fromConstraintViolations(errors);
我现在就可以注释CustomForm
参数与@Valid
并让Spring注入BindingResult
作为另一种方法的参数,但它不是我的情况选择。
// I know this is possible, but doesn't work for me
public String submitForm(@Valid CustomForm form, BindingResult bindingResult) {
...
}
谢谢!这绝对更简洁。 – 2013-02-15 11:35:06
我看不到接受BindingResult的方法验证 – gstackoverflow 2015-04-13 23:04:43
如果您没有看到接受绑定结果的validate方法,则表示您的Validator验证程序是javax.validation.Validator。将其更改为org.springframework.validation.Validator,你会没事的。无需更改bean定义。 – Icegras 2016-12-08 15:52:56