About STRUTS2 (iii)-Authenticator

Source: Internet
Author: User

a good validation framework must be considered: Verify the reusability of the feature. Verify the scalability of the feature. separation of validation and business logic STRUTS2 provides a powerful verification framework: under the Xwork-core-2.3.24.jar package, under the \com\opensymphony\xwork2\validator\validators path, look for a name called " Default.xml "XML file

First, Struts2 pre-defined validator

<validators> <validator name= "required" class= " Com.opensymphony.xwork2.validator.validators.RequiredFieldValidator "/> <validator name=" requiredstring " class= "Com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/> <validator name= "int" class= "Com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/> <validator name= "Long" class= " Com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator "/> <validator name=" short "class=" Com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator "/> <validator name=" Double "class=" Com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator "/> <validator name=" date "class=" Com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator "/> <validator name=" expression "class= "Com.opensymphony.xwork2.validator.validators.ExpressionValidator"/> <validator name= "Fieldexpression" Class= "Com.opensymphony.xwork2.valIdator.validators.FieldExpressionValidator "/> <validator name=" email "class=" Com.opensymphony.xwork2.validator.validators.EmailValidator "/> <validator name=" url "class=" Com.opensymphony.xwork2.validator.validators.URLValidator "/> <validator name=" Visitor "class=" Com.opensymphony.xwork2.validator.validators.VisitorFieldValidator "/> <validator name=" Conversion "class=" Com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator "/> <validator name=" Stringlength "class=" Com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator "/> <validator Name= "regex" class= "Com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/> <validator name= " Conditionalvisitor "class=" Com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator "/> </validators>

when you need to verify certain fields, the personal information submitted by the user can be created under the same package as the xxxaction that need verification. Actionclassname-validation.xml, all the methods in this action are validated, and if you only want to work with a method in the action, the validation file is named Actionclassname-actionname-validation.xml, it is important to note that ActionName is the name of the action in Struts.xml <action name= "" >. There is a login method in Useraction, Struts.xml <action name= "User_login", then verify the file name is:

Useraction-user_login-validation.xml

Several commonly used validators, parsing

