Thursday, 15 May 2014

Spring MVC Form Validation - The request sent by the client was syntactically incorrect -



Spring MVC Form Validation - The request sent by the client was syntactically incorrect -

i trying add together form validations working application. started adding notnull check login form. using hibernate impl of bean validation api.

here's code have written

@controller @requestmapping(value="/login") @scope("request") public class logincontroller { @autowired private commonservice commonservice; @autowired private siteuser siteuser; @initbinder private void datebinder(webdatabinder binder) { simpledateformat dateformat = new simpledateformat("dd-mm-yyyy"); customdateeditor editor = new customdateeditor(dateformat, true); binder.registercustomeditor(date.class, editor); } @modelattribute protected modelmap setupform(modelmap modelmap) { modelmap.addattribute("siteuser", siteuser); homecoming modelmap; } @requestmapping(value="/form", method = requestmethod.get) public modelandview form(modelmap map){ if (siteuser.getid() == null){ map.addattribute("command",new siteuser()); homecoming new modelandview("login-form",map); }else { homecoming new modelandview("redirect:/my-dashboard/"+siteuser.getid()); } } @requestmapping(value="/submit", method=requestmethod.post) public modelandview submit(@valid siteuser user, modelmap map, bindingresult result){ if (result.haserrors()) { map.addattribute("command", user); system.out.println("login error block"); homecoming new modelandview("login/form",map); } else { user loggedinuser = commonservice.login(user.getemail(), user.getpassword()); if (loggedinuser != null) { siteuser.setid(loggedinuser.getid()); siteuser.setname(loggedinuser.getname()); system.out.println("site user attr set"); } homecoming new modelandview("redirect:/my-dashboard/"+loggedinuser.getid()); } } }

the model is

@component @scope("session") public class siteuser { private integer id = null; @notnull private string name = null; private string email = null; private string password = null; private list<string> displayprivlist = null; private list<string> functionprivlist = null; // , getters , setters }

the jsp is

<c:url var="loginsubmiturl" value="/login/submit"/> <form:form method="post" action="${loginsubmiturl}"> <form:errors path="*" /> <div class="row"> <div class="span4"> </div> <div class="span4"> <h3>please login</h3> <label><span style="color:red">*</span>email</label><form:input path="email" type="text" class="input-medium" /> <label><span style="color:red">*</span>password</label><form:input path="password" type="password" class="input-medium" /> <br/> <button type="submit" class="btn btn-primary">login</button> <button type="button" class="btn">cancel</button> </div> </div> </form:form>

i have added messages.properties , annotation driven bean def in context xml. other answers on subject talk form fields not getting posted. in case, that's expected behavior - if submit blank form, should error.

please advise missing?

i think question had same issue yours

syntactically wrong request sent upon submitting form invalid info in spring mvc (which uses hibernate validator)

which points out

you have modify order of arguments. set bindingresult result parameter straight after parameter @value annotation

validation spring-mvc tomcat7 hibernate-validator

No comments:

Post a Comment