Struts2 Framework Validation

Source: Internet
Author: User

Struts2 Framework Validation (XML mode):
* First to get the value of the corresponding tag Name property from the page, declare the property with the same name in the action class action, provide the get and set methods

* Create an XML format validation file:
* Naming method: Actionclassname-validation.xml,actionclassname refers to the name of Action class action
* <validators> Tags: root element
* Field: Specifies the property to validate in the action, which is actually the value of the Name property of the form on the page
* Name: Specifies the value of the Name property of the form in the page
* Field-validator: Specify validation rules
* Type: Specifies the name of the validation rule,
The validation rules provided by the STRUTS2 framework are placed in the Xwork-core-xxx.jar
Under the Com\opensymphony\xwork2\validator\validators
The Default.xml configuration file.
* param: Parameters passed to the underlying validation rule
* Message: Error message provided when validation fails

* If the specified method is to be validated:
*XML Validation file naming method: Actionclassname-actionname-validation.xml,
ActionName corresponds to the value of the Name property of the action tag that corresponds to the Struts.xml file

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <!DOCTYPE validators Public3 "-//apache struts//xwork Validator 1.0.3//en"4 "Http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">5  <validators>6      <!-- 7 field: Specifies the property to validate in the action, which is actually the value of the form's Name property on the page8 * Name: Specifies the value of the Name property of the form in the page9        -Ten      <Fieldname= "username"> One      <!--  A Field-validator: Specifying validation rules - * Type: Specifies the name of the validation rule, - the validation rules provided by the STRUTS2 framework are placed in the Xwork-core-xxx.jar the under the Com\opensymphony\xwork2\validator\validators - the Default.xml configuration file -        - -          <Field-validatortype= "Requiredstring"> +          <!--  - param: Parameters passed to the underlying validation rule +            - A              <paramname= "Trim">True</param> at          <!--  - message: Error message provided when validation fails -            - -              <message><! [Cdata[user name cannot be empty]]></message> -          </Field-validator> -      </Field> in      <Fieldname= "pwd"> -          <Field-validatortype= "Requiredstring"> to              <paramname= "Trim">True</param> +              <message><! [Cdata[The password cannot be empty]]></message> -          </Field-validator> the          <Field-validatortype= "Regex"> *             <paramname= "Trim">True</param> $             <paramname= "expression"><! [Cdata[^[0-9a-za-z]{6,12}$]]></param>Panax Notoginseng             <message><! [Cdata[the length of the password must be between 6 and 12! ]]></message> -         </Field-validator> the      </Field> +  </validators>

Required: Ensure that the value of a given field is not a null value
requiredstring: Ensure that the value of a given field is neither null nor blank.
The trim parameter. The default is true, which means that struts rejects the front and back spaces before validating the field value.
stringlength: Verify that a non-empty field value is not sufficient in length.
minlength: The minimum length of the related field. If this parameter is not given, the field will not have a minimum length limit
maxlength: The maximum length of the related field. If this parameter is not given, the field will not have a maximum length limit
Trim: Whether or not to remove the front and rear spaces before verifying
int: Checks whether the value of a given field can be converted to an integer
min: The minimum value of the related field. If this parameter is not given, the field will have no minimum limit
Max: The maximum value of the related field. If this parameter is not given, the field will not have a maximum limit
Date: Ensures that the value of a given date field falls within a given range
Max: The maximum value of the related field. If this parameter is not given, the field will not have a maximum limit
min: The minimum value of the related field. If this parameter is not given, the field will have no minimum limit
Email: Check if the given String value is a legitimate email
URL: Checks whether the given String value is a valid URL
Regex: Checks whether the value of a given field matches a given regular expression pattern.
expresssion*: A regular expression to match
casesensitive: Whether to distinguish the case of letters. Default is True
Trim: Whether to remove space before and after. Default is True
conversion (conversion checker, specifying the error message when a type conversion fails)
visitor (used to validate the composite property in the action, which specifies that a checksum file is used to verify the properties in the composite property)
Expression (the OGNL expression Validator, which specifies the OGNL expression, which evaluates based on Valuestack and returns true if the checksum is passed or fails, the validator is not available in the field Validator style configuration)
Double (dual-precision floating-point validator, which requires a double-precision floating-point number for field must be within a specified range, min specifies the minimum value, max specifies the maximum value)

Struts2 Framework Validation

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.