BindingResult总是返回0个错误Hibernate Validator



我对Hibernate Validator有问题,更确切地说,对BindingResult和method.hasError((有问题。即使对象为null,它也总是返回true。请在此处查看我的代码:https://github.com/jeddyn/spring-mvc-demo

Customer: null customer first name: null
bindingresult: org.springframework.validation.BeanPropertyBindingResult: 0 errors
Customer: null customer first name: null
bindingresult: org.springframework.validation.BeanPropertyBindingResult: 0 errors

尝试在pom.xml 中添加以下依赖项

<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version> 
</dependency>

您可以更改与hibernate验证器兼容的版本。

@Valid放在@ModelAttribute之前,改变顺序如下,很奇怪,但它有效。

public String processForm(
@Valid @ModelAttribute("customer") Customer customer,
BindingResult theBindingResult)

参考本

对我来说,它是这样工作的:

@PostMapping("/registration/post")
public String processRegistrationForm(@ModelAttribute("registrationForm") @Valid RegistrationForm registrationForm, BindingResult bindingResult, HttpServletRequest request, RedirectAttributes redirectAt) {...}

bindingResult之前总是0,因为我忘记添加spring-boot-starter-validationpom.xml:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>

相关内容

  • 没有找到相关文章

最新更新