title: Adding custom error messages for validation in Spring-MVC
body: <p>I am using Spring-MVC and I would like to do validation tests in backend. Currently I am able to perform the tests, redirect properly to the link. But I am only unable to add custom error messages incase there is a problem, Example : "Firstname is empty". I tried using the ValidationMessages_en_EN.properties file in WEB-INF, but that also didn't help. I am posting my code, kindly let me know where I am going wrong :</p>
  
  <p>COntroller :</p>
  
  <pre><code>@Controller
  public class PersonController {
   @RequestMapping(value = "/", method = RequestMethod.GET)
      public String listPersons(Model model) {
          Person person = personService.getCurrentlyAuthenticatedUser();
          if(!(person==null)){
              return "redirect:/canvas/list";
          } else {
              model.addAttribute("person", new Person());
           //   model.addAttribute("listPersons", this.personService.listPersons());
              model.addAttribute("notices",new Notes());
              model.addAttribute("canvases",new Canvas());
              return "person";
          }
      }
  
      @RequestMapping(value= "/person/add", method = RequestMethod.POST)
      public String addPerson(@Valid Person person,BindingResult bindingResult,@ModelAttribute("person") Person p,Model model){
          if(bindingResult.hasErrors()){
              return "redirect:/";
          }
              this.personService.addPerson(p);
              return "redirect:/";
      }
  </code></pre>
  
  <p>Entity :</p>
  
  <pre><code>@Entity
  @Table(name="person")
  public class Person implements UserDetails{
  
      private static final GrantedAuthority USER_AUTH = new SimpleGrantedAuthority("ROLE_USER");
  
      @Id
      @Column(name="id")
      @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "person_seq_gen")
      @SequenceGenerator(name = "person_seq_gen",sequenceName = "person_seq")
      private int id;
  
      @Valid
      @NotEmpty @Email(message = "username.empty")
      @Column(name = "username")
      private String username;
  
  
      @NotEmpty
      @Column(name = "password")
      private String password;
  
  
      @Valid
      @Size(min = 2,max = 30,message = "firstName.empty")
      @Column(name = "firstname")
      private String firstName;
  
      @Valid
      @Size(min = 2,max = 50)
      @Column(name = "secretquestion")
      private String secretquestion;
  
      @Valid
      @Size(min = 2,max = 500,message = "secretanswer.empty")
      @Column(name = "secretanswer")
      private String secretanswer;
  </code></pre>
  
  <p>Servlet-Context.xml </p>
  
  <pre><code>&lt;beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"&gt;
          &lt;beans:property name="basename" value="ValidatorMessages_en_EN.properties"/&gt;
             &lt;/beans:bean&gt;
  </code></pre>
  
  <p>JSP/HTML :</p>
  
  <pre><code>      &lt;c:url var="addAction" value="/person/add" &gt;&lt;/c:url&gt;
  &lt;form:form action="${addAction}" commandName="person" method="post"&gt;
  &lt;table&gt;
      &lt;c:if test="${!empty person.username}"&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &lt;form:label path="id"&gt;
                  &lt;spring:message text="ID"/&gt;
              &lt;/form:label&gt;
          &lt;/td&gt;
          &lt;td&gt;
              &lt;form:input path="id" readonly="true" size="8"  disabled="true" /&gt;
              &lt;form:hidden path="id" /&gt;
          &lt;/td&gt; 
      &lt;/tr&gt;
      &lt;/c:if&gt;
  
      &lt;tr&gt;
          &lt;td&gt;
              &lt;form:label path="firstName"&gt;
                  &lt;spring:message text="FirstName"/&gt;
              &lt;/form:label&gt;
          &lt;/td&gt;
          &lt;td&gt;
              &lt;form:input path="firstName" /&gt;
          &lt;/td&gt;
          &lt;td&gt;&lt;form:errors path="firstName"/&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &lt;form:label path="username"&gt;
                  &lt;spring:message text="Email"/&gt;
              &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"/&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &lt;form:label path="password"&gt;
                  &lt;spring:message text="Password"/&gt;
              &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"/&gt;
      &lt;/tr&gt;
  
      &lt;tr&gt;
      &lt;td&gt;
          &lt;form:label path="secretquestion"&gt;
              &lt;spring:message text="secretquestion"/&gt;
          &lt;/form:label&gt;
      &lt;/td&gt;
      &lt;td&gt;
          &lt;form:input path="secretquestion" /&gt;
      &lt;/td&gt;
          &lt;td&gt;&lt;form:errors path="secretquestion"/&gt;
      &lt;/tr&gt;
  
  
      &lt;tr&gt;
          &lt;td&gt;
              &lt;form:label path="secretanswer"&gt;
                  &lt;spring:message text="secretanswer"/&gt;
              &lt;/form:label&gt;
          &lt;/td&gt;
          &lt;td&gt;
              &lt;form:input path="secretanswer" /&gt;
          &lt;/td&gt;
          &lt;td&gt;&lt;form:errors path="secretanswer"/&gt;
      &lt;/tr&gt;
  </code></pre>
  
  <p>POM.xml :</p>
  
  <pre><code> &lt;!-- Validation --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.validation&lt;/groupId&gt;
              &lt;artifactId&gt;validation-api&lt;/artifactId&gt;
              &lt;version&gt;1.0.0.GA&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
              &lt;version&gt;4.3.1.Final&lt;/version&gt;
          &lt;/dependency&gt;
  </code></pre>
  
  <p>Messages.properties :</p>
  
  <pre><code>firstName=firstName
  Email=Email
  Password=password
  secretquestion=secretquestion
  secretanswer=secretanswer
  NotEmpty.person.firstName=firstName is required!
  NotEmpty.person.username=Email is required!
  NotEmpty.person.password=Password is required!
  NotEmpty.person.secretquestion=SecretQuestion is required!
  NotEmpty.person.secretanswer=SecretAnswer is required!
  </code></pre>
  
  <p>Messages_en_US.properties</p>
  
  <pre><code>firstName=firstName
  Email=Email
  Password=password
  secretquestion=secretquestion
  secretanswer=secretanswer
  NotEmpty.person.firstName=firstName is required!
  NotEmpty.person.username=Email is required!
  NotEmpty.person.password=Password is required!
  NotEmpty.person.secretquestion=SecretQuestion is required!
  NotEmpty.person.secretanswer=SecretAnswer is required!
  </code></pre>