hibernate - spring form validation : Unable to print errors in jsp -


i working on spring mvc application, here have form user can change password, here validating form using spring form validation implements "org.springframework.validation.validator" class.

please @ code snippets.  <%@ taglib uri="http://www.springframework.strong textorg/tags/form" prefix="spring"%>         <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>         <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>          <%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%>         <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">         <html>         <head>         <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">         <title> change password </title>         </head>         <body>         <%@include file="index.jsp" %>         <div class="row">             <div class="container" style=" background-color: #f9ffed; ">                 <div>                     <h4><label>change password:</label></h4>                  </div>                  <div class="col-md-12" >                 <spring:form commandname="changepassword" action="changepassword.do" method="post">                 <table>                     <tr>                         <td><label for="current_pwd"  >current password</label></td>                         <td><spring:input path="current_pwd" type="text" class="form-control"  placeholder="name"/></td>                         <td><spring:errors path="current_pwd" type="text" cssstyle="color: red;"></spring:errors></td>                     </tr>                     <tr>                         <td>&nbsp</td>                     </tr>                     <tr>                         <td><label for="newpassword"  >new password</label></td>                         <td><spring:input path="newpassword" type="text" class="form-control"  placeholder="password"/></td>                         <td><spring:errors path="newpassword" type="text" cssstyle="color: red;"></spring:errors></td>                     </tr>                     <tr>                         <td>&nbsp</td>                     </tr>                     <tr>                         <td><label for="confirmpassword"  >confirm password</label></td>                         <td><spring:input path="confirmpassword" type="text" class="form-control"  placeholder="password"/></td>                         <td><spring:errors path="confirmpassword" type="text" cssstyle="color: red;"></spring:errors></td>                     </tr>                     <tr>                         <td>&nbsp</td>                     </tr>                     <tr>                         <td>                             <button type="submit" class="btn btn-success"> save </button>                              <a href="listusers.do" class="btn btn-success"> cancel </a>                         </td>                       </tr>                  </table>                  </spring:form>                 </div>             </div>         </div>         </body>         </html> 
  1. controller , post methods

    @requestmapping(value="/changepassword",method=requestmethod.get) public string changepassword(changepassword chpaswd,bindingresult result,modelmap model){ chpaswd=new changepassword(); model.addattribute("changepassword",chpaswd); return "changepassword"; }

        @requestmapping(value="/changepassword",method=requestmethod.post)     public string changepasswordpost(changepassword chpwd,bindingresult result,modelmap model,httpsession session){         string message="";          changepwdvalidator.validate(chpwd, result);          chpwd=new changepassword();          if(result.hasfielderrors()){             system.out.println("has errors");             model.addattribute("changepassword",chpwd);             return "changepassword";         }else{              system.out.println("chnage pwd values :"+chpwd.getnewpassword()+","+"current pwd:"+chpwd.getcurrent_pwd());             try{                // other operations                  model.addattribute("changepassword",chpwd);                 }catch(exception e){                     message="failed process request, please re-verify values!";                     model.addattribute("message", message);                    }             return "changepassword";         }     } 
  2. validator class

    import org.springframework.validation.errors; import org.springframework.validation.validationutils; import org.springframework.validation.validator;

    import com.knot.pirautomation.models.changepassword;  public class changepasswordvalidator implements validator{      changepassword chngepwd;      public boolean supports(class clazz) {          return changepassword.class.equals(clazz);     }      public void validate(object target, errors errors) {         if(target instanceof changepassword){             chngepwd=(changepassword) target;              system.out.println("----------");             system.out.println("old pwd:"+chngepwd.getcurrent_pwd());             system.out.println("new pwd:"+chngepwd.getnewpassword());             system.out.println("confirm pwd:"+chngepwd.getconfirmpassword());             system.out.println("----------");              validationutils.rejectifemptyorwhitespace(errors, "newpassword", "newpassword.required");             validationutils.rejectifemptyorwhitespace(errors, "confirmpassword", "confirmpassword.required");             validationutils.rejectifemptyorwhitespace(errors, "current_pwd", "oldpassword.required");              if( !(chngepwd.getnewpassword().equals(chngepwd.getconfirmpassword()))){                 errors.rejectvalue("newpassword", "newpassword.match");             }             if((chngepwd.getnewpassword().length()<8)){                 errors.rejectvalue("newpassword", "newpasswordlength.match" );             }             if((chngepwd.getconfirmpassword().length()<8)){                 errors.rejectvalue("confirmpassword", "confirmpasswordlength.match" );             }              string blacklistchars = "!'=();<> \"";             char blacklistarr[] = blacklistchars.tochararray();             for(int i=0;i<blacklistarr.length;i++) {                 if(chngepwd.getnewpassword().contains("" + blacklistarr[i])) {                     errors.rejectvalue("newpassword","newpassword.invalidchars");                      break;                 }                        }              for(int i=0;i<blacklistarr.length;i++) {                 if(chngepwd.getconfirmpassword().contains("" + blacklistarr[i])) {                     errors.rejectvalue("confirmpassword","confirmpassword.invalidchars");                      break;                 }                        }          }      }  } 
  3. error.properties file

    oldpassword.required= old password required newpassword.required= new password required confirmpassword.required= confirm password should required newpassword.match= confirmation passwords should match newpasswordlength.match= new password should @ least 8 characters confirmpasswordlength.match= confirm password should @ least 8 characters newpassword.invalidchars=new password has special characters not allowed confirmpassword.invalidchars= confirm password has special characters not allowed

you can see when form has errors, returning control same jsp. unable trace bug/error springform validation working fine,but when trying display errors defined in properties file.

please 1 me on issue.

use this...you should not explicitly invoking validator.

@valid @modelattribute("forname") formname formname, bindingresult bindingresult, model model, redirectattributes redirectattributes, httpsession session


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 -