STRUTS2 validation of data 2 ways

Source: Internet
Author: User

Data validation for STRUTS2:

The checksum of the data is divided into two types: client Checksum server side.

Client-side checksum: The checksum completed by JS. (To improve the user experience. Reduce user input errors)

Server-side check: Check in the background. (Required.)

Manual encoding for verification:

1. Write an action that must inherit the Actionsupport.

2. Override the Validate method:---Validate against all methods in the action.

@Override

STRUTS2 provides a way to verify the data:

public void Validate () {

if (username = = NULL | | Username.trim (). Length () = = 0) {

Addfielderror ("username", "user name cannot be empty!");

}

if (password = = NULL | | Password.trim (). Length () = = 0) {

Addfielderror ("Password", "Password cannot be empty!");

}

}

What if you check for one of the methods in the action? Verify the Add method.

* Writing method Validateadd ();

use XML to configure the check mode:

1. Write an action inheritance Actionsupport

2. Provide a Get method for the corresponding attribute.---to get the value of the variable in the XML.

3. Create a-validation.xml file with the same name as the action class under the action package.

* such as the name of the XML: Registaction2-validation.xml

* Introduction of DTD:XWORK-CORE-2.3.7.JAR/XWORK-VALIDATOR-1.0.3.DTD

* Configuration in XML:

<validators>

<field name= "username" >

<field-validator type= "Requiredstring" >

<message> user name cannot be empty! (XML) </message>

</field-validator>

</field>

<field name= "Password" >

<field-validator type= "Requiredstring" >

<message> password cannot be empty! (XML) </message>

</field-validator>

</field>

</validators>

How do I validate a method with an XML checksum?

* Create a <action> name-validation.xml with the same name as the action class under the package where the action is located

<action name= "Regist2" class= "" method= "execute" >

The name of the XML created is:registaction2-regist2-validation.xml

<validators>

<field name= "Password" >

<field-validator type= "Stringlength" >

<param name= "MinLength" >6</param>

<param name= "MaxLength" >12</param>

<message> password must be between 6-12 bits! (XML) </message>

</field-validator>

</field>

</validators>

Comprehensive Exercises:

<validators>

<field name= "username" >

<field-validator type= "Requiredstring" >

<message> user name cannot be empty!</message>

</field-validator>

</field>

<!--check password--

<field name= "Password" >

<field-validator type= "Requiredstring" >

<message> password cannot be empty!</message>

</field-validator>

<field-validator type= "Stringlength" >

<param name= "MinLength" >6</param>

<param name= "MaxLength" >12</param>

<message> password needs to be between 6-12 bits </message>

</field-validator>

</field>

<!--check Confirm password--

<field name= "Repassword" >

<field-validator type= "Fieldexpression" >

<param name= "expression" ><! [Cdata[(Password==repassword)]]></param>

<message> two times password input inconsistency!</message>

</field-validator>

</field>

<!--check Age--

<field name= "Age" >

<field-validator type= "int" >

<param name= "min" >18</param>

<param name= "Max" >60</param>

<message> age must be between 18-60 </message>

</field-validator>

</field>

<!--Birthday--

<field name= "Birthday" >

<field-validator type= "Date" >

<param name= "min" >1954-01-01</param>

<param name= "Max" >1996-12-31</param>

<message> birthdays need to be between 54-96 years </message>

</field-validator>

</field>

<!--Check your mailbox--

<field name= "Email" >

<field-validator type= "Email" >

<message> mailbox format is incorrect </message>

</field-validator>

</field>

<!--Telephone--

<field name= "Phone" >

<field-validator type= "Regex" >

<param name= "expression" ><! [cdata[^18\d{9}$]]></param>

<message> cell phone number format is incorrect!</message>

</field-validator>

</field>

</validators>

STRUTS2 validation of data 2 ways

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.