title: Spring MVC form validation not working
body: <p>I am using Spring 4.
  My form contains the following variables:</p>
  
  <pre><code>@NotNull
  @Email
  private String email;
  @NotNull
  private String firstName;
  @NotNull
  private String lastName;
  @Digits(fraction = 0, integer = 10)
  private String phoneNo;
  @NotNull
  private String role;
  </code></pre>
  
  <p>My controller:</p>
  
  <pre><code>@Controller
  @RequestMapping("/user")
  public class UserController {
  
      @RequestMapping(value = "/add", method = RequestMethod.POST)
      public ModelAndView add(@ModelAttribute("user") @Valid UserBean user, BindingResult result) {
      String message;
      if (result.hasErrors() &amp;&amp; user != null)
          return new ModelAndView("userAdd");
      else {
          userService.addUser(user);
          message = "Successfully added user";
      }
      return new ModelAndView("success", "message", message);
      }
  
      @RequestMapping(value = "/register")
      public ModelAndView register(@ModelAttribute("user") UserBean user, BindingResult result) {
      List&lt;String&gt; roles = new ArrayList&lt;String&gt;();
      roles.add("Receiver");
      roles.add("Resolver");
      roles.add("Logger");
      Map&lt;String, List&lt;String&gt;&gt; model = new HashMap&lt;String, List&lt;String&gt;&gt;();
      model.put("roles", roles);
      return new ModelAndView("userAdd", "model", model);
      }
  }
  </code></pre>
  
  <p>My jsp:</p>
  
  <pre><code>&lt;c:url var="userAdd" value="user/add.do" /&gt;
      &lt;sf:form method="post" action="${userAdd}" modelAttribute="user"&gt;
          &lt;table&gt;
              &lt;tr&gt;
                  &lt;td&gt;First Name&lt;/td&gt;
                  &lt;td&gt;&lt;sf:input path="firstName" /&gt;&lt;br /&gt; &lt;sf:errors
                          path="firstName" cssClass="error" /&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;Last Name&lt;/td&gt;
                  &lt;td&gt;&lt;sf:input path="lastName" /&gt;&lt;br /&gt; &lt;sf:errors
                          path="lastName" cssClass="error" /&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;Email&lt;/td&gt;
                  &lt;td&gt;&lt;sf:input path="email" /&gt;&lt;br /&gt; &lt;sf:errors
                          path="email" cssClass="error" /&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;Phone No.&lt;/td&gt;
                  &lt;td&gt;&lt;sf:input path="phoneNo" /&gt;&lt;br /&gt; &lt;sf:errors
                          path="phoneNo" cssClass="error" /&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;Role&lt;/td&gt;
                  &lt;td&gt;&lt;sf:select path="role" items="${model.roles}" /&gt;&lt;br /&gt; &lt;sf:errors
                          path="role" cssClass="error" /&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;&lt;input type="submit" value="Submit" /&gt;&lt;/td&gt;
              &lt;/tr&gt;
          &lt;/table&gt;
      &lt;/sf:form&gt;
  </code></pre>
  
  <p>When I leave the the inputs blank, the form does not validate and does not throw any error. The <code>BindingResult</code> does not have any errors in it.</p>
  
  <p>My libraries are:
  <img src="https://i.stack.imgur.com/1zcRC.png" alt="Libraries"></p>
  
  <p>My <code>dispatcher-serlvet.xml</code> is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"&gt;
      &lt;!-- Scans for annotated @Controllers in the classpath --&gt;
      &lt;context:component-scan base-package="com.mj.cchp.controller"&gt;
          &lt;context:include-filter type="annotation"
              expression="org.springframework.stereotype.Controller" /&gt;
      &lt;/context:component-scan&gt;
      &lt;bean id="myBeansValidator"
          class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" /&gt;
      &lt;mvc:annotation-driven validator="myBeansValidator" /&gt;
      &lt;bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="prefix"&gt;
              &lt;value&gt;/WEB-INF/pages/&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="suffix"&gt;
              &lt;value&gt;.jsp&lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>My <code>applicationContext</code> is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"&gt;
      &lt;!-- Scans for annotated @Controllers in the classpath --&gt;
      &lt;context:component-scan base-package="com.mj.cchp"&gt;
          &lt;context:exclude-filter type="annotation"
              expression="org.springframework.stereotype.Controller" /&gt;
      &lt;/context:component-scan&gt;
      &lt;bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver" /&gt;
          &lt;property name="url" value="jdbc:db2://172.16.2.181:60000/CCHP" /&gt;
          &lt;property name="username" value="db2inst1" /&gt;
          &lt;property name="password" value="db2inst1" /&gt;
      &lt;/bean&gt; 
  &lt;/beans&gt;
  </code></pre>
  
  <p>My <code>web.xml</code> is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://java.sun.com/xml/ns/javaee"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
      id="WebApp_ID" version="3.0"&gt;
      &lt;display-name&gt;CCHP&lt;/display-name&gt;
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/application-context.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;!-- Creates the Spring Container shared by all Servlets and Filters --&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  &lt;/web-app&gt;
  </code></pre>