Struts2 learning verification framework 4 Rule file Writing Style

Source: Internet
Author: User

Writing Style of Rule files
In general, XWork has released two types of validators, including our custom ones.

Plain Validators/Non-Field validators
FieldValidators
The smooth validators (such as expression validators) are not bound to a specific attribute. When we define a smooth validators, we do not need to specify the attribute name.
Attribute validators (such as email validators) are based on a specific attribute and need to be in *-validation. xml specifies a fieldname attribute. there are two equivalent methods to define the property validators in XML syntax.

Non-Field Validator Vs Field-Validator
In the-validation. xml file, we can define the validators in two ways:

1 <validator>

2 <field-validator>

Remember the following syntax:

Non-property validators: Two validators can be defined in the <validator> element, whether smooth or attribute validators.

<Validator type = "expression>
<Param name = "expression"> foo gt bar </param>
<Message> foo must be great than bar. </message>
</Validator>
<Validator type = "required">
<Param name = "fieldName"> bar </param>
<Message> You must enter a value for bar. </message>
</Validator>
Property validators: The <field-validator> element inherits the fieldName attribute of the <field> element, basically the same as the <validator> element. the property validators defined in <field-validator> automatically obtain the fieldName attribute value from the fieldName attribute of the parent element <field>. this is because it is convenient to group some validators, that is, you can define multiple <field-validator> elements in a <field> element, all these property validators verify this property. if this is not done, you must specify the fieldName attribute in each <validator> element.

Note:

Generally, defining attribute validators in <field> elements is better than defining and specifying attribute names in <validator>, because this method is clear and clear.

Use field-validator to define an attribute validator:


[Html]
<Field name = "email_address">
<Field-validator type = "required">
<Message> You cannot leave the email address field empty. </message>
</Field-validator>
<Field-validator type = "email">
<Message> The email address you entered is not valid. </message>
</Field-validator>
</Field>

The following two methods are equivalent:

<Field name = "email_address">
<Field-validator type = "required">
<Message> You cannot leave the email address field empty. </message>
</Field-validator>
<Field-validator type = "email">
<Message> The email address you entered is not valid. </message>
</Field-validator>
</Field>
 
<Validator type = "required">
<Param name = "fieldName"> email_address </param>
<Message> You cannot leave the email address field empty. </message>
</Validator>
<Validator type = "email">
<Param name = "fieldName"> email_address </param>
<Message> The email address you entered is not valid. </message>
</Validator>

You can choose one by yourself.

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.