Common validation rules for validation. XML in struts2
Validation. XML Naming rules and placement path:
File Name: <actionclassname>-validation. xml
<Actionclassname> is the name of the action class to be verified.
To save this file to the same directory as the class file.
If the action class has multiple action instances in the struts configuration, the rules for verifying the file name of an action are 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)
Content example of validation. xml:
<? 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"> here is not available to replace with the following
<! Doctype validators public "-// Apache struts // xwork validator 1.0.2 //" http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd ">
<Validators>
<Field name = "username">
<Field-validator type = "requiredstring">
<Param name = "trim"> true </param>
<Message> enter the user name </message>
</Field-validator>
<Field-validator type = "stringlength">
<Param name = "minlength"> 4 </param>
<Param name = "maxlength"> 32 </param>
<Message> the length of the user name should be 4 to 32 characters </message>
</Field-validator>
</Field>
<Field name = "password">
<Field-validator type = "requiredstring">
<Message> enter the password. </message>
</Field-validator>
<Field-validator type = "stringlength">
<Param name = "minlength"> 6 </param>
<Param name = "maxlength"> 32 </param>
<Message> for your account security, set a password with more than 6 letters (up to 32 letters can be set) </message>
</Field-validator>
</Field>
</Validators>
Struts2 has the following verification rules:
Required: required.
Requiredstring: Required string checker
INT: integer validator
Double: double-precision floating-point checker
Date: Date validator
Expression: expression validator
Fieldexpression: field expression validator
Email: email validator
URL: URL validator
Visitor: Visitor checker
Conversion: conversion validator
Stringlength: String Length checker
RegEx: Regular Expression validator
You can use Google for specific purposes. The following describes several common verification rules:
Common verification rules:
1. Mandatory check
<Validators>
<Field name = "username">
<Field-validator type = "required">
<Message> indicates a message indicating a failed verification. </message>
</Field-validator>
</Field>
</Validators>
2. Required string check
<Validators>
<Field name = "username">
<Field-validator type = "requiredstring">
<Param name = "trim"> true </param>
<Message> indicates a message indicating a failed verification. </message>
</Field-validator>
</Field>
</Validators>
3. Integer Inspector/floating point check
<Validators>
<Field name = "Age">
<Field-validator type = "int">
<Param name = "min"> 1 </param>
<Param name = "Max"> 150 </param>
<Message> the age must be between 1 and 150 </message>
</Field-validator>
</Field>
</Validators>
4. Date Inspection
<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 Tester (the specified field must meet a logic expression)
<Validators>
<Field name = "re_pass">
<Field-validator type = "fieldexpression">
<! -- Specify the logical expression -->
<Param name = "expression"> (pass EQ re_pass) </param>
<Message> the password must be the same as the Confirmed 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. URLInspection
<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 in the range of 4 to 20 </message>
</Field-validator>
</Field>
</Validators>
9. Regular Expression Test
<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 a letter or array, and the length must be between 4 and 25 </message>
</Field-validator>
</Field>
</Validators>
After reading this story, you have to configure <result name = "input">/login in the struts configuration file. JSP </reslut>.