Struts data verification and struts Verification
1. validate () Verification
Write the content verified by the page form to the validate () method to separate the verification content from the business processing content.
Add the validate () method to the Action
Public void validate (){
If (//****){
AddFieldError ("name", "XX cannot be XXX .... "); // If the condition does not match, you can use addFieldError () to add it to the ActionContext.
}
}
On the JSP page, use <s: fielderror/> to display the error message
After the validate () method is added to the Action, once verification information is added during the verification process, the Struts2 framework will jump to the input view page with more Action configurations.
Tip: 1) when the parameter is accepted, the validate () method is also called when the data fails.
2) The validate () method does not pass the verification and does not execute the business method.
2.ValidateXxx () Verification
When multiple forms are submitted to the same action page, the validate () method takes effect for all forms. You can use the validateXxx () method to verify a service.
For example:
The JSP form is submitted to the Login () method of login. action. You can add the validateLogin () Verification Method to the corresponding class of Login. action.
Tip:
1) The validate () method takes effect for all business methods in the Action class.
2) The validateXxx () method performs data verification on a business method.
I: Struts2 supports the validateXxx () method for data verification.
I: Use the validateRegister () method to verify the register () method.
3) The validate () method and the validateXxx () method both work.
4) The validateXxx () method is better than the validate () method.
3. Struts2 framework Verification
No matter which two methods are used, you need to manually write verification rules. When the verification rules are complex, the Action class will be bloated. You can use the Struts2 framework.
No encoding is required. You only need to specify the verification type for a field in the external configuration file and provide error information, which can reduce the burden on developers and improve development efficiency.
1) The validate () or validateXxx () method is not required in the Action class.
2) The struts. xml file does not require configuration verification.
3) JSP page
<s:fielderror/> <form action="login.action"> <s:text name="userName"></s:text> : <s:textfield name="name" ></s:textfield> <br/> <s:text name="userPwd"></s:text>: <s:password name="pwd" ></s:password> <br/> <s:text name="submit"></s:text> : <input type="submit" /> </form>
4) create a verification file under the same Action package.
Validation File naming rules: ClassName-validation.xml or ClassName-alias-validation.xml
[View the example file of struts]
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE validators PUBLIC "-// Apache Struts // XWork Validator 1.0.2 //" http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd "> <validators> <field name =" name "> <field-validator type =" requiredstring "> <param name =" trim "> true </param> <message> the user name cannot be blank </message> </field-validator> <field-validator type =" stringlength "> <param name =" maxLength "> 10 </param> <param name =" minLength "> 6 </param> <message> the username length must be at $ {minLength} and between $ {maxLength} </message> </field-validator> </field> </validators>
Requiredstring: specifies that a string cannot be null and cannot be a null string.
Stringlength: Check the length range of a string. You can use the minLength and maxlength parameters to specify the minimum length and maximum degree of the field.
Fieldexpression: use an OGNL expression to verify a field. For example, <param name = "expression"> pwd1 = pwd2 </param>
Regex: Regular Expression
4. Summary of data verification