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>