【发布时间】:2019-03-18 12:16:56
【问题描述】:
在 Spring MVC 应用程序中,表单数据验证未正确进行,并且 org.springframework.validation.BeanPropertyBindingResult 未报告任何错误。
@RequestMapping(value="downloadfile", method=RequestMethod.POST)
public String submitForm(@ModelAttribute("downloadMVbinder")
@Valid DownloadFileVO downloadFileVO,
BindingResult bindingResult,
Model model) {
System.out.println("Binding result object value ==> " + bindingResult.toString());
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<DownloadFileVO>> constraintViolations = validator.validate(downloadFileVO);
System.out.println("Constrain Violation object value ==>" + constraintViolations.toString());
System.out.println("Model object value ==> " + model.toString());
System.out.println("Model object value ==> " + downloadFileVO.toString());
if (bindingResult.hasErrors()){
return "DownloadfilePage";
}
调试输出是
Binding result object value ==> org.springframework.validation.BeanPropertyBindingResult: 0 errors
Constrain Violation object value ==>[ConstraintViolationImpl{interpolatedMessage='length should be between 5 and 8 characters', propertyPath=password, rootBeanClass=class com.user1.test.model.DownloadFileVO, messageTemplate='length should be between 5 and 8 characters'}, ConstraintViolationImpl{interpolatedMessage='file length cannot be greater than 44 bytes length', propertyPath=fileName, rootBeanClass=class com.user1.test.model.DownloadFileVO, messageTemplate='file length cannot be greater than 44 bytes length'}, ConstraintViolationImpl{interpolatedMessage='length should be between 5 and 8 characters', propertyPath=userName, rootBeanClass=class com.user1.test.model.DownloadFileVO, messageTemplate='length should be between 5 and 8 characters'}]
Model object value ==> {downloadMVbinder=DownloadFormVO [ipAddres=NONEuserName=password=fileName=windowsPath=], org.springframework.validation.BindingResult.downloadMVbinder=org.springframework.validation.BeanPropertyBindingResult: 0 errors}
Model object value ==> DownloadFormVO [ipAddres=NONEuserName=password=fileName=windowsPath=]
我在查看0 errors 后更新了方法参数。 bindingResult 中仍然没有出现错误。我还使用捕获约束冲突的 javax.validation.ConstraintViolation 验证了模型对象。
您能否帮助解决为什么 bindingResult 对象中没有显示错误。
谢谢!
【问题讨论】:
标签: spring-mvc