Use the Struts Validator framework author: Chuck Cavaness
Original: http://www.onjava.com/pub/a/onjava/2002/12/11/jakartastruts.html
Translator: javaduke
Email: javaduke@263.net
QQ: 658155
MSN: javaduke@hotmail.com
Each application is responsible for ensuring that the data they are inserted into the latter library is legally effective. After all, if the data relying on these applications is destroyed, it will be disastrous. What can I take anything? Come to make yourself working properly? For example, an application using a regular relational database, each field in the database has its own rules and constraints to ensure that the data stored in it is properly correct. Any application of the Taiwan-funded material database data is responsible for protecting the integrity of the data they submitted.
Any operation attempts to insert or update data that does not meet the standard may be discovered and rejected. This detection may be spread throughout the entire application, and there may be some verification in the performance layer, and the business logic layer, the business logic object is generally verified, and the data is also checked in the post-Taiwan investment.
Unfortunately, since this verification is ubiquitous in the application, it has caused code redundancy of applications to a certain extent. This is not what the app is hoped because this repetitive labor is repeated in a plurality of times, making the application deployment and maintenance take more time. If these validation rules can be reused throughout the app, they will make the application more flexible. In other words, the deployment is faster, the customization is easier, and the program is more flexible.
JAKARTA COMMONS project Validator Framework
Validator is an open source project created by David Winterfeldt, which is also a sub-project for Jakarta Commons. The Commons project is primarily providing some reusable components like Validator. Other well-known Commons components are as BeanUtils, Digester, Logging frameworks, and more. Validator version 1.0 was posted in early November 2002.
Benefits using Validator
Use the Validator framework than the general value of the verification rules in the application's code, such as:
. You can define verification rules for your application;
Verification rules and applications are loose coupling;
The server-side and client verification rules can be defined at the same point;
Configuring a new verification rule or modify an existing verification rule becomes simpler;
Support internationalization;
Support regular expressions;
Can be used for web applications can also be used for standard Java applications;
The method of declaration is realized rather than programming;
In addition to it, Validator's largest feature is its own support of pluggability. After the article
You will see the validation rules built in the Validator framework to better complete your work, and more importantly, the Validator Framework allows you to customize the verification program and insert it into the frame.
Struts and Validator relationships
It should be noted that the Validator framework itself is built due to the Struts framework. Validator's creator David Winterfeldt discovered in the process of using Struts, which requires repeated use of the same verification rule in many ACTIONForm classes, which causes a lot of code redundancy. So he decided to create a Validator framework to eliminate this redundancy, so that Validator was born.
Although the Validator architecture was originally born for the Struts architecture, it was still designed and configured to be used separately from the Struts architecture. This feature allows you to use this frame in any application, don't take it to the Struts architecture. It doesn't affect your application role because you don't use the Struts framework. In fact, this is why Validator is part of the Jakarta Commons project and is not part of the Struts project. Now let's integrate this framework to web applications like the Struts architecture. In the final midth of the article, we will show how to apply it to other types of applications, such as EJB-based applications.
Validator Component Overview
The Validator architecture has the following components:
Validators;
Profile;
Resource binding;
JSP custom label;
Validator Form class;
What is Validators?
A Validator is a Java class that calls the Validator framework when performing a verification rule. The framework calls this Validaotor class based on the method defined in the configuration file. In general, each Validator class provides a separate verification rule, and then these rules can be combined into a more complex rule set.
Note: Sometimes it is convenient, a Validator class can also define multiple authentication rules, and each rule is a static method and does not contain any client status information.
The framework provides 14 default verification rules, sometimes these rules are also known as the "Basic Rules" of the Validator framework, these basic rules, such as Table 1:
Name description
Byte, short, integer, whether the verification can be converted into a corresponding basic data type
Long, Float, Double
CreditCard checks if the input domain is a legal credit card number
DATE Test Your input domain is a legal date
Email check is a legal email address
Mask checks if the input field can successfully match a regular expression
Is the length of the maxlength test value less than or equal to a given maximum length
Is the length of the minlength test value is greater than or equal to a given minimum length
Whether the range of the RANGE test value is between the maximum and minimum values
Required checks if the input field is not empty, or whether the length of the space is not included is greater than zero
Table I
Just as seen in Table 1, the Validator framework provides most of the validation rules that the web application needs. You can use these existing verification rules to create your own verification profile. Despite this, as we mentioned earlier, and later, you can add more Validator according to your needs.
Now let's discuss how to configure these basic validator in a Struts architecture-based application.
The reason for the Validator framework is flexible in that all verification rules and their specific details are implemented by configuring declarations in external files. Your app doesn't have to know these specific verification rules. This feature allows you to expand and modify the rule set, you don't have to move your application's source code. This is very important for you to perform each personalized installation or when the demand changes.
If you use the struts1.1 Validator framework, you will use the two configuration files, a validator- rules.xml, another called validation.xml; actually you can also give them free to name, and you can merge them A XML file. However, you are still best to separate them because they have their own uses. Note: If you download Validator from the Jakarta website, it does not contain both files. These two files can only be found in the Struts of the Validator Framework.
Validator-rules.xml file
Validator-rules.xml file defines the validator that the application can use. Validator-rules.xml acts as a template's role, define the validator that all applications may be used.
Note: This XML file and another XML file we have to discuss below should be placed in the class loader. When we use the Validator framework in a web application, the correct location should be under Web-INF.
Validator-rules.xml file service from validator- rules_1_1.dtd, validator- rules_1_1.dtd can be downloaded in Jakarta.apache.org/struts/dtds/Validator- rules_1_1.dtd. We don't want to spend too much time to study the specific details of this document, we only make some basic introductions here.
The most important element in the Validator-Rules.xml file is included in the
Example: a simple validator-rules.xml file
The second profile of the Validation.xml file validator frame is this file called Validation.xml. In fact, you can name it as any name you like, or put it in the validator-rules.xml file.
Validation.xml is used to map all validator you defined in Validator-Rules.xml and components in your application. Since we discussed here is the use of the Validator framework in Struts, then validation.xml here is a mapping of these Validator and Struts. The ActionForm class is actually a class similar to JavaBean, which is used to capture user input and help to transfer these inputs to the next application components in STRUTS. The ActionForm also provides a convenient place to verify these inputs before user input is transmitted to the business logic layer. Example 2 is a simple validation.xml:
Example 2: A simple validation.xml file
Example I show a Name attribute called CheckoutForm's