STRUTS2 Manual Verification

Source: Internet
Author: User

* First to get the value of the corresponding tag Name property from the page, declare the property with the same name in the action class action, provide the get and set methods

* to inherit the Actionsupport class or implement the Validateable interface
    
* Override the Validate () method of the Validateable interface
* The prerequisite is to ensure that the setusername (), validate (), login () methods are executed in this order

* If login fails, how to handle:
* THIS.ADDFIELDERROR (key, value);
* Key: Error prompt field
* Value: Error message

* When is verification passed?
* Verification by: 1, Map collection does not exist; 2, map set exists and is empty
* Validation does not pass: The Map collection exists and is not empty

* Analysis Requirements:
* User name cannot be null, ""
* The password cannot be null, "", and the length of the password is between 6-12

* Validate for all business methods or for a specific business method?
* Overridden Validate () method to validate against all business methods
*The overridden Validate () method, plus the specified business method name to validate (the first letter of the business method name), is implemented to validate against a specified business method
* Why do you want to do this stitching? Because the struts2 frame is spliced at the bottom, if you do not write it, the bottom will not find the corresponding method name

1 index.jsp2<s:fielderror></s:fielderror>3<s:form name="LoginForm" namespace="/validate"action="validateaction_login.action"Method="Post">4User name: <s:textfield name="username"/><br/>5Password: <s:password name="pwd"/><br/>6<s:submit value="Login"/>7</s:form>8<a href="${pagecontext.request.contextpath}/validate/validateaction_test.action">test</a>
1@SuppressWarnings ("Serial")2  Public classValidateactionextendsActionsupport {3     PrivateString username;4     PrivateString pwd;5      PublicString GetUserName () {6         returnusername;7     }8      Public voidSetusername (String username) {9          This. Username =username;Ten     } One      PublicString getpwd () { A         returnpwd; -     } -      Public voidsetpwd (String pwd) { the          This. PWD =pwd; -     } -     //Verify the Login service only, do not validate test, and if all is verified, write the method name validate () -      Public voidValidatelogin () { +         if(username==NULL|| Username.equals ("")) -         { +              This. Addfielderror ("Error", "User name cannot be empty");//key: Error flag; Value: Error prompt A         } at         if(pwd==NULL|| Pwd.equals ("")) -         { -              This. Addfielderror ("error", "Password cannot be empty"); -}Else { -String pattern= "^[0-9a-za-z]{6,12}$"; -Pattern p=Pattern.compile (Pattern); inMatcher m=P.matcher (PWD); -             Booleanb=m.matches (); to             if(!b) { +                  This. Addfielderror ("Error", "Bad password format"); -             } the         } *     } $     Panax Notoginseng      PublicString Login () { -System.err.println ("1234567890-"); the         return"Success"; +     } A      PublicString Test () { theSystem.err.println ("987654321"); +         return"Success"; -     } $}

STRUTS2 Manual Verification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.