title: Spring MVC JSR 303 Validation with Json response validation errors from ValidationMessages.properties
body: <p>Hi I was looking for everywhere but can't find solution.
  I am making an ajax call in Jquery function and submiting data from spring inputs form:input
  to Controller. Controller is validating date by JSR 303 Hibernate Validation and return errors by JSON to the same Jquery function (receiving data by Json) and displaying errors in jsp. 
  Everything work fine but displayed error messages are only dafault or from message parameter from validation annotations. I want to take error messages from ValidationMessages.properties file and i have that file with ready messages but displaying messages are dafault instead of from ValidationMessages.properties.  I am not using form:error tag because i want to display errors received by Json. Problem is that bad messages are displayed not from file but dafault.</p>
  
  <p>I will add that while doing normal JSR 303 Validation (without Ajax and Json) displaying error messages by form:error tag  everything works fine and messages come from ValidationMessages.properties file.</p>
  
  <p>My .jsp page</p>
  
  <pre><code>    &lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
      &lt;%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %&gt;
      &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
      &lt;html&gt;
       &lt;head&gt;
        &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
          &lt;title&gt;Add Users using ajax&lt;/title&gt;
           &lt;script src="&lt;%=request.getContextPath() %&gt;/js/jquery.js"&gt;&lt;/script&gt;
           &lt;script type="text/javascript"&gt;
             var contexPath = "&lt;%=request.getContextPath() %&gt;";
           &lt;/script&gt;
           &lt;script src="&lt;%=request.getContextPath() %&gt;/js/user.js"&gt;&lt;/script&gt;
            &lt;link rel="stylesheet" type="text/css" href="&lt;%=request.getContextPath()%&gt;/style/app.css"&gt;
       &lt;/head&gt;
  
         &lt;body&gt;
            &lt;h1&gt;Add Users using Ajax ........&lt;/h1&gt;
             &lt;form:form method="post"  modelAttribute="user"&gt;
             &lt;table&gt;
                  &lt;tr&gt;&lt;td colspan="2"&gt;&lt;div id="error" class="error"&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt; 
                     &lt;tr&gt;&lt;td&gt;Name:&lt;/td&gt; &lt;td&gt;&lt;form:input path="name" /&gt;&lt;/td&gt; 
                  &lt;tr&gt;&lt;td&gt;Education&lt;/td&gt; &lt;td&gt;&lt;form:input path="education" /&gt;&lt;/td&gt; 
  
                  &lt;tr&gt;&lt;td colspan="2"&gt;&lt;input type="button" value="Add Users" onclick="doAjaxPost()"&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
  
                  &lt;tr&gt;&lt;td colspan="2"&gt;&lt;div id="info" class="success"&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
            &lt;/table&gt;
  
    &lt;/form:form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>My doAjaxPost function in user.js</p>
  
  <pre><code>      function doAjaxPost() {  
    // get the form values  
    var name = $('#name').val();
    var education = $('#education').val();
  
    $.ajax({  
      type: "POST",  
      url: contexPath + "/AddUser.htm",  
      data: "name=" + name + "&amp;education=" + education,  
      success: function(response){
        // we have the response 
        if(response.status == "SUCCESS"){
            userInfo = "&lt;ol&gt;";
            for(i =0 ; i &lt; response.result.length ; i++){
                userInfo += "&lt;br&gt;&lt;li&gt;&lt;b&gt;Name&lt;/b&gt; : " + response.result[i].name + 
                ";&lt;b&gt; Education&lt;/b&gt; : " + response.result[i].education;
            }
            userInfo += "&lt;/ol&gt;";
            $('#info').html("User has been added to the list successfully. " + userInfo);
            $('#name').val('');
            $('#education').val('');
            $('#error').hide('slow');
            $('#info').show('slow');
        }else{
  
            errorInfo = "";
            for(i =0 ; i &lt; response.result.length ; i++){
  
                errorInfo += "&lt;br&gt;" + (i + 1) +". " + response.result[i].defaultMessage;
  
            }
            $('#error').html("Please correct following errors: " + errorInfo);
  
            $('#info').hide('slow');
            $('#error').show('slow');
  
        }       
      },  
      error: function(e){  
        alert('Error: ' + e);  
      }  
    });  
  }  
  </code></pre>
  
  <p>Here is a Controller</p>
  
  <pre><code>  package com.raistudies.controllers;
  
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
  
     import javax.validation.Valid;
     import org.springframework.stereotype.Controller;
     import org.springframework.validation.BindingResult;
     import org.springframework.web.bind.annotation.ModelAttribute;
     import org.springframework.web.bind.annotation.RequestMapping;
     import org.springframework.web.bind.annotation.RequestMethod;
     import org.springframework.web.bind.annotation.ResponseBody;
  
     import com.raistudies.domain.JsonResponse;
     import com.raistudies.domain.User;
  
    @Controller
    public class UserController {
  private List&lt;User&gt; userList = new ArrayList&lt;User&gt;(); 
  
  @RequestMapping(value="/AddUser.htm",method=RequestMethod.GET)
  public String showForm(Map model){
      User user = new User();
      model.put("user", user);
      return "AddUser";
  }
  
  
  @RequestMapping(value="/AddUser.htm",method=RequestMethod.POST)
  public @ResponseBody JsonResponse addUser(@ModelAttribute(value="user") @Valid User user, BindingResult result ){
      JsonResponse res = new JsonResponse();
  
      if(!result.hasErrors()){
          userList.add(user);
          res.setStatus("SUCCESS");
          res.setResult(userList);
  
      }else{
          res.setStatus("FAIL");
          res.setResult(result.getAllErrors());
  
      }
  
      return res;
  }
  
    }
  </code></pre>
  
  <p>User Class</p>
  
  <pre><code> package com.raistudies.domain;
  
   import org.hibernate.validator.constraints.NotEmpty;
  
   import javax.validation.constraints.Size;
  
   public class User {
  
  @NotEmpty
  private String name = null;
  
  @NotEmpty
  @Size(min=6, max=20)
  private String education = null;
  
  
  public String getName() {
      return name;
  }
  public void setName(String name) {
      this.name = name;
  }
  public String getEducation() {
      return education;
  }
  public void setEducation(String education) {
      this.education = education;
  }
  
    }
  </code></pre>
  
  <p>Here is ValidationMessage.properties file</p>
  
  <pre><code>  NotEmpty.user.name=Name of user cant be empty
    NotEmpty.user.education = User education cant be empty
    Size.education=education must hava between 6 and 20 digits
  </code></pre>
  
  <p>Here is app-config.xml</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/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
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"&gt;
  
  &lt;!-- Application Message Bundle --&gt;
     &lt;bean id="messageSource"    class="org.springframework.context.support.ReloadableResourceBundleMessageSource"&gt;
      &lt;property name="basename" value="/WEB-INF/ValidationMessages" /&gt;
  &lt;/bean&gt;
  
  
  &lt;!-- Scans the classpath of this application for @Components to deploy as beans --&gt;
  &lt;context:component-scan base-package="com.raistudies" /&gt;
  
  &lt;!-- Configures the @Controller programming model --&gt;
  &lt;mvc:annotation-driven/&gt;
  
  
  
  &lt;!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory --&gt;
  &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
      &lt;property name="prefix" value="/WEB-INF/jsp/"/&gt;
      &lt;property name="suffix" value=".jsp"/&gt;
  &lt;/bean&gt;
  
  
  
     &lt;/beans&gt;
  </code></pre>
  
  <p>Here is a Screenshot
  <a href="https://www.dropbox.com/s/1xsmupm1cgtv4x7/image.png?v=0mcns" rel="nofollow">https://www.dropbox.com/s/1xsmupm1cgtv4x7/image.png?v=0mcns</a></p>
  
  <p>As You see there are displaying default error messages not from ValidationMessages.properties file. How to solve that? </p>