Struts2 Validator Framework Use method detailed

Source: Internet
Author: User
Tags prepare
Original source:
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
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" " Xwork-validator-1.0.2.dtd ">

<!--the field name to be validated-->
<!--to verify the ID field of the UserBean in the action, name= "" (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-validatortype= "Email" >
Invalid MAIL
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>

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>
<fieldname= "Password" >
<field-validatortype= "Requiredstring" >
<paramname= "Trim" >false</param>
<message>please Enter a password</message>

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>

Verifies that the value of a given date field is within a given range.
Param:max (date); min (date)

<fieldname= "Borthday" >
Related Article

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: 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.