<!--required check-in <!--non-field checksum-<validator type= "required" > <param name= "fidlename" >f Ield</param> <message> Please enter data </message> </validator> <!--field Check- <field name= "field" > <field-validator type= "required" > <message&gt Please enter data </message> </field-validator> </field> <!--Required String checksum-<!--non-field check --<validator type= "requiredstring" > <param name= "fidlename" >field</param> <p      Aram Name= "Trim" >true</param> <message> Please enter data </message> </validator>             <!--field Checksum-<field name= "field" > <field-validator type= "requiredstring" > <param name= "Trim" >true</param> <message> Please enter data </message> </field- Validator> </field> <!--integer checksum <!--non-field checksum-<validator type= "int" > <param name= "fidle Name ">field</param> <param name=" min ">1</param> <param name=" Max " >80</param> <message> numbers must be between ${min}-${max} years old </message> </validator> <!--field check --<field name= "field" > <field-validator type= "int" > <param name= "min" >1< /param> <param name= "Max" >80</param> <message> number must be between ${min}-${max} years old </messa Ge> </field-validator> </field> <!--floating-point checksum <!--non-field check-in <vali Dator type= "Double" > <param name= "fidlename" >field</param> <param name= "MinExclusive" ;0.1</param> <param name= "maxexclusive" >10.1</param> <message> input floating point Invalid &LT ;/message> </validator> <!--field Checksum-<field name= "field" > <field-validator type= "Double" >                        <param name= "minexclusive" >0.1</param> <param name= "maxexclusive" >10.1</param> <message> input floating point invalid </message> </field-validator> </field> < !--Date Check-<!--non-field checksum-<validator type= "date" > <param name= "fidlename" >field< /param> <param name= "min" >2009-01-01</param> <param name= "Max" &GT;2019-01-01&LT;/PARAM&G         T         <message> Invalid date </message> </validator> <!--field Checksum-<field name= "field" > <field-validator type= "Date" > <param name= "min" >2009-01-01</param> <param name= "Max" >2019-01-01</param> <message> Invalid date </message> </field-validator> <   /field>    <!--expression Checksum <!--non-field checksum-<validator type= "expression" > <param name= "Expressi         On ">password==repassword</param> <message> inconsistent input </message> </validator> <!--field expression checksum <!--non-field checksum-<validator type= "fieldexpression" > <param name= "exp Ression ">password==repassword</param> <message> inconsistent input </message> </validator> & lt;!  --field Check-<field name= "field" > <field-validator type= "fieldexpression" > <param Name= "Expression" ><! [cdata[#password = = #repassword]]></param> <message> input inconsistency </message> </field-va         Lidator> </field> <!--message Check-in <!--non-field check-<validator type= "Email" > <param name= "Fidlename" >field</param> <message> illegal email address </message>     </validator> <!--field Checksum-<field name= "field" > <field-validator type= "Email"             ;     <message> Illegal Email address </message> </field-validator> </field> <!--URL Checksum                      <!--non-field checksum-<validator type= "url" > <param name= "fidlename" >field</param> <message> Invalid URL </message> </validator> <!--field Checksum-<field name= "field" &G         T <field-validator type= "url" > <message> invalid URL </message> </field-validat         Or> </field> <!--visitor Checksum <!--non-field checksum-<validator type= "Visitor" >         <param name= "Fidlename" >field</param> <param name= "context" >fieldContext</param> <param name= "Appendprefix" >true</param> <message> input checksum </message> &Lt;/validator> <!--field Checksum-<field name= "field" > <field-validator type= "Visitor" > <param name= "Context" >fieldContext</param> <param name= "Appendprefix" >t rue</param> <message> Input checksum </message> </field-validator> </f ield> <!--type conversion checksum <!--non-field checksum-<validator type= "Conversion" > <param Name= "Fidlename" >field</param> <message> type conversion error </message> </validator> <!--word Segment Checksum-<field name= "field" > <field-validator type= "Conversion" > <message> type Turn Change Error </message> </field-validator> </field> <!--string length checksum <!--non-field check- -<validator type= "Stringlength" > <param name= "fidlename" >field</param> <para M name= "MinLength" >1</param> <param name= "maxLength" >10</param> <param name= "Trim" >true</param> <message> string length must be 10-bit </message> </validator> <!--field Check-<f Ield name= "field" > <field-validator type= "stringlength" > <param name= "minLength" >1</ param> <param name= "maxLength" >10</param> <param name= "Trim" &GT;TRUE&LT;/PARAM&G                          T <message> string length must be 10-bit </message> </field-validator> </field> <!--regular-expression check--&     Gt                          <!--non-field checksum-<validator type= "regex" > <param name= "fidlename" >field</param> <param name= "Regexexpression" ><! [Cdata[(/^13[13567890] (\d{8}) $/)]]></param> <message> mobile phone number must be a number and is 11 bits </message> </valid ator> <!--field Checksum-<field name="Field" > <field-validator type= "regex" > <param name= "regexexpression" ><! [Cdata[(/^13[13567890] (\d{8}) $/)]]></param> <message> mobile phone number must be a number and is 11 bits </message> &L  T;/field-validator> </field>

Validation time for validator:

Validation occurs before the Execute method, the params interceptor in STRUTS2 has set the requested parameter through reflection to the Action's properties, so the validation framework actually validates the value in the value stack

Results of the validation:

If the user enters a parameter that fully satisfies the validation result, the Execute method continues. If it is not satisfied, it jumps to the result in the action configuration in page name= "input"





Ii.. Custom Validator

Verify that the input string contains Chinese:

Ideas:

1. Get the length of Chinese and English

2. Such as "haha" and "AA" length are all 2, so I can't judge,

3. A Chinese is two characters, while English still occupies one byte

4. So to determine whether to include Chinese is only to determine the length of the byte and the length of the character is consistent.

Import Com.opensymphony.xwork2.validator.validationexception;import Com.opensymphony.xwork2.validator.validators.fieldvalidatorsupport;public class Chinesevalidator  extends fieldvalidatorsupport{public void Validate (Object object) throws Validationexception {//Get field name final String FieldName = This.getfieldname ();//Gets the field value of final String Fieldvalue = (String) this.getfieldvalue (FieldName, object);//bytes Final int bytes = Fieldvalue.getbytes (). length;//number of characters final int chars = Fieldvalue.length (), if (Mode.equals ("some")) {if (chars== bytes| | Chars*2==bytes) {This.addfielderror (FieldName, object);}}}
declaring a custom validator

Create a validators.xml under SRC

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE validators public        "-//opensymphony group//xwork Validator Config 1.0//en"        "/HTTP/ Www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd "><validators>    <validator name=" Chinese "class=" Xx.xxx.ChineseValidator "/></validators>
PS:STRUTS2 2.0. Before the 8 version, once the custom validators.xml file is used, the system will no longer invoke the Struts2 predefined validator, so the struts2 default content needs to be copied to Custom Validators.xml.

Referencing a custom validator

In the Useraction-user_login.validation.xml

<validators><field name= "username" ><field-validator type= "Chinese" ><message> user account, You can only enter characters that are not Chinese </message></field-validator></field></validators>
Display validation error messages on the page

Field Validator: <s:filederror>, without specifying filedname, displays all field validation errors by default

Action Validator: <s:actionerror>, Ibid.

Verifier Short Circuit:

When a field has more than one validation condition, the validation is terminated immediately after the first validation error, and subsequent validation of the field is no longer performed, which is called the validator short-circuit.

Add short-circuit= "True" when specifying the authenticator

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE validators public        "-//opensymphony group//xwork Validator 1.0.2//en"        "http://www.opensymphony.com/ Xwork/xwork-validator-1.0.2.dtd ">  <validators><field name=" user.age "><field-validator type = "int" short-circuit= "true" ><param name= "min" >15</param><message> age greater than or equal to 15 years </message> </field-validator></field></validators>

Note: only the Validator1.0.2 version can properly perform the validator short circuit.


About STRUTS2 (iii)-Authenticator

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.