STRUTS2 Form Verification-principle plus basic usage

Source: Internet
Author: User
Tags cdata

There are two ways to struts2 form validation [both ways are the same data validation interceptor (validate) in action]:
1. Manual Verification: Manually write a validate () method to achieve data validation.
Premise: This action inherits Actionsupport or implements Validateable, Validationaware interface
In this method, write the data check code and add the error message to the value stack if there is an error. The error message in the value stack is not empty, and struts2 automatically returns the result of an input
Validate () verifies all the methods in the action
Write a validatexxx () method if you want to do a data check on only one of the XXX methods in action
JSP page mates <s:Filederror/> and <s:actionerror/> (shown with expression validation)

2. Using an XML configuration file to implement form validation
The data validation interceptor, when there is an XML file for the validation rule, automatically reads the validation rules inside to validate the field, and returns input if the validation does not pass the same as the type conversion process.
Premise: To perform data validation on an action, the XML file name must be the action name-validation.xml and the same directory as the action.
Note: Using expression validation, the resulting error, which belongs to Actionerror, uses <s:actionerror/> outputs this type of error message.

Common use of XML configuration files to implement form validation
<validators>
< validation rules for!--fields--

<!--check rules for age fields--
<field name= "User.age" >
<field-validator type= "int" >
<param name= "min" >20</param>
<param name= "Max" >30</param>
<message> age must be between ${min} to ${max} </message>
</field-validator>
</field>

<!--user name cannot be empty, length check-
<field name= "User.username" >
<field-validator type= "Requiredstring" >
<param name= "Trim" >true</param>
<message> user name cannot be empty </message>
</field-validator>

<field-validator type= "Stringlength" >
<param name= "MinLength" >6</param>
<param name= "MaxLength" >12</param>
<param name= "Trim" >true</param>
<message> user name must be between 6-12 bits </message>
</field-validator>

</field>

<!--Email Verification--
<field name= "User.email" >
<field-validator type= "Email" >
<message> email must comply with mail rules </message>
</field-validator>
</field>

<!--password length check-
<field name= "User.password" >
<field-validator type= "Requiredstring" >
<param name= "Trim" >true</param>
<message> password cannot be empty </message>
</field-validator>
<field-validator type= "Stringlength" >
<param name= "MinLength" >6</param>
<param name= "MaxLength" >12</param>
<param name= "Trim" >true</param>
<message> password length must be between 6-12 bits </message>
</field-validator>
</field>

<!--use regular, ID verification--
<field name= "User.idcard" >
<field-validator type= "Regex" >
<param name= "Regex" ><! [Cdata[^[1-9]\d{5}[1-9]\d{3} ((0\d) | ( 1[0-2]) (([0|1|2]\d) |3[0-1]) \d{4}$]]></param>
<message> ID must conform to rules </message>
</field-validator>
</field>


<!--use expression validation to verify that the password and Confirm password are the same-
<validator type= "expression" >
<param name= "expression" ><! [cdata[user.password==password2]]></param>
<message> Password and Confirm password must be the same </message>
</validator>
</validators>

Struts2 Form Validation-principle plus basic usage

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.