struts1.x Series Tutorials: Getting Started with the validator validation framework

Source: Internet
Author: User

I. Advantages of the validator framework

The validator framework is a struts plug-in that was originally designed and implemented by David Winterfeldt. The validator framework was available from the Struts 0.5 era, but at that time the validator framework was just a donated jar package. To make it easier for developers to use the validator framework, the Apache organization decided to start with Struts1.1 and publish the validator framework as part of struts with struts.

The validator framework can configure validation rules and validation objects in an XML file. Therefore, using the validator framework makes it easy to validate the client's submission data without overwriting the Validate method in the Actionform subclass. Because the validator framework has a number of predefined validation mechanisms built into it, such as verifying that an attribute exists, verifying that the email is legitimate, and so on. So in general, just configure the XML file to meet our verification requirements.

When you use the validator framework, you'll find that this is a better way to validate than using the Validate method directly:

1. Easier to maintain. Because authentication information can be placed in the same configuration file, it is easier for us to maintain these authentication information.

2. Standardization. Because many simple validations are the same. The username and password are required to be composed of letters, numbers, and the following strokes. If these validations are written in the Validate method, it is difficult to standardize these validations. These validation mechanisms in the validator framework are predefined, so standardizing the same validation will be very easy for the validator framework.

3. Avoid reinventing the wheel. Although some of the validations are simple, it is also very difficult to implement them correctly. A typical example is the format for verifying an email address. If this validation is to be perfect, you must verify the email address as required by the RFC-2822 specification. And if we use the validator framework, we don't have to reinvent the wheel to verify the email address.

4. Reduce the number of duplicate code. Because the validator framework provides a lot of predefined validations, we can avoid writing a lot of repetitive code to validate ourselves. Of course, we can also encapsulate much-used validation in class methods that avoid a lot of repetitive work, but that means that new members of our team must learn them before they can use these encapsulated validation methods. The worst case scenario is that many developers may forget to use the validation libraries implemented by other members and rewrite their own validation libraries with the same functionality. Of course, all this can be solved if the validator framework is used.

5. Client and server side validation automatic switching. We simply put a separate

Although the predefined validation of the validator framework has already met most of the validation requirements, in some exceptional cases, these predefined validations do not meet our requirements, and for this reason the validator framework also provides developers with the ability to extend the validation mechanism. This also enables the validator framework to perform more complex validation work.

Ii. configuration and use of the validator framework

1. Install Validator Frame

Because validator is a plug-in to struts, you need to install the validator framework in the Struts-config.xml file in the same way as the Struts plug-in. Open the Struts-config.xml file and add a <plug-in> child element to the <struts-config> element, as shown in the following code:

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
   <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validator.xml"/>
</plug-in>

Where the <set-property> element sets the value of the Pathnames property used in the plug-in. Two XML files were included in the Pathnames property value.

(1) Validator-rules.xml: A predefined validation of the validator framework is declared in this file. This file can be found in the Lib directory of Struts's release package. When you add a struts feature to a Web project using MyEclipse, the file is automatically added to the Web-inf directory.

(2) Validator.xml: This file defines the object to validate. In fact, in this file, you include one or more actionform subclasses and the properties and validation rules that you want to validate. Therefore, this file is equivalent to the Validate method. In the validator framework, you can have multiple XML files that define validation objects (you can scatter different actionform subclasses into different XML files), separated by commas (,), as shown in the following code:

<plug-in classname= "Org.apache.struts.validator.ValidatorPlugIn" >
<set-property property= "pathnames" value= "/web-inf/validator-rules.xml,/web-inf/validator1.xml,
/web-inf/validator2.xml,/web-inf/validator3.xml "/>
</plug-in>

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.