Struts data validation

Source: Internet
Author: User

1. Validate () Verification

Writes the contents of the page form validation to the Validate () method, enabling the separation of validation and business processing content


Add the Validate () method to the action
public void Validate () {
if (//****) {
Addfielderror ("name", "XX cannot xxx ....")  "); If the conditions do not match, you can use the Addfielderror () method to add to the Actioncontext.
}
}

Display error messages through <s:fielderror/> on JSP page

Once the Validate () method has been added to the action, once validation information has been added to the validation process, the STRUTS2 framework jumps to the view page of input with a more action configuration.

Tip: 1) When parameters are accepted, data failure also calls the Validate () method
2) Validate () method validation does not pass, business methods are not executed

2. validatexxx () Verification

When multiple forms are submitted to the same action page, the Validate () method takes effect for all forms, and you can use the ValidateXxx () method to implement validation of a business

For example:

The JSP form is submitted to Login.action's Login () method and can be login.action to add validatelogin () to the corresponding class.

Tips:

1) The Validate () method will work for all business methods in the action class
2) The ValidateXxx () method implements data validation for a business method.
I:STRUTS2 supports the ValidateXxx () method for data validation
I: Using the Validateregister () method to implement validation against the Register () method
  3) when the Validate () method and the ValidateXxx () method are present, they all work
 4) The invocation of the ValidateXxx () method is better than the Validate () method

3. Framework validation for STRUTS2

Regardless of which of the two methods above, you need to write the validation rules manually. When validation rules are complex, it causes the action class to become bloated. You can use the STRUTS2 framework and users

No coding is required, as long as the type of validation that a field needs to be made in an external configuration file and the error message is provided, the developer can be less burdensome and more productive.

1) You do not need to add the validate () or validatexxx () method in the Action class

2) Struts.xml file does not require authentication configuration

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 the validation file under Action with the package.

To verify the naming rules for files: Classname-validation.xml or Classname-alias-validation.xml

"File can view Struts ' sample Files"

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE validators Public "-//apache struts//xwork Validator 1.0.2//en" "http://struts.apache.org/dtds/ Xwork-validator-1.0.2.dtd "><validators><Fieldname= "Name"><Field-validatortype= "Requiredstring"><paramname= "Trim">True</param><message>User name cannot be empty</message></Field-validator><Field-validatortype= "Stringlength"><paramname= "MaxLength">10</param><paramname= "MinLength">6</param><message>User name length must be between ${minlength} and ${maxlength}</message></Field-validator></Field></validators>

Requiredstring: Specifies that a string cannot be null and cannot be an empty string

Stringlength: Checks the length range of a string, and can specify the minimum length and maximum of the field through the MinLength and maxlength two parameters

Fieldexpression: Use the OGNL expression to validate the field. For example <param name= "expression" >pwd1==pwd2</param>

Regex: Regular Expression

4. Data Validation Summary

Struts data validation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.