Adding custom error messages for validation in Spring-MVC -


i using spring-mvc , validation tests in backend. able perform tests, redirect link. unable add custom error messages incase there problem, example : "firstname empty". tried using validationmessages_en_en.properties file in web-inf, didn't help. posting code, kindly let me know going wrong :

controller :

@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:/";     } 

entity :

@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; 

servlet-context.xml

<beans:bean id="messagesource" class="org.springframework.context.support.reloadableresourcebundlemessagesource">         <beans:property name="basename" value="validatormessages_en_en.properties"/>            </beans:bean> 

jsp/html :

      <c:url var="addaction" value="/person/add" ></c:url> <form:form action="${addaction}" commandname="person" method="post"> <table>     <c:if test="${!empty person.username}">     <tr>         <td>             <form:label path="id">                 <spring:message text="id"/>             </form:label>         </td>         <td>             <form:input path="id" readonly="true" size="8"  disabled="true" />             <form:hidden path="id" />         </td>      </tr>     </c:if>      <tr>         <td>             <form:label path="firstname">                 <spring:message text="firstname"/>             </form:label>         </td>         <td>             <form:input path="firstname" />         </td>         <td><form:errors path="firstname"/>     </tr>     <tr>         <td>             <form:label path="username">                 <spring:message text="email"/>             </form:label>         </td>         <td>             <form:input path="username" />         </td>         <td><form:errors path="username"/>     </tr>     <tr>         <td>             <form:label path="password">                 <spring:message text="password"/>             </form:label>         </td>         <td>             <form:input path="password" />         </td>         <td><form:errors path="password"/>     </tr>      <tr>     <td>         <form:label path="secretquestion">             <spring:message text="secretquestion"/>         </form:label>     </td>     <td>         <form:input path="secretquestion" />     </td>         <td><form:errors path="secretquestion"/>     </tr>       <tr>         <td>             <form:label path="secretanswer">                 <spring:message text="secretanswer"/>             </form:label>         </td>         <td>             <form:input path="secretanswer" />         </td>         <td><form:errors path="secretanswer"/>     </tr> 

pom.xml :

 <!-- validation -->         <dependency>             <groupid>javax.validation</groupid>             <artifactid>validation-api</artifactid>             <version>1.0.0.ga</version>         </dependency>          <dependency>             <groupid>org.hibernate</groupid>             <artifactid>hibernate-validator</artifactid>             <version>4.3.1.final</version>         </dependency> 

messages.properties :

firstname=firstname email=email password=password secretquestion=secretquestion secretanswer=secretanswer notempty.person.firstname=firstname required! notempty.person.username=email required! notempty.person.password=password required! notempty.person.secretquestion=secretquestion required! notempty.person.secretanswer=secretanswer required! 

messages_en_us.properties

firstname=firstname email=email password=password secretquestion=secretquestion secretanswer=secretanswer notempty.person.firstname=firstname required! notempty.person.username=email required! notempty.person.password=password required! notempty.person.secretquestion=secretquestion required! notempty.person.secretanswer=secretanswer required! 

<spring:message text="firstname"/> should <spring:message code="firstname"/> try replace text code in <spring:message /> tag

and change basename value validatormessages , en_us spring append

 <beans:bean id="messagesource" class="org.springframework.context.support.reloadableresourcebundlemessagesource">          <beans:property name="basename" value="validatormessages"/>  </beans:bean> 

sava file validatormessages_en_us.properties in resource folder


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -