Visitor checker of struts2 verification framework

Source: Internet
Author: User
The visitor validator is used to detect compound attributes in an action. For example, an action contains attributes of the user type.
Assume that the user class contains the username and password attributes. to verify whether the input values of the username and password attributes of the user class in the action are valid, you can use the visitor checker.
Generally, you can use [Action name] + [- Method Name (alias in struts. xml configuration) ] + Validation. XML to verify. In this case, you need to add a user-[context name]-validation to verify the user class. and user-[context name]-validation. the XML file must be in the same package as the user class.

If the action name is adduseraction, the validation file name is a AddUserAction-validation.xml with the following content: <!Doctype validators public
"-// Opensymphony group // xwork validator 1.0.2 // en"
Http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

< Validators >
<! -- User class in the verified action -->
< Field Name = "User" >
< Field-validator Type = "Visitor" >
<! -- The context name of the validation rule file is set to add. -->
< Param Name = "Context" > Add </ Param >
<! -- Specifies whether to add the following prefix after verification fails. If it is set to false, no error is displayed in the corresponding part of the tag. fielderrors must be used for display. -->
< Param Name = "Appendprefix" > True </ Param >
<! -- Prefix of the prompt message indicating verification failure -->
< Message > Error: </ Message >
</ Field-validator >
</ Field >

</Validators>

then create a User-add-validation.xml named in the package where the user class is located (add for the above param name =" context " > Add param >). The content is as follows:

code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> doctype validators Public
"-// opensymphony group // xwork validator 1.0.2 // en "
" http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd " >

<Validators>

< Field Name = "Username" >
< Field-validator Type = "Requiredstring" >
< Message > The user name cannot be blank. </ Message >
</ Field-validator >
</ Field >
< Field Name = "Password" >
< Field-validator Type = "Requiredstring" >
< Message > The password cannot be blank. </ Message >
</ Field-validator >
</ Field >

</Validators>

After the preceding settings, the user in the action can be verified!
Another: You can define a User-validation.xml to validate public attributes. After the visitor validator is defined, public rules in the User-validation.xml are automatically checked, for example, the visitor appeal validator writes only message attributes, only public attributes are verified.

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.