Struts2 framework validator

Source: Internet
Author: User
Tags cdata valid email address

The struts2 framework system has registered a built-in checker. You can open the xwork under the Lib package of struts2. jar package, find a default under the COM/opensymphony/xwork2/validator/validators directory. xml configuration file, which defines the built-in checker of the struts2 framework.
From the struts2 configuration file, we can see that struts2 has the following built-in Checker:
1 required: required. The field must have a value.
Fieldname: this parameter specifies the verification field name. If it is a field verification, you do not need to specify this parameter.
2 requiredstring: A string validator. A value must be set and the length must be greater than 0. That is, it cannot be a null string. Spaces before and after the string are removed by default.
Fieldname: this parameter specifies the verification field name. If it is a field verification, you do not need to specify this parameter.
Trim: this parameter is an optional parameter that specifies whether to sort the string before verification.
3 stringlength: String Length checker, used to check whether the string length in the field is within the specified range
Fieldname: this parameter specifies the verification field name. If it is a field verification, you do not need to specify this parameter.
Trim: this parameter is an optional parameter that specifies whether to sort the string before verification.
Maxlength: specifies the maximum length of a string. this parameter is optional.
Minlength: specifies the minimum string length. This parameter is optional.
4 int integer checker. You can configure an integer within the specified range.
Fieldname: this parameter specifies the verification field name. If it is a field verification, you do not need to specify this parameter.
Min: specifies the minimum value of a field. This parameter is optional.
MAX: specifies the maximum value of a field. This parameter is optional.
5 double-precision checker, you can configure the number of double-precision in the specified range
Fieldname: this parameter specifies the verification field name. If it is a field verification, you do not need to specify this parameter.
Min1_sive: specifies the minimum value accepted by the field value. This parameter is optional.
Maxcompute sive: specifies the maximum value accepted by the field value. This parameter is optional.
Minexclusive: specifies the minimum excluded value of a field. This parameter is optional.
Maxexclusive: specifies the maximum excluded value of a field. This parameter is optional.
6. Date validator, which can be configured within the specified range
Fieldname: this parameter specifies the verification field name. If it is a field verification, you do not need to specify this parameter.
Min: specifies the minimum value of the field date value. This parameter is optional.
Parameter MAX: specifies the maximum value of the field date value. This parameter is optional.
6. Expression expression validator, which is a non-field validator. If the value calculated by expression is true, the verification is successful. Otherwise, a prompt is returned.
Parameter expression: this parameter is a logical expression that uses an ognl expression and returns a Boolean value based on the value stack calculation.
7. requiredstring field expression checker. If the value calculated by expression is true, the verification is successful. Otherwise, a prompt is returned.
Fieldname: this parameter specifies the verification field name. If it is a field verification, you do not need to specify this parameter.
Parameter expression: this parameter is a logical expression that uses an ognl expression and returns a Boolean value based on the value stack calculation.
8. The URL verification tool requires that the checked field be a valid URL if it is not empty.
Fieldname: this parameter specifies the verification field name. If it is a field verification, you do not need to specify this parameter.
9. the email address checker requires that the field to be checked be a valid email address if it is not empty.
Fieldname: this parameter specifies the verification field name. If it is a field verification, you do not need to specify this parameter.
10 visitor composite type checker
This validator is used to verify the composite type attributes defined in the action. It supports Simple Composite Type and array type. Map and other collection types.
Fieldname: this parameter specifies the verification field name. If it is a field verification, you do not need to specify this parameter.
Context: the context referenced by the validator. Optional.
Appendprefix: prefix the field verification information. Optional.

Required (required. The field value cannot be null );
Requiredstring (required string validator. The field value cannot be null and the length must be greater than 0. By default, spaces are removed before and after the string)
Requiredlength, 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 matches the regular expression. 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)
------------ Common ---------------
URL (the URL validator requires that the field value be a valid URL if it is not empty)
Data (date validator, requires that the field date value must 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)

Required
<Field-validator type = "required">
<Message> the gender cannot be blank! </Message>
</Field-validator>
Requiredstring mandatory string validator
<Field-validator type = "requiredstring">
<Message> 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> the email address is invalid </message>
</Field-validator>
RegEx: Regular Expression validator:
<Field-validator type = "email">
<Param name = "expressioin"> <! [CDATA [^ 1 [358] \ D {9} $]> </param>
<Message> the mobile phone number format is incorrect! </Message>
</Field-validator>

When writing a ActionClassName-validator.xml validation file, if there is no help information, you can solve the problem as follows:
Windows? preferences? myeclipse? files and editors? xml? xmlcatalog
Click Add, select File System in the location in the window that appears, and select the xwork-2.1.2 In the STR \ Java directory of the xwork-validator-1.0.3.dtd extract directory, when you return to the settings window, do not rush to close the window. Set the key type in the window to URL. Change key to http: www.opensymphony.com/xwork/xwork-validator-1.0.3.dtd

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.