Here, the jakarta-struts-1.1 is used as an example to illustrate the configuration method of validator verification.
- Reference jar package (jakarta-struts-1.1/jakarta-struts-1.1/LIB)
The implementation of validator verification requires two struts jar packages: commons-validator.jar and jakarta-oro.jar
Create the resource file applicationresources. properties in the src directory. If the file contains Chinese characters, convert it to unicode encoding. You can use the Eclipse plug-in propertyeditor, URL: http://propedit.sourceforge.jp/eclipse/updates/, and use naitve2
User. usrname = Name <br/> User. usrpwd = PASSWORD <br/> User. usrpwdconfirm = Confirm Password <br/> User. usrcomment = user description <br/> errors. required = {0} cannot be blank # <br/> errors. invalid = {0} contains invalid characters # <br/> errors. minlength = {0} cannot be smaller than {1} # <br/> errors. the number of maxlength = {0} digits cannot exceed {1} # <br/> errors. twofields = {0} and {1} are not equal #
- Add validator configuration file (jakarta-struts-1.1/jakarta-struts-1.1/LIB)
Copy the following two files to the WEB-INF directory (contents do not need to be modified unless you use a special verification, which will be described later ):
Validator-rules_1_1.dtd
Validator-rules.xml
Create a verification file under the WEB-INF directory:
Validation. XML (use the logon page as an example)
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <form-validation> <br/> <FormSet> <br/> <form name = "/user"> <br/> <field property = "usrname" depends = "required, maxlength, mask "> <br/> <arg0 key =" user. usrname "/> <br/> <arg1 name =" maxlength "Key =" $ {var: maxlength} "resource =" false "/> <br/> <arg2 name =" Mask "Key =" $ (VaR: mask) "resource =" false "/> <br/> <var-Name> maxlength </var-Name> <br/> <var-value> 20 </var-value> <br/> </var> <br/> <var-Name> mask </var-Name> <br /> <var-value> ^/W + $ </var-value> <br/> </var> <br/> </field> <br/> <Field property = "usrpwd" depends = "required, twofields, maxlength, mask "> <br/> <arg0 key =" user. usrpwd "/> <br/> <arg1 key =" user. usrpwdconfirm "/> <br/> <arg2 name =" maxlength "Key =" $ {var: maxlength} "resource =" false "/> <br/> <arg3 name =" Mask "Key =" $ (VaR: mask) "resource =" false "/> </P> <p> <var> <br/> <var-Name> maxlength </var-Name> <br/> <VaR -value> 20 </var-value> <br/> </var> <br/> <var-Name> mask </var-name> <br/> <var-value> ^/W + $ </var-value> <br/> </var> <br/> <var> <br/> <var-Name> secondproperty </var-Name> <br/> <var-value> usrpwdconfirm </var-value> <br/> </var> <br/> </field> <br/> <field property = "usrcomment" depends = "maxlength, mask "> <br/> <arg0 key =" user. usrcomment "/> <br/> <arg1 name =" maxlength "Key =" $ {var: maxlength} "resource =" false "/> <br/> <arg2 name =" Mask "Key =" $ (VaR: mask) "resource =" false "/> <br/> <var-Name> maxlength </var-Name> <br/> <var-value> 20 </var-value> <br/> </var> <br/> <var-Name> mask </var-Name> <br /> <var-value> ^/W + $ </var-value> <br/> </var> <br/> </field> <br/> </ form> <br/> </FormSet> <br/> </form-validation> <br/>
- Modify struts profiles struts-config.xml
...... <Br/> <action-mappings> <br/> <action name = "userform" parameter = "handler" <br/> Path = "/user" Scope = "request" <br/> type = "com. mycompany. action. useraction "<br/> input ="/user. JSP "> </P> <p> </Action> <br/> </Action-mappings> <br/> <message-resources parameter =" applicationresources "/> <br/> <plug-in classname = "org. apache. struts. validator. validatorplugin "> <br/> <set-property value ="/WEB-INF/validator-rules.xml,/WEB-INF/validation. XML "property =" pathnames "/> <br/> </plug-in> <br/> ......
Note: You must set input = "/user. jsp". Otherwise, an error is returned.
- Userform should inherit from validatexceptionform rather than actionform
Public class userform extends validatemeditionform
- User. jsp: return information code for failed verification Addition
Insert <HTML: errors/> where you want to display the error message.
Here, we can implement basic verification.