Struts1.x Series Tutorials (one): Advanced application of Validator validation framework

Source: Internet
Author: User
Tags constant regular expression

In the struts1.x Series Tutorial (Ten): Introduction to the Validator validation framework the name, age, and email in the example given in the article are simple attributes, and if a nested property (that is, the data type of a property is another class with attributes), you can use a dot (.) Symbols to access. We can verify the salary property in the Employee property by setting the property value of <field> element to "Employee.salary".

If you want to validate the indexed properties, we can use the <field> element's Indexedlistproperty property to specify this indexed property, as shown in the following code:

<field indexedListProperty="items" depends=……>……</field>

After using the configuration code above, the validator framework verifies each value of the Items property by pressing the constraints in the Depends property. We can also use the property and Indexedlistproperty as shown in the following code:

<field indexedListProperty="items" property="employee.age" depends=……>……</field>

The validator framework verifies the Employee.age property of each item in the items property based on the configuration code described above.

Second, using constants and variables

1. constant

The name attribute in the struts1.x series Tutorial: Getting Started with the validator validation framework uses mask for validation. This mask actually represents a regular expression. But if there are many places in the Validator.xml file that need to use this regular expression, it will be repeated in many places. To do this, we can use constants to avoid this kind of thing happening.

Constants are divided into global constants and local constants.

(1) Global constants

Global constants can be used in all of the form defined in Validator.xml. We can use the <global> element to configure global constants. To configure global constants, place the following in front of the first <formset> element.

<global>
    <constant>
      <constant-name>mask</constant-name>
      <constant-value>^[a-zA-Z0-9]*$</constant-value>
    </constant>
  </global>
  <formset>

(2) Local constants

Local constants need to be placed at the beginning of the <formset> element (according to the DTD definition, local constants can only be placed here, and global constants may be placed in front of the first <formset> element, or behind <formset>). As shown in the following code:

<formset>
    <constant>
      <constant-name>mask</constant-name>
      <constant-value>^[a-zA-Z0-9]*$</constant-value>
    </constant>
    <form name="firstValidatorForm">……
    ……
  </formset>

We can write the following code to use the Mask constants:

<var>
   <var-name>mask</var-name>
   <var-value>${mask}</var-value>
</var>

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.