Original source: http://www.cnblogs.com/eggbucket/archive/2012/02/06/2339591.html
STRUTS2 Validation Framework
The input return page must be set in the action configuration
Add validation just create the validated XML file 1. Create XML file name Verify all methods in the action
Under action with package, create: Action class name-validation.xml
such as: validateaction create a validateaction-validation.xml validation action in a single method
<!--Each method configures a single action-->
<!--under action with the package, create: Action Class name-action method configuration name-validation.xml-->
<actionname= "Validateadd" class= "Com.struts2.validator.ValidateAction" method= "Add" >
<!--to create Validateaction-validateadd-validation.xml-->
<!--Configure--> using wildcard characters
<!--under action with the package, create: Action class name-action method corresponding to the name-validation.xml-->
<actionname= "validate_*" class= "com.struts2.validator.ValidateAction" method= "{1}" >
<!--to create Validateaction-validate_add-validation.xml,validate_add to access the path to this action method-->
Attention Matters
Attention:
1. The method to be validated cannot be called input.
2. Configure this in the form form to write a name in the <s:form action= "Validate_add" > action.
This will not find the corresponding configuration file, skipping validation.
3. If the validation error, return to the input page, those existing valuestack values will be lost, you can implement the action Preparable interface,
The value that is required to add the page is then initialized in the prepare () method.
4. If you use the Preparable interface, you must add the <interceptor-ref name= "Paramsprepareparamsstack"/> to the action configuration;.
So prepare () can get the parameters that form submits.
2. Create XML Content
<! DOCTYPE validators Public "-//opensymphony group//xwork Validator 1.0.2//en" "http://www.opensymphony.com/xwork/ Xwork-validator-1.0.2.dtd ">
<validators>
<!--the field name to be validated-->
<!--to verify the ID field of the UserBean in the action, name= "Userbean.id" (UserBean to the variable name in action)-->
<fieldname= "Mail" >
<!--type to be validated, short-circuit (default false), true meaning, if the current validation fails, the following validation is not performed. If requiredstring fails, email will not be validated. -->
<!--Field-validator can have multiple param elements, but only one message-->
<field-validatortype= "Requiredstring" >
<paramname= "Trim" >true</param>
<message>please Enter a mail</message>
</field-validator>
<field-validatortype= "Email" >
<message>
Invalid MAIL
</message>
</field-validator>
</field>
</validators>
Struts built-in validator (value of type)
Required
guarantees that the value of the field is not null. Empty string is not null value NULL.
<fieldname= "UserName" >
<field-validatortype= "Required" >
<message>please Enter a user name</message>
</field-validator>
</field>
requiredstring
Guarantees that the field is not null or blank (empty).
Param:trim (Boolean)->true-> remove before and after spaces
<fieldname= "UserName" >
<field-validatortype= "Requiredstring" >
<paramname= "Trim" >true</param>
<message>please Enter a user name</message>
</field-validator>
</field>
<fieldname= "Password" >
<field-validatortype= "Requiredstring" >
<paramname= "Trim" >false</param>
<message>please Enter a password</message>
</field-validator>
</field>
int
Verifies that the field value can be converted to an integer.
param:min (int); max (int)
<fieldname= "Yeaar" >
<field-validatortype= "int" >
<paramname= "Min" >1999</param>
<paramname= "Max" >2010</param>
<message>year:1999-2010</message>
</field-validator>
</field>
Date
Verifies that the value of a given date field is within a given range.
Param:max (date); min (date)
<fieldname= "Borthday" >
<field-validator