java - Spring validation with Javax.validation redirects to 400 -


i working on spring-mvc application, , want validate data. able validate data no problems. thing if data invalid, go jsp page, not happening right now. instead apache 400 error, request sent syntactically incorrect. can tell me remaining implement in validation.

controller :

@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,@modelattribute("person") person p,model model,bindingresult bindingresult){          if(bindingresult.haserrors()){             return "redirect:/";         }             this.personservice.addperson(p);             return "redirect:/";      } 

entity :

@entity @table(name="person") public class person implements userdetails{      @notempty @email     @column(name = "username")     private string username;      @notempty(message = "please enter password")     @column(name = "password")     private string password;      @size(min = 2,max = 30)     @column(name = "firstname")     private string firstname;      @size(min = 2,max = 50)     @column(name = "secretquestion")     private string secretquestion;       @size(min = 2,max = 500)     @column(name = "secretanswer")     private string secretanswer; } 

jsp :

<tr>     <td>         <form:label path="firstname">             <spring:message text="firstname"/>         </form:label>     </td>     <td>         <form:input path="firstname" />     </td>     <td><form:errors path="firstname"/>please enter firstname properly</td> </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"/>please enter email properly</td> </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"/>please enter password properly</td> </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"/>please enter secretquestion properly</td> </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"/>please enter secretanswer properly</td> </tr> 

servlet-context.xml

 <beans:bean id="messagesource" class="org.springframework.context.support.reloadableresourcebundlemessagesource"/> 

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> 

any pointers nice. avoid going on apache 400, display went wrong input field.

this might because of public string addperson(@valid person person,@modelattribute("person") person p,model model,bindingresult bindingresult) signature .
bindingresult must follow @modelattribute method signature might have more 1 model object , spring create separate bindingresult instance each of them.that why when data invalid spring not able bind errors bindingresult , throws 400 error.

try changing method signature public string addperson(@valid person person,@modelattribute("person") person p,bindingresult bindingresult,model model).

read more on bindingresult.


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 -