For input validation, Struts2 provides two methods: 1. Use the validate method; 2. Implement Based on XML configuration.
Validate () method
Actions that support verification must implement the Validateable interface. Generally, you can simply inherit ActionSupport.
Public void validate () {// takes effect for all actions. if (username = null | "". equals (this. username. trim () {this. addFieldError ("username", "username cannot be blank");} if (mobile = null | "". equals (this. mobile. trim () {this. addFieldError ("mobile", "mobile phone number cannot be blank");} else {if (! Pattern. compile ("^ 1 [3, 5, 8] \ d {9} $ "). matcher (this. mobile ). matches () {this. addFieldError ("mobile", "Incorrect mobile Phone Number Format") ;}} super. validate ();}
Public void validateUpdate () {// assume that this Action has a method named update (). // It only verifies the update () method validateXxx, the first method name must be capitalized if (username = null | "". equals (this. username. trim () {this. addFieldError ("username", "username cannot be blank");} if (mobile = null | "". equals (this. mobile. trim () {this. addFieldError ("mobile", "mobile phone number cannot be blank");} else {if (! Pattern. compile ("^ 1 [3, 5, 8] \ d {9} $ "). matcher (this. mobile ). matches () {this. addFieldError ("mobile", "Incorrect mobile Phone Number Format") ;}} super. validate ();}
For these error messages, you can use tags on the JSP page. To display
XML-based verification method
XML-based verification, the Action class also needs to inherit the ActionSupport class, and provide the verification file, the verification file with the Action class in a package, the file name format is: ActionClassName-validation.xml, the ActionClassName is the simple name of the Action class.
True
The user name cannot be blank.
True
The mobile phone number cannot be blank.
^1[358]\d{9}$
Incorrect Mobile Phone Number Format
Struts 2 common built-in checker
In the xwork-2.0.4.jar package (com. opensymphony. xwork2.validator. validators) There is a file default. xml that defines the built-in checker of the Struts2 framework. The default. xml file defines common validator types.
1. type conversion Tester:
(1) Non-Field Verification:
MyField
Type conversion error
True
(2) Field Verification:
Type conversion error
True
FieldName: this parameter specifies the name of the field to check whether a conversion exception exists. If it is a field verification, you do not need to specify this parameter.
RepopulateField: this parameter specifies whether the original incorrect input is retained when the type conversion fails and the input page is returned. True indicates retained, and false indicates not retained.
2. Date validator:
(1) Non-Field Verification:
Birthday
1990-01-02
2010-07-28
Birthday data error
(2) Field Verification:
1990-01-01
2010-07-28
Min: specifies the minimum value of the field date value. This parameter is optional.
Max: specifies the maximum value of the field date value. This parameter is optional.
3. floating point value Checker:
(1) Non-Field Verification:
Percentage
20.1
50.1
Birthday data error
(2) Field Verification:
20.1
50.1
Minclusive | minExclusive: specifies the minimum value of a field, including this value | does not contain this value.
Maxcompute sive | maxExclusive: specifies the maximum value of a field, including this value | does not contain this value.
4. Email Address Checker:
(1) Non-Field Verification:
MyEmail
Invalid email address
(2) Field Verification:
Invalid email address
5. Expression validator:
.......
Failed to meet OgnlExpression...
Expression: this parameter is a logical expression that uses an OGNL expression and returns a Boolean value based on the value stack calculation.
6. Field expression validator:
(1) Non-Field Verification:
myField
#myCreditLimit >#myGirfriendCreditLimit
My credit limit shouldbe MORE than my girlfriend
(2) Field Verification:
#myCreditLimit >#myGirfriendCreditLimit
My credit limitshould be MORE than my girlfriend
7. Integer Checker:
(1) Non-Field Verification:
Age
10
100
The age must be between $ {min} and $ {max }.
(2) Field Verification:
10
100
The age must be between $ {min} and $ {max }.
8. Regular Expression validator:
(1) Non-Field Verification:
MyStrangePostcode
([aAbBcCdD][123][eEfFgG][456])]></param> </validator></pre><br></p><p>(2)字段校验:</p><p> <pre class="code"> <field name="myStrangePostcode"> <field-validator type="regex"> <param name="expression"><![CDATA[#myCreditLimit >#myGirfriendCreditLimit
My credit limitshoshould be MORE than my girlfriend
Expression: A required parameter. It specifies a matching expression.
CaseSensitive: Specifies whether the matching is case sensitive. this parameter is optional and the default value is true.
9. Required validators:
(1) Non-Field Verification:
Username
The user name cannot be blank.
(2) Field Verification:
The user name cannot be blank.
10. Required string validators:
(1) Non-Field Verification:
Username
True
The user name cannot be blank.
(2) Field Verification:
True
The user name cannot be blank.
Trim: an optional parameter that specifies whether to sort the string before verification. The default value is true.
11. String Length Checker:
(1) Non-Field Verification:
Username
4
10
The username length ranges from $ {minLength} to $ {maxLength }.
(2) Field Verification:
4
10
true
12. url validator:
(1) Non-Field Verification:
myHomePage
Invalid homepageurl
(2) Field Verification:
Invalid homepageurl
13. visitor Checker:
The checker name is visitor, which is used to verify the composite type attributes defined in Action. It supports Simple Composite Type, array type, Map and other set types.
(1) Non-Field Verification:
user
myContext
true
(2) Field Verification:
myContext
true
To customize a checker of your own, you can add a validators. xml configuration file under the WEB-LIB \ lib directory of the Web project, which defines your checker.
Note: If the validators. xml configuration file is used, the default. xml configuration file will not be loaded. Therefore, you need to copy the content of default. xml to the validators. xml configuration file. Otherwise, you cannot use the system's built-in checker.