ValidateXxx () method
The previous article wrote the Validate () method using action, validate () can only validate all methods of the action, and the ValidateXxx () method is required to implement validation of a specific method in the action. ValidateXxx () verifies only the method named XXX in the action. Where the first letter of XXX should be capitalized.
Source
Useraction.java
Package Org.dny.action;import Org.dny.model.user;import Com.opensymphony.xwork2.actionsupport;import Com.opensymphony.xwork2.ModelDriven; Public classUseraction extends Actionsupport implements modeldriven<user>{PrivateUser user; @Override PublicUserGetmodel() {if( This. user = =NULL) This. user =NewUser ();return This. User; } PublicStringLogin() {//LoginSystem. out. println ("Useraction Login"); System. out. println ("UserName:"+user.getusername ()); System. out. println ("Paswword:"+user.getpassword ());return "Loginsuccess"; } Public void Validatelogin() {System. out. println ("Useraction validatelogin");if(User.getusername () = =NULL|| User.getusername (). Trim (). Equals ("")) This. Addfielderror ("Loginusername","Account cannot be empty");//Add error message to Loginusername property "account cannot be empty" if(User.getpassword () = =NULL|| User.getpassword (). Trim (). Equals ("")) This. Addfielderror ("Loginpassword","Password cannot be empty"); } PublicStringRegister() {//RegistrationSystem. out. println ("Useraction Register"); System. out. println ("UserName:"+user.getusername ()); System. out. println ("Paswword:"+user.getpassword ());return "Registersuccess"; } Public void Validateregister() {System. out. println ("Useraction validateregister");if(User.getusername () = =NULL|| User.getusername (). Trim (). Equals ("")) This. Addfielderror ("Registerusername","Account cannot be empty");//Add error message to Registerusername property "account cannot be empty" if(User.getpassword () = =NULL|| User.getpassword (). Trim (). Equals ("")) This. Addfielderror ("Registerpassword","Password cannot be empty"); } @Override Public void Validate() {System. out. println ("Useraction Validate"); }}
There are two methods of login () and register () in the Useraction, and the Validatelogin () and Validateregister () authentication methods corresponding to the two.
Struts.xml Configuration
<action name =< Span class= "hljs-string" > "user_*" class = " Org.dny.action.UserAction " method=" {1} " > <result name =" loginsuccess " >/ Success.jsp</result > <result
name =
"registersuccess" >/success.jsp</< Span class= "hljs-constant" >result > <
result
name =
" input " >/index.jsp</
Result ></action>
Successful login or successful registration will jump to the success.jsp page, and the verification failure will jump to the index.jsp page.
index.jsp page
<center>Login<br> <!--Login - <form Action="User_login" method="POST"> <table> <tr> <TD>Account:</td> <TD><input type="text" name="UserName"></td> <TD> <!--Remove the value of Loginusername in Fielderrors by El Expression--${fielderrors[' Loginusername '][0]}</td> </tr> <tr> <TD>Password:</td> <TD><input type="password" name="password"></td> <TD> <!--Remove the value of Loginpassword in Fielderrors by El Expression--${fielderrors[' Loginpassword '][0]}</td> </tr> <tr> <TD></td> <TD Align="Left"><input type="Submit" value="Login"> <inputtype=" reset" value="reset"> </tr> </table> </form> </Center> <hr> <center>Error message<br> <!--Remove all error messages in fielderrors with El Expression--Remove all error messages in fielderrors with the El Expression:<br>${fielderrors}<br> <br> <!--Remove all error messages from Fielderror via the Struts tab--Remove all error messages from the Fielderror via the Struts tab:<s:fielderror /> </Center> <hr> <center>Registered<br> <!--registration -- <form Action="User_register" method="POST"> <table> <tr> <TD>Account:</td> <TD><input type="text" name="UserName"></td> <TD> <!--Remove the value of Registerusername in Fielderrors by El Expression--${fielderrors[' Registerusername '][0]}</td> </tr> <tr> <TD>Password:</td> <TD><input type="password" name="password"></td> <TD> <!--Remove the value of Registerpassword in Fielderrors by El Expression--${fielderrors[' Registerpassword '][0]}</td> </tr> <tr> <TD></td> <TD Align="Left"><input type="Submit" value="register"> <inputtype=" reset" value="reset"> </tr> </table> </form> </Center> <hr> <center> <s:debug /> </Center>
Operating Results and
Verification process
1. The type converter performs a type conversion on the request parameter and assigns the converted value to the property in the action.
2. If an exception occurs during the execution of a type conversion, the system will save the exception information to the Actioncontext,conversionerror interceptor to add the exception information to the fielderrors. Regardless of whether the type conversion has an exception, the 3rd step will be entered.
3, the system through the reflection technology first call action in the ValidateXxx () method, XXX is the method name.
4. Call the Validate () method in action again.
5, through the above 4 steps, if the system in the Fielderrors error message (that is, the collection of error information is larger than 0), the system automatically forwards the request to a view named input. If the fielderrors in the system does not have any error messages, the system performs the processing in the action.
Reference Links:
http://blog.csdn.net/houpengfei111/article/details/9038233
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
STRUTS2 Study Notes (7) method of the VALIDATEXXX ()---data validation