java - How does Spring MVC resolve and validate handler method parameters? -


i pretty new in spring mvc , have imported tutorial project related server side validation , have doubt how works.

so have login page named login.jsp contain login form:

<form:form action="${pagecontext.request.contextpath}/login" commandname="user" method="post">      <table>          <tr>             <td><label>enter username : </label></td>             <td><form:input type="text" path="username" name="username" />                 <br> <form:errors path="username" style="color:red;"></form:errors>             </td>         </tr>          <tr>             <td><label>enter password : </label></td>             <td><form:input type="password" path="password" name="password" />                 <br> <form:errors path="password" style="color:red;"></form:errors>             </td>         </tr>          <tr>             <td>&nbsp</td>             <td align="center"><input type="submit" value="login" /></td>         </tr>      </table>  </form:form> 

that think use object specied commandname="user" attribute retrieved model (corect me if doing wrong assertion) store username , password inserted user.

this commandname="user" instance of user class:

import javax.validation.constraints.size; import org.hibernate.validator.constraints.notblank;  public class user {      @notblank(message="username can not blank")     private string username;      @size(min=6,message="password must atleast 6 characters long")     private string password;      private string gender;     private string vehicle;     private string country;     private string image;      ...............................................     ...............................................     getter , setter methods     ...............................................     ............................................... } 

so can see there @notblank , @size validation annotation declared on username , password fields.

and here first doubt: difference 2 used library javax.validation , org.hibernate.validator ?

why in tutorial use both? can same thing using hibernate validator library? (i think can specify valid lenght of string using hibernate validator, or not)?

so when login form submitted generate , httprequest toward /login resource handled method declared controller class:

@requestmapping(value="/login" , method=requestmethod.post) public string do_login(httpservletrequest req , model md , httpsession session , @valid user user, bindingresult br) {     try     {         //system.out.println(br.getallerrors().size());          string username = req.getparameter("username");         string password = req.getparameter("password");          system.out.println("username , pasword : "+username +"  "+ password);         if(br.getallerrors().size() > 0){             system.out.println("server side validation takes place....");         }         else{         login_model lm = new login_model();         string message = lm.do_login_process(username, password);          if(message.equals("login success"))         {             session.setattribute("username", username);             return "redirect:/myprofile";         }         else         {             md.addattribute("error_msg", message);         }         }         return "login";     }     catch(exception e)     {         return "login";     } } 

ok, , have following doubts method:

1) take object input parameter: @valid user user. pass it? think depend fact in form specified commandname="user" spring automatically it. correct?

2) have understand @valid annotation automatically call validation process. how happen? related aop feature provided spring? or what? why @valid annotation related javax.validation library , not hibernate validator (so @valid annotation validate field annotated hibernate validator annotation? why?)

3) have understand if user insert wrong values login form can obtain error bindingresult br input parameter. using debugger can see object contain error message defined annotation defined user model object. how works?

tnx

and here first doubt: difference between 2 used libraries javax.validation , org.hibernate.validator?

javax.validation comes jsr-303 api. can @ api classes instance in this maven dependency.

hibernate validator 1 of implementations of jsr 303 (reference implementation actually), implements of api, adds own extensions, instance @notblank annotation mention. other implementation of jsr 303 instance apache bval.


it takes object input parameter: @valid user user. passes handler method?

values handler methods in spring mvc provided implementations of interface handlermethodargumentresolver. in case implementation called resolve user parameter servletmodelattributemethodprocessor. can view source code , javadocs of these classes see how work internally.


the @valid annotation automatically call validation process. how happend? use aop? why @valid annotation related javax.validation library , not hibernate validator (so @valid annotation validate field annotated hibernate validator annotation?)

the validation process invoked modelattributemethodprocessor, mentioned class servletmodelattributemethodprocessor inherits from. contains following method:

protected void validateifapplicable(webdatabinder binder, methodparameter parameter) {     annotation[] annotations = parameter.getparameterannotations();     (annotation ann : annotations) {         if (ann.annotationtype().getsimplename().startswith("valid")) {             object hints = annotationutils.getvalue(ann);             binder.validate(hints instanceof object[] ? (object[]) hints : new object[] {hints});             break;         }     } } 

if closely, see condition following expression:

ann.annotationtype().getsimplename().startswith("valid") 

this means spring invoke validation if parameter has any annotation starts valid. might jsr 303's @valid or spring's @validated supports validation groups. custom annotation, long name starts valid.


from have understand if user inserts wrong values login form can obtain error bindingresult handler parameter. using debugger can see object contain error message defined annotation defined user model object. how works?

let's go modelattributemethodprocessor class. there following code in resolveargument method:

webdatabinder binder = binderfactory.createbinder(webrequest, attribute, name); 

this creates instance of webdatabinder on validation invoked in mentioned validateifapplicable method. validation populates bindingresult provided controller handler method via errorsmethodargumentresolver class, once again implements handlermethodargumentresolver.


tldr: lot of thing ask in question can traced various implementation of handlermethodargumentresolver. suggest go through these classes , step through them using debugger gain better understanding of them.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -