Depending on the processing location of the input check, the input check can be divided into two types: client Checksum server. Server-side validation is currently available in two ways:
The first type:
Reference: struts2: Data validation, using the Validate () method in action to verify, plot
The second type:
When using the Validate () method checksum, the validate () method needs to be rewritten multiple times if there is a large number of actions in the Web app, which makes the code cumbersome. Since the STRUTS2 calibration framework is essentially based on the Xwork Validato framework, the validator framework of the xwork can be used to validate the data in order to reduce the amount of code. This paper discusses this kind of verification method.
1. The front page adds the error prompt label (registerx.jsp)
<%@ page language= "java" import= "java.util.*" pageencoding= "utf-8"%><%@ page iselignored= "false"%> <%@ taglib uri= "/struts-tags" prefix= "s"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
2. Background action inheritance Actionsupport (Registerxaction.java)Package Com.clzhang.ssh.demo1;import Com.opensymphony.xwork2.actionsupport;public Class Registerxaction extends Actionsupport {public static final long serialversionuid = 1; Private String username; private String password; Private String Repassword; Private Integer age; Private Double height; Public String GetUserName () {return username; } public void Setusername (String username) {this.username = username; } public String GetPassword () {return password; } public void SetPassword (String password) {this.password = password; } public String Getrepassword () {return repassword; } public void Setrepassword (String repassword) {This.repassword = Repassword; } public Integer Getage () {return age; public void Setage (Integer age) {this.age = age; } public Double GetHeight () {return height; public void SetHeight (Double height) {this.height = height; } @Override Public String execute () {System.out.println (username + "|" + Age + "|" + height + "Register fin Ished! "); return "register"; }}3. Add calibration configuration file (Registerxaction-validation.xml)3.1 Naming conventions
Verify file naming rules: Actionname-validation.xml, where ActionName is the class name of the action that needs to be validated.
If you need to validate a particular method in action, you need to specifically define a checksum file for that particular method (otherwise, call the default). The file naming convention is: Actionnme-methodnae-validation.xml; Also, you need to specify its method property when the Struts.xml file is configured for action (otherwise, the default is called).
3.2 Position Specification
The file should be located under the same path as the file for the action class.
3.3 Definition of the file
Reference: Description of the Xwork-validator-1.0.x.dtd file in the Struts-2.3.x\lib\xwork-core-2.3.x.jar package.
3.4 Types of validator in this file
Reference: Default.xml file in Xwork-core-2.3.x.jar\com\opensymphony\xwork2\validator\validators.
3.5 Steps to follow for data validation
- The type converter in the STRUTS2 framework converts data in the HTTP request data type to a value that conforms to the type, for example, in this case, age is converted to int.
- Use STRUTS2 's Xwork validation framework to verify data based on Actionname-validation.xml files and actionname-methodname-validation.xml files.
- Call the Validatex () method to verify the data.
- Call the Validate () method for data validation.
- If a data validation error occurs, a result named input is returned, entering the specified view resource without invoking the business logic processing method that was supposed to be invoked. If no error occurs during data validation, the business logic processing method in the corresponding action is invoked.
3.6 How to configure the checksum file
The STRUTS2 framework provides two ways to configure the checksum file, one for the field check configuration, and one for the non-field check configuration method.
Field Check mode (field-validator)
<! DOCTYPE validators Public "-//apache struts//xwork Validator 1.0.2//en" "Http://struts.apache.org/dtds/xwork -validator-1.0.2.dtd "><validators> <field name=" username "> <field-validator type=" Requiredstri ng "> <param name=" trim ">true</param> <message> Please enter your user name </message> < ;/field-validator> <field-validator type= "Stringlength" > <param name= "minLength" &G t;2</param> <param name= "maxLength" >16</param> <message> user name length in ${minlength} Between the ${maxlength} bits! </message> </field-validator> </field> <field name= "password" > <field -validator type= "requiredstring" > <param name= "trim" >true</param> <message> Please enter a secret Yards </message> </field-validator> <field-validator type= "regex" > <par Am Name= "Expression" ><! [cdata[\w{6,12}]]></param> <message> Password must be between 6-12 bits and can only be letters and numbers </message> </field -validator> </field> <field name= "Repassword" > <field-validator type= "requiredstring" > <param name= "Trim" >true</param> <message> Please enter a confirmation password </message> </field- validator> <field-validator type= "fieldexpression" > <param name= "expression" >&l t;! [cdata[repassword==password]]></param> <!--can also be used repassword.equals (password)//--> <MESSAGE&G t; two password unequal </message> </field-validator> </field> <field name= "age" > <field -validator type= "Required" > <param name= "Trim" >true</param> <message> age cannot be empty! </message> </field-validator> <field-validator type= "int" > <param name="Min" >1</param> <param name= "Max" >60</param> <message> age range in ${min} to ${m AX} between </message> </field-validator> </field> <field name= "height" > <field- Validator type= "Double" > <param name= "min" >1.10</param> <param name= "Max" >2.1 0</param> <message> height range between ${min} to ${max} meters </message> </field-validator> & Lt;/field></validators>Non-field check mode (Non-fild validator), this is just a demonstration, not in this case.
View Code
3.7 Checker Introduction
- Required validator, requires field is not empty;
- Requiredstring Validator, requires that the field value is not empty and the length is greater than 0;
- int validator, which represents integers, can be specified by the Min/max parameter;
- Date validator, which requires dates, can be specified by the Min/max parameter;
- Email validator, request e-mail format;
- The Stringlength validator, specifying the field length range through the Minlength/maxlength parameter.
4. Configure errors in the Struts.xml file to return to the page address
<action name= "Registerx" class= "com.clzhang.ssh.demo1.RegisterXAction" > <result name= "register" >/ ssh/demo1/success.jsp</result> <result name= "Input" >/ssh/demo1/registerX.jsp</result> </action>
5. Testing
Open IE, enter address: http://127.0.0.1:8080/st/ssh/demo1/registerX.jsp
The results are as follows:
Submitted directly, the results are as follows:
(EXT) STRUTS2: Data validation via xwork Check Framework (validation.xml)