title: Spring MVC 3 Form Validator not displaying Error message in .jsp
body: <p>I entered in <strong>Spring Web MVC Framework</strong> not long ago thus I am a complete beginner. Right now I am implementing a basic <strong>form</strong> for my first <strong>web application</strong>.</p>
  
  <p>In the same time I would like to say that I've been seeking a solution for my problem for whole day. I apolgize in advance if the solution for similar problem was already published.</p>
  
  <p>Source code:</p>
  
  <p><em>spring-servlet.xml</em>
  </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:p="http://www.springframework.org/schema/p"
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="http://www.springframework.org/schema/beans
                              http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                              http://www.springframework.org/schema/context
                              http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
      &lt;context:component-scan base-package="si.src.controllers" /&gt;
      &lt;context:component-scan base-package="si.src.validators" /&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.JstlView&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="prefix"&gt;
              &lt;value&gt;/WEB-INF/jsp/&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>
  
  <hr>
  
  <p><em>index.jsp</em></p>
  
  <pre><code>&lt;!-- language: lang-jsp --&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Spring 3.0 MVC Series - Index&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;br&gt;
      &lt;div align='center'&gt;
          &lt;p&gt;
          &lt;h1&gt;Example - Spring Application&lt;/h1&gt;
          The "index.jsp" is the entry point for our application.
          This is my first test. Work!            
          &lt;p&gt;
          &lt;a href="login.html"&gt;Welcome! Click Here to Login&lt;/a&gt;           
      &lt;/div&gt;      
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <hr>
  
  <p><em>login.jsp</em></p>
  
  <pre class="lang-jsp prettyprint-override"><code>&lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;
  &lt;%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %&gt;
  &lt;%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %&gt;
  
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Spring Sample - Login Page&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;h3&gt;Login Page&lt;/h3&gt;&lt;br&gt;
      &lt;form:form id="form" method="post" commandName="loginForm"&gt;
          &lt;form:errors path="*" cssClass="errorblock" element="div" /&gt;
          &lt;table&gt;
              &lt;tr&gt;
                  &lt;td&gt;&lt;form:label path="username"&gt;Username&lt;/form:label&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;form:input path="username" /&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;form:errors path="username" cssClass="error"/&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;&lt;form:label path="username"&gt;Password&lt;/form:label&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;form:input path="password" /&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;form:errors path="password" cssClass="error"/&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td colspan="2"&gt;&lt;input type="submit" value="Sign in" /&gt;&lt;/td&gt;
              &lt;/tr&gt;
          &lt;/table&gt;
      &lt;/form:form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <hr>
  
  <p><em>LoginFormController.java</em></p>
  
  <pre class="lang-java prettyprint-override"><code>package si.src.controllers;
  
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import org.springframework.stereotype.Controller;
  import org.springframework.validation.BindException;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.servlet.mvc.SimpleFormController;
  import org.springframework.web.servlet.ModelAndView;
  
  import si.src.logic.Login;
  import si.src.validators.LoginValidator;
  
  @Controller
  @RequestMapping(value="/login")
  public class LoginFormController extends SimpleFormController{
  
      public LoginFormController(){
          setCommandClass(Login.class);   //Form's values will store into the Login object    
          setCommandName("loginForm");    //If HTML form action value with named "loginForm" is sumbitted, Spring will forward request to this form controller
      }
  
      @RequestMapping(method=RequestMethod.POST)
      protected ModelAndView onSubmit(HttpServletRequest request,
          HttpServletResponse response, Object command, BindException errors) throws Exception{
  
          LoginValidator validator = new LoginValidator();        
          Login userLogin = (Login) command;
          validator.validate(userLogin, errors.getBindingResult());
  
          if(errors.hasErrors()){
              userLogin.setUsername("");
              userLogin.setPassword("");
              System.out.println("Ne");
              return new ModelAndView("login","loginForm", userLogin);
  
          }
          else{
              System.out.println(userLogin);
              System.out.println(userLogin.getUsername() + " " + userLogin.getPassword());    
              return new ModelAndView("success","userLogin", userLogin);
          }
      }
  
      protected Object formBackingObject(HttpServletRequest request) throws Exception {   
          //Initialize the values in the form. Not necessary
          Login userLogin = new Login();
          userLogin.setUsername("Admin");
          return userLogin;
      }
  
  }
  </code></pre>
  
  <hr>
  
  <p><em>LoginValidator.java</em></p>
  
  
  
  <pre><code>package si.src.validators;
  
  import org.springframework.validation.Errors;
  import org.springframework.validation.Validator;
  import org.springframework.validation.ValidationUtils;
  
  import si.src.logic.Login;
  
  public class LoginValidator implements Validator{
  
      public boolean supports(Class aClass) {
          //just validate the Login instances
          return Login.class.isAssignableFrom(aClass);
      }
  
      public void validate(Object obj, Errors errors) {
          Login login = (Login) obj;
  
          ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username","required-username", "Enter username");
          ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password","required-password", "Enter password");    
      }   
  }
  </code></pre>
  
  <hr>
  
  <p><em>Login.java</em></p>
  
  <pre class="lang-java prettyprint-override"><code>package si.src.logic;
  
  public class Login {
  
      private String username;
      private String password;
  
      public Login(){}
  
      public void setUsername(String username){
          this.username=username;
      }
  
      public String getUsername(){
          return username;
      }
  
      public void setPassword(String password){
          this.password=password;
      }
  
      public String getPassword(){
          return password;
      }   
  }
  </code></pre>
  
  <hr>
  
  <p>I suppose the error messages obtained by <strong>LoginValidator</strong> object are not correctly "binded" with my <strong>login.jsp</strong> file. I guess that is the reason why the <strong>error messages</strong> i.e <em>"Enter username"</em> and <em>"Enter password"</em> don't show up when my form is poorly fulfilled.</p>
  
  <p>My question is: Why the error messages are not showing up in a login.jsp?</p>