STRUTS2 Validation Framework validation.xml Common validation rules

Source: Internet
Author: User
Tags valid email address

naming rules and placement paths for Validation.xml:
File name: <actionclassname>-validation.xml
<ActionClassName> is the name of the action class to validate. To place this file in the same directory as the class file.
If the action class has more than one action instance (action name) in the struts configuration, then the validation file name rule for an action is as follows:
File name: <actionclassname>-<aliasname>-validation.xml
Example: Useraction-login-validation.xml
(Note: The above <aliasName> is not the method name, but the name of the action configured in Struts.xml)
Examples of validation.xml content:
<?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= "username" >
<field-validator type= "requiredstring" > <param name= "trim" >true</param> <message> please fill in your username </message> </field-validator>
<field-validator type= "Stringlength" > <param name= "minLength" >4</param> <param name= "MaxLength ">32</param>
<message> username should be between 4 and 32 characters in length </message>
</field-validator>
</field>

<field name= "Password" >
<field-validator type= "requiredstring" > <message> please fill in the password </message> </field-validator>
<field-validator type= "Stringlength" > <param name= "minLength" >6</param> <param name= "MaxLength ">32</param>
<message> for the security of your account, please set a password of 6 letters or more (up to 32 letters) </message>
</field-validator>
</field> </validators>
There are several types of validation rules for STRUTS2:Required: Required Validator requiredstring: Required String Validator int: integer validator double: dual precision floating-point calibrator Date: Date Validator expression: Expressions Validator Fieldexpression: Field Expression Validator Email: email validator URL: URL Validator visitor:visitor Validator conversion: Convert validator stringlength: string length validator regex: Regular Expression Validator
Specific use of the method can be Google, the following examples of several common validation rules: Common validation Rules:
1. Mandatory inspection <validators> <field name= "username" > <field-validator type= "Required" > <message> specify a message to verify failure </message> </field-validator> </field> </validators>
2. Required String validation <validators> <field name= "username" > <field-validator type= "requiredstring" > <param name = "Trim" >true</param> <message> Specify the message that the test failed </message> </field-validator> </field> </validators>
3. Integer verifier/floating point inspection <validators> <field name= "age" > <field-validator type= "int" > <param name= "min" >1 </param> <param name= "Max" >150</param> <message> age must be between 1 and 150 </message> </ Field-validator> </field> </validators>
4. Date validation <validators> <field name= "Birth" > <field-validator type= "Date" > <param name= "Min" > 1900-01-01</param> <param name= "Max" >2050-02-21</param> <message key= "Birth.range"/> </ Field-validator> </field> </validators>
5. Field Expression Verifier (requires that the specified field satisfies a logical expression)<validators> <field name= "Re_pass" > <field-validator type= "fieldexpression" > <!--Specify logical expressions-- <param name= "expression" > (pass eq re_pass) </param> <message> Password must be equal to the confirmation password </message> </ Field-validator> </field> </validators>
6. Email address verification <validators> <field name= "email" > <field-validator type= "email" > <message> Your email address must be a valid email address </message> </field-validator> </field> </validators>
7. Url Inspection<validators> <field name= "url" > <field-validator type= "url" > <message> your home address must be a valid URL </ message> </field-validator> </field> </validators>
8. String length test<validators> <field name= "user" ><field-validator type= "Stringlength" > <param name= "minlength" >4</param> <param name= "maxlength" >20</param> <message> your username must be between 4 and 20 in length </message > </field-validator> </field> </validators>
9. Regular expression Validation
<validators> <field name= "user" > <field-validator type= "regex" > <param name= "Expression_r" > <! [Cdata[(\w{4,25})]]></param> <message> The user name you enter can only be alphabetic and array, and must be between 4 and 25 in length </message> </ Field-validator> </field> </validators>

STRUTS2 Validation Framework validation.xml Common validation rules

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.