When the XML-based configuration method is used for input validation,Action also needs to inherit actionsupportAnd provides the verification file,Put the verification file and action class in the same packageThe file name format is: ActionClassName-validation.xml, where actionclassname is the simple class name of action,-validation is fixed writing. If the action class is com. Jim. useraction, the file name should be: UserAction-validation.xml. The following is a template for file Verification:
<? XML version = "1.0" encoding = "UTF-8" ?>
<! Doctype validators public "-// opensymphony group // xwork validator 1.0.3 //" http://www.opensymphony.com/xwork/xwork-validator-1.0.3.dtd" >
< Validators >
< Field Name = "Username" >
< Field-validator Type = "Requiredstring" >
< Param Name = "Trim" > True </ Param >
< Message > The user name cannot be blank! </ Message >
</ Field-validator >
</ Field >
</ Validators >
<Field> specifies the attribute to be verified in the action. <field-validator> specifies the validator. The requiredstring specified above is provided by the system, the system provides validators that meet most of the verification requirements, and the definitions of these validators can be in the xwork-2.x.jar of COM. opensymphony. xwork2.validator. default under validators. in XML. <Message> This is the prompt message after verification failure. If you want to internationalize the message, you can specify the key attribute for the message. The key value is the key in the resource file. In this verification file, to verify the string-type username attribute in the action, you must first call the trim () method to remove spaces and then determine whether the username is empty. The list of validators provided by struts2 is as follows:
-
Required (required. The field value cannot be null)
-
Requiredstring)
-
Stringlength (String Length validator, which requires that the field value be within the specified range; otherwise, the verification fails. The minlength parameter specifies the minimum length and the maxlength parameter specifies the maximum length, the TRIM parameter specifies whether to remove spaces before and after the string before verifying the field)
RegEx (Regular Expression validator, which checks whether the verified field matches a regular expression. The expression parameter specifies the regular expression, and the casesensitive parameter specifies whether the regular expression is case sensitive when matching. The default value is true)
-
INT (integer validator, which requires that the integer value of the field be within the specified range, min to specify the minimum value, Max to specify the maximum value)
-
Double (double-precision floating point checker, requires that the field's double-precision floating point number must be within the specified range, Min specifies the minimum value, Max specifies the maximum value)
-
Fieldexpression)
-
Email (the email address checker requires that the field value be a valid email address if it is not empty)
-
URL (the URL validator requires that the field value be a valid URL if it is not empty)
-
Date (date validator, requires that the field date value be within the specified range, Min specifies the minimum value, Max specifies the maximum value)
-
Conversion (Conversion validator, which specifies the error message prompted when type conversion fails)
-
Visitor (used to verify the compound attribute in action. It specifies a validation file to verify the attribute in the compound attribute)
-
Expression (ognl expression checker. The expression parameter specifies the ognl expression. This logical expression is evaluated based on valuestack. If true is returned, it is verified. Otherwise, it fails, this validator cannot be used in the style configuration of the field validator)
Example Required <Field-validator type = "required"> <message> the gender cannot be blank! </Message> </field-validator> Requiredstring mandatory string validator <Field-validator type = "requiredstring"> <Param name = "trim"> true </param> <message> the user name cannot be blank! </Message> </field-validator> Stringlength: String Length checker <Field-validator type = "stringlength"> <Param name = "maxlength"> 10 </param> <Param name = "minlength"> 2 </param> <Param name = "trim"> true </param> <message> <! [CDATA [product name should be 2-10 characters in length]> </message> </field-validator> Email: email address checker <Field-validator type = "email"> <message> invalid email address </message> </field-validator> RegEx: Regular Expression validator <Field-validator type = "RegEx"> <Param name = "expression"> <! [CDATA [^ 1 [358] \ D {9} $]> </param> <message> incorrect mobile phone number format! </Message> </field-validator> INT: integer validator <Field-validator type = "int"> <Param name = "min"> 1 </param> <Param name = "Max"> 150 </param> <message> Age must be between 1-</message> </field-validator> Field ognl expression validator <Field name = "imagefile"> <field-validator type = "fieldexpression"> <Param name = "expression"> <! [CDATA [imagefile. Length () <= 0]> </param> <message> the file cannot be blank </message> </field-validator> </field>