hibernate - Spring validator - The request sent by the client was syntactically incorrect -
my friend helped me set first spring/hibernate project , trying implement custom validator. have registrationform model uses anotations validate. however, need implement password validator - check if password , confirmpassword equal.
problem: when post, if passwords match, data saved. if dont match, @ validators following line, error title.
errors.rejectvalue("confirmpassword", "valid.passwordconfdiff");
the request sent client syntactically incorrect.
tutorial followed: http://examples.javacodegeeks.com/enterprise-java/spring/mvc/spring-mvc-password-example/
this have:
controller:
@controller @requestmapping("/") //@sessionattributes(value = {"registerform"}) @componentscan("ba.codecentric.movienight") public class frontcontroller { @autowired private userservice userservice; @autowired @qualifier("passwordvalidator") private passwordvalidator validator; @initbinder("password") private void initbinder(webdatabinder binder) { binder.setvalidator(validator); } @requestmapping(method = requestmethod.get) public string initindex(model model){ model.addattribute("registerform", new registerform()); password password = new password(); model.addattribute("password", password); return "index"; } @requestmapping(value="/register", method=requestmethod.post) public string addnewuser(@valid @modelattribute("registerform") registerform registerform, bindingresult result, model model, httpservletrequest request, @validated password password){ if(result.haserrors()){ model.addattribute("error", true); model.addattribute("userisregistering", "<script>$('#loginbox').hide(); $('#signupbox').show();</script>"); system.out.println("error"); return "index"; } user user = new user(); user.setusername(registerform.getusername()); user.setpassword(registerform.getpassword()); userservice.addnewuser(user); return "index"; } }
password validator:
public class passwordvalidator implements validator{ public boolean supports(class<?> paramclass) { return password.class.equals(paramclass); } public void validate(object obj, errors errors) { password password = (password) obj; if (!password.getpassword().equals(password.getconfirmpassword())) { errors.rejectvalue("confirmpassword", "valid.passwordconfdiff"); } } }
registrationform:
public class registerform { @notblank @notempty private string username; @notblank @notempty @size(min=7,max=16,message="password range error") private string password; @notblank @notempty private string confirmpassword; //getters setters }
registration form jsp:
<form:form id="signupform" class="form-horizontal" role="form" commandname="registerform" action="${pagecontext.request.contextpath}/register" method="post"> <form:errors path="*" cssclass="errorblock" element="div" class="alert alert-danger" role="alert" /> <div class="form-group"> <label for="username" class="col-md-3 control-label">user name</label> <div class="col-md-9"> <form:input type="text" path="username" class="form-control" name="username" placeholder="user name" /> </div> </div> <form:errors path="username" element="div" class="alert alert-danger" role="alert" /> <div class="form-group"> <label for="password" class="col-md-3 control-label">password</label> <div class="col-md-9"> <form:password path="password" class="form-control" name="password" placeholder="password"/> </div> </div> <form:errors path="password" element="div" class="alert alert-danger" role="alert" /> <div class="form-group"> <label for="confirmpassword" class="col-md-3 control-label">confirm password</label> <div class="col-md-9"> <form:password path="confirmpassword" class="form-control" name="confirmpassword" placeholder="confirm password"/> </div> </div> <form:errors path="confirmpassword" class="alert alert-danger" role="alert" /> <div class="form-group"> <!-- button --> <div class="col-md-offset-3 col-md-9"> <form:button id="btn-signup" type="submit" class="btn btn-info"><i class="icon-hand-right"></i>   sign up</form:button> </div> </div> </form:form>
i have faced same problem.
@requestmapping(method = requestmethod.post) public string processform( @valid @modelattribute("student") student student, model model, bindingresult result) { if (result.haserrors()) { return "registration"; } return "success"; }
when object student has invalid attribute(s), code gives error: request sent client syntactically incorrect.
however, if order of arguments student , model changed, method works expected. e.g.,
public string processform(model model, @valid @modelattribute("student") student student, bindingresult result)
and
public string processform( @valid @modelattribute("student") student student, bindingresult result, model model)
both work expected.
please, let me know science behind this.
Comments
Post a Comment