"Java EE learning 36th Day" "Struts2" "Struts2 system Verification"

Source: Internet
Author: User
Tags cdata

First, STRUTS2 system verification

1. The way in which the STRUTS2 system is validated is essentially a way of configuring an XML file to achieve the purpose of validation.

2. In fact, the method of system check and Manual Check method at the bottom of the basic implementation is the same. However, the implementation process is encapsulated when the system check is used, which is more convenient to use.

3. Implementation process

(1) Define the relevant property in the action file, and the property name and the Name property value in the form are the same.

(2) Create a new XML file in the action directory with the following file naming rules

* If it is for the entire action file, the file name is: Actionclassname-validation.xml

* If it is for a method in action, the file name is: Actionclassname-actionname-validation.xml (ActionName is in Struts.xml file)

(3) Writing configuration file

(4) Do not need to register in the Struts.xml file.

4. Writing the configuration file

(1) First, the DTD file specification is: Xwork-core-2.3.24.jar package under the Xwork-validator-x.x.x.dtd file, the file into the configuration file, if there is no relevant hint, then the method reference:

http://kuangdaoyizhimei.blog.163.com/blog/static/22055721120158582251764/

(2) Follow the prompts step by step to write, but note there is a field-validator tag, the label has a Type property, the value of the property should refer to

Related definitions in the Com.opensymphony.xwork2.validator.validators.default.xml file

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <!DOCTYPE validators Public3 "-//apache struts//xwork Validator Definition 1.0//en"4 "Http://struts.apache.org/dtds/xwork-validator-definition-1.0.dtd">5 6 <!--START Snippet:validators-default -7 <validators>8     <Validatorname= "Required"class= "Com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/>9     <Validatorname= "Requiredstring"class= "Com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/>Ten     <Validatorname= "int"class= "Com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/> One     <Validatorname= "Long"class= "Com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator"/> A     <Validatorname= "Short"class= "Com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator"/> -     <Validatorname= "Double"class= "Com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/> -     <Validatorname= "Date"class= "Com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/> the     <Validatorname= "expression"class= "Com.opensymphony.xwork2.validator.validators.ExpressionValidator"/> -     <Validatorname= "Fieldexpression"class= "Com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/> -     <Validatorname= "Email"class= "Com.opensymphony.xwork2.validator.validators.EmailValidator"/> -     <Validatorname= "url"class= "Com.opensymphony.xwork2.validator.validators.URLValidator"/> +     <Validatorname= "Visitor"class= "Com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/> -     <Validatorname= "Conversion"class= "Com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/> +     <Validatorname= "Stringlength"class= "Com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/> A     <Validatorname= "Regex"class= "Com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/> at     <Validatorname= "Conditionalvisitor"class= "Com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator"/> - </validators> - <!--END Snippet:validators-default -
Default.xml

(3) The parameters in the Param tag under field-validator refer to the class source files in the Com.opensymphony.xwork2.validator.validators.default.xml file, and the source code can be viewed to obtain information about the parameters to be passed, such as Parameter names, type of arguments, and so on.

5. Re-write the manually validated case using system validation, listing only the configuration files:

* File name: Validateaction-validateaction-validation.xml

* File Contents:

<?XML version= "1.0" encoding= "UTF-8"?> <!DOCTYPE validators Public "-//apache struts//xwork Validator 1.0.3//en" "Http://struts.apache.org/dt Ds/xwork-validator-1.0.3.dtd "><validators>    <Fieldname= "username">        <Field-validatortype= "Requiredstring">            <paramname= "Trim">True</param>            <message><! [Cdata[the user name cannot be empty! ]]></message>        </Field-validator>    </Field>    <Fieldname= "Password">        <Field-validatortype= "Requiredstring">            <message><! [Cdata[The password cannot be empty]]></message>        </Field-validator>        <Field-validatortype= "Regex">            <paramname= "Trim">True</param>            <paramname= "Regex"><! [Cdata[^[0-9a-za-z]{6,10}$]]></param>            <message><! [Cdata[password is a combination of 6-10-bit letters and numbers! ]]></message>        </Field-validator>    </Field></validators>

6. Search order for two profiles: Actionclassname-validation.xml,actionclassname-actionname-validation.xml, the system will continue to search for the next checksum file when it finds the first checksum file, when all the checksum files are found , all the validation rules in the checksum file are summarized and applied to the validation of the processing method. If a validation rule that is specified in the two checksum file conflicts, only the validation rules in the following file are used.

"Java EE learning 36th Day" "Struts2" "Struts2 system 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.