In normal development, it is essential for me to use struts2 form verification. Today I will share with you two forms for verification.
1. Inherit the ActionSupport class implementation test method
ActionSupport is a tool class that has implemented the Action interface. In addition, it also implements the Validateable interface and provides the data verification function. By inheriting the ActionSupport class, it can simplify the development of Struts2 actions.
The Validatable interface defines a validate () method to override this method. If an error occurs in the validation form input field, add the error to the fieldErrors field of the ActionSupport class, and then pass, the OGNL expression is used for output.
To enable Struts2 to provide the input data verification function. The LoginAction. java class must inherit the sub-ActionSupport and override the validate () method. For example, to override the validate () method, the program is as follows:
/**
* Verification
*/
Public void validate ()
{
If (name = null | name. equals (""))
{
AddFieldError ("name", getText ("user name cannot be blank "));
}
If (age = null | age. equals (""))
{
AddFieldError ("age", getText ("age cannot be blank "));
}
}
This class overrides the validate () method, which is executed before the execute method of the system,
II. Verification of the XWork-based Validator framework
The above input validation is implemented through the validate () method of the ActionSupport class. Although this method is good, a large number of validate () methods need to be rewritten.
Similar to struts1, struts2 allows you to define a configuration file for data verification. The validation framework of struts2 is actually based on the Validator () framework of XWork. The validator definition file is located in the com/opensymphony/xwork2/Validator/validators/default. xml file of the java package of Xwork.
1 <? Xml version = "1.0" encoding = "UTF-8"?>
2 <! DOCTYPE validators PUBLIC
3 "-// OpenSymphony Group // XWork Validator Config 1.0 // EN"
4 "http://www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd">
5 <! -- Start snippet: validators-default -->
6 <validators>
7 <validator name = "required" class = "com. opensymphony. xwork2.validator. validators. RequiredFieldValidator"/>
8 <validator name = "requiredstring" class = "com. opensymphony. xwork2.validator. validators. RequiredStringValidator"/>
9 <validator name = "int" class = "com. opensymphony. xwork2.validator. validators. IntRangeFieldValidator"/>
10 <validator name = "double" class = "com. opensymphony. xwork2.validator. validators. DoubleRangeFieldValidator"/>
11 <validator name = "date" class = "com. opensymphony. xwork2.validator. validators. DateRangeFieldValidator"/>
12 <validator name = "expression" class = "com. opensymphony. xwork2.validator. validators. ExpressionValidator"/>
13 <validator name = "fieldexpression" class = "com. opensymphony. xwork2.validator. validators. FieldExpressionValidator"/>
14 <validator name = "email" class = "com. opensymphony. xwork2.validator. validators. EmailValidator"/>
15 <validator name = "url" class = "com. opensymphony. xwork2.validator. validators. URLValidator"/>
16 <validator name = "visitor" class = "com. opensymphony. xwork2.validator. validators. VisitorFieldValidator"/>
17 <validator name = "conversion" class = "com. opensymphony. xwork2.validator. validators. ConversionErrorFieldValidator"/>
18 <validator name = "stringlength" class = "com. opensymphony. xwork2.validator. validators. StringLengthFieldValidator"/>
19 <validator name = "regex" class = "com. opensymphony. xwork2.validator. validators. RegexFieldValidator"/>
20 </validators>
21 <! -- End snippet: validators-default -->
To verify the Action form data, you must create a Validation rule file for the Action. The naming rules for this file should follow the following rules: ActionName-validation.xml: its ActionName is the class name of the Action to be verified. Therefore, the above rules should follow this rule. The preparation file is as follows:
1 <? Xml version = "1.0" encoding = "UTF-8"?>
2 <! DOCTYPE validators PUBLIC "-// OpenSymphony Group // XWork Validator 1.0.2 // EN"
3 "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
4 <validators>
5 <field name = "name1">
6 <field-validator type = "requiredstring">
7 <param name = "trim"> true </param>
8 <message> the user name cannot be blank </message>
9 </field-validator>
10 <field-validator type = "stringlength">
11 <param name = "trim"> true </param>
12 <param name = "maxLength"> 8 </param>
13 <param name = "minLength"> 3 </param>
14 <message> The username length should be between $ {maxLength} and $ {minLength} </message>
15 </field-validator>
16 </field>
17 <field name = "age1">
18 <field-validator type = "requiredstring">
19 <param name = "trim"> true </param>
20 <message> age cannot be blank </message>
21 </field-validator>
22 </field>
23 </validators>