Spring MVC form validation not working

前端 未结 6 1989
日久生厌
日久生厌 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.

    0 讨论(0)
  • 2021-01-06 01:03

    i also faced the same issue where my entire code was properly written.

    The problem was the different version of jar files that i was using .

    I had hibernate-validator-cdi- 5.0.7.Final and hibernate-validator-6.0.2.Final.

    Just make sure your jar files are of the same version.

    When i kept all the jars of same version, my issue got resolved. I hope this will help you .

    0 讨论(0)
  • 2021-01-06 01:05

    I faced the same problem. I resolved by adding below statement in dispatcher-serlvet.xml file.

    <mvc:annotation-driven />
    
    0 讨论(0)
  • 2021-01-06 01:09

    You need to add

    <bean id="myBeansValidator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
    

    and

    <mvc:annotation-driven validator="myBeansValidator">
    

    and

        <!-- Hibernate Validator -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.2.0.Final</version>
        </dependency>
    

    Should work!

    0 讨论(0)
  • 2021-01-06 01:09

    I'm not sure whether you have found ways to fix this. I am facing the same issue as well. and I managed to solve it. The problem with my setting is totally manual and I'm doing big mistake by placing the whole hibernate-validator-5.1.3.Final-dist.zip inside lib folder.

    So what I did is I get this 6 files inside "hibernate-validator-5.1.3.Final-dist.zip" in dist folder and place this with web app lib.

    • classmate-1.0.0.jar
    • hibernate-validator-5.1.3.Final.jar
    • javax.el-2.2.4.jar
    • javax.el-api-2.2.4.jar
    • jboss-logging-3.1.3.GA.jar
    • validation-api-1.1.0.Final.jar

    This fixed my issue.

    0 讨论(0)
  • 2021-01-06 01:09

    In my case hibernate-validator jars are not available in run time. I've copied them to .../WEB-INF/lib/ then it worked correctly.

    0 讨论(0)
提交回复
热议问题