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.