Spring MVC form validation not working

前端 未结 6 2027
日久生厌
日久生厌 2021-01-06 00:27

I am using Spring 4. My form contains the following variables:

@NotNull
@Email
private String email;
@NotNull
private String firstName;
@NotNull
private Stri         


        
6条回答
  •  天命终不由人
    2021-01-06 01:01

    I had a similar problem and in my case it was sufficient to just add dependency for hibernate validator: org.hibernate:hibernate-validator:5.2.4.Final.

    The validation is done by LocalValidatorFactoryBean bean and documentation about it comes handy (here).

    Yet at the same time it is worth mentioning that you do not have to instantiate LocalValidatorFactoryBean explicitly as long as you use @EnableWebMvc annotation : http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-config-validation

    By default use of @EnableWebMvc or automatically registers Bean Validation support in Spring MVC through the LocalValidatorFactoryBean when a Bean Validation provider such as Hibernate Validator is detected on the classpath.

    Hope this helps.

提交回复
热议问题