title: Spring 3 validation not working
body: <p>I have a user entity in my application which I need to validate. </p>
  
  <pre><code>public class User {
  
  private String userName;
  private String password;
  
  public void setUserName(String userName){
  this.userName = userName;
  }
  
  public getUserName(){
  return this.userName;
  }   
  
  // and so on 
  </code></pre>
  
  <p>}</p>
  
  <p>For this I have created a UsersValidator like below.</p>
  
  <pre><code>public class UserValidator implements Validator {
  
  public boolean supports(Class clazz) {
      return User.class.equals(clazz);
  }
  
  public void validate(Object obj, Errors errors) {
      ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userName", "field.required");
      ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "field.required");
  }
  }
  </code></pre>
  
  <p>and I have a controller like this</p>
  
  <pre><code>@RequestMapping(value = "/login", method = RequestMethod.POST)
  public String home(@Valid User user,
          BindingResult result) {
  
      if (result.hasErrors()) {
          return "loginForm";
      } else {
      //continue
  }
  
  }
  </code></pre>
  
  <p>The binding result does not have any errors. </p>
  
  <p>What else I need to do in order for the validation to work? Do I have make any changes in the controller or the spring configuration file.</p>
  
  <pre><code>&lt;mvc:annotation-driven /&gt;
  
  &lt;context:component-scan base-package="com.myapp" /&gt;
  
  &lt;mvc:resources location="/resources/" mapping="/resources/**" /&gt;
  
  &lt;bean id="viewResolver"
      class="org.springframework.web.servlet.view.UrlBasedViewResolver"&gt;
      &lt;property name="viewClass"&gt;
          &lt;value&gt;org.springframework.web.servlet.view.tiles2.TilesView&lt;/value&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="tilesConfigurer"
      class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"&gt;
      &lt;property name="definitions"&gt;
          &lt;list&gt;
              &lt;value&gt;/WEB-INF/tiles.xml&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="messageSource"
      class="org.springframework.context.support.ResourceBundleMessageSource"&gt;
      &lt;property name="basenames"&gt;
          &lt;list&gt;
              &lt;value&gt;com/dimex/resourceBundles/ApplicationResources&lt;/value&gt;
              &lt;value&gt;com/dimex/resourceBundles/errors&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;            
    &lt;/bean&gt;
  
  &lt;mvc:interceptors&gt;  
      &lt;bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"&gt;
          &lt;property name="paramName" value="locale"&gt;&lt;/property&gt;
      &lt;/bean&gt;
  &lt;/mvc:interceptors&gt;
  
  &lt;bean id="localeResolver"
      class="org.springframework.web.servlet.i18n.SessionLocaleResolver"&gt;
      &lt;property name="defaultLocale" value="en" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p><strong>EDIT:-</strong></p>
  
  <p>Do I need to have hibernate validator in my classpath. We are not using hibernate in our application. 
  Please help.</p>
  
  <p><strong>EDIT2:-</strong></p>
  
  <p>When I use validation annotations (@NotNull, @Size etc) directly in my entity class then @Valid annotations in controller works but if I remove them from my entities and try to use the validator written above then @Valid does  not work. </p>
  
  <p>Is it like that @Valid annotations only work with the validation annotation in the entities only and not with the validators? In order to use my validators will I have to invoke the validate method in my validator directly?</p>