springMVC 5.1.6 validation not working (BindingResult errors are 0 ?)

188 Views Asked by At

I am facing an issue with my "User" Object being Validated before being registered into a database

Used Technologies : - Spring Web MVC with Hibernate Validator - Spring Data (JDBC) - Maven

Here is a snippet from my Controller Code :

 @RequestMapping(method = RequestMethod.POST,params = "add")
public ModelAndView add(@Valid @ModelAttribute("user") User user, BindingResult result) throws Exception {

    if (!result.hasErrors()) { // ERRORS NEVER HAPPEN ??
        userDao3.insertUser(user);
    }
    return returnHome();
}

my User Validation is as follows :

@Size(min = 3, max = 20, message = "{required.name}")
String name;
@Min(value = 25, message = "{min.age}")
int age;
@Min(value = 2000, message = "{invalid.salary}")
float salary;

my jsp layout as follows :

<c:set var="contextPath" value="${pageContext.request.contextPath}"/>
<div class="container">
<form:form method="post" action="${contextPath}/?add" modelAttribute="user">
    <div class="row">
        <div class="col-15">
            <tags:message code="field.username"/>
        </div>
        <div class="col-50">
            <form:input cssClass="field" path="name"/>
        </div>
        <div class="col-25">
            <form:errors path="name"/>
        </div>
    </div>

    <div class="row">
        <div class="col-15">
            <tags:message code="field.age"/>
        </div>
        <div class="col-50">
            <form:input cssClass="field" path="age"/>
        </div>
        <div class="col-25">
            <form:errors path="age"/>
        </div>
    </div>
    <div class="row">
        <div class="col-15">
            <tags:message code="field.salary"/>
        </div>
        <div class="col-50">
            <form:input cssClass="field" path="salary"/>
        </div>
        <div class="col-25">
            <form:errors path="salary"/>
        </div>
    </div>
    <div class="row">
        <input type="submit" value="Submit">
    </div>
</form:form>

my Spring mvc Configuration xml file :

<mvc:annotation-driven/>

<bean name="homeController" class="Controllers.HomeController">
    <property name="userDao2" ref="userDao2"/>
    <property name="userDao3" ref="userDao3"/>
</bean>

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
    <list>
        <value>naming</value>
        <value>ValidationMessages</value>
    </list>
</property>
</bean>

and my pom's dependencies are :

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-validator</artifactId>
  <version>5.2.5.Final</version>
</dependency>
<dependency>
  <groupId>javax.xml.bind</groupId>
  <artifactId>jaxb-api</artifactId>
  <version>2.3.1</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>5.1.6.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>5.1.6.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>5.1.6.RELEASE</version>
</dependency>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>8.0.19</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>5.1.6.RELEASE</version>
</dependency>
<dependency>
  <groupId>javax</groupId>
  <artifactId>javaee-web-api</artifactId>
  <version>7.0</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>jstl</groupId>
  <artifactId>jstl</artifactId>
  <version>1.2</version>
</dependency>

<dependency>
  <groupId>javax.servlet.jsp.jstl</groupId>
  <artifactId>jstl-api</artifactId>
  <version>1.2</version>
</dependency>
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.1.0</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>javax.servlet.jsp</groupId>
  <artifactId>jsp-api</artifactId>
  <version>2.2</version>
  <scope>provided</scope>
</dependency>

the issue is : that on debugging found that errors are always equal to 0 in the BindingResult Object.

Debugging results:

enter image description here

Please help me understand the issue.

0

There are 0 best solutions below