Using the Validator Framework with Strutsby Chuck Cavaness, author of Programming Jakarta Struts
Every application has a responsibility to ensure that only valid data is inserted into its repository. After all, what value would an application offer if the data that it relied upon were corrupted? For applications that use a formal database, like a RDBMS, for example, there are rules or constraints that can be placed upon the fields, which help to guarantee that the data stored within it meets a certain level of quality. Any and all applications that utilize the data within the repository have a responsibility to protect the integrity of the data that they submit.
Attempts to insert or update data that do not meet the criteria should be detected as soon as possible and rejected. This detection usually occurs in several places throughout an application; the presentation tier (if one is present) might perform some level of validation, the business objects typically have business-level validation rules, and as mentioned, the data repository usually does, as well.
Unfortunately, because data validation can occur in several areas within an application, there's usually a certain amount of redundancy that exists in applications for validating application data. This redundancy is generally an unwanted characteristic of an application, since it typically means longer development and maintenance times because the work is being repeated in multiple places. A more resilient application will attempt to reuse the validation rules throughout the application. This most likely translates into quicker development, easier customization cycles, and a more flexible application.
Jakarta Struts: Seven Lessons from the Trenches -- Chuck Cavaness, author of Programming Jakarta Struts, describes for Java programmers some of lessons he learned the hard way when he used the Struts framework to build a company application.
Introducing the Jakarta Commons Validator
The Validator framework is an open source project that was created by David Winterfeldt and is part of the Jakarta Commons subproject. The Commons project was created for the purpose of providing reusable components like the Validator. Other well-known Commons components include BeanUtils, Digester, and the Logging framework. Version 1.0 of the Validator was released at the beginning of November 2002.
The Benefits of Using the Validator
The Validator framework offers several benefits over the more conventional method of defining validation rules within the source code of an application. A few of the benefits include:
A single definition of validation rules for an application.
Validation rules are loosely coupled to the application.
Server-side and client-side validation rules can be defined in one location.
Configuration of new rules and/or changes to existing rules are made simpler.
Supports regular expressions.
Can be used for both Web-based and standard Java applications.
Promotes a declarative approach rather than a programmatic one.
These benefits aside, the most important characteristic of the Validator is its inherent support for pluggability. As you'll see later in this article, the Validator comes with several built-in validation rules that you can leverage right "out of the box." More importantly, however, the Validator allows you to define your own validation routines and easily plug them into the existing framework.
The Relationship Between Struts and the Validator
It should be pointed out that the Validator was originally created for use with the Jakarta Struts framework. The creator of the Validator, David Winterfeldt, was using Struts and realized that there was a great deal of redundancy in programming the same validation rules over and over again inside of the Struts ActionForm classes. He decided to create the Valdiator framework to eliminate that redundancy and subsequently, the Validator was born.
Although the Validator was originally created for use with Struts, it is designed and built in such a way that makes it possible to use on its own, without Struts. This characteristic allows you to use the framework in your applications, Struts-based or not. Just because you are not using Struts doesn't mean that you can't leverage the fine work that's been done. In fact, this is why the Validator is part of the Jakarta Commons project rather then being tied directly to the Struts project.
For now, we'll stick to the use of the framework with Web applications like those built on top of Struts. Later in the article, we'll discuss the steps required to use it with other types of applications, like those built with EJBs.
Overview of the Validator Components
There are several components that make up the Validator framework.
- Configuration Files
- Resource Bundle
- JSP Custom Tags
- Validator Form Classes
What are Validators?
A Validator is a Java class that, when called by the Validator framework, executes a validation rule. The framework knows how to invoke a Validator class based on its method signature, as defined in a configuration file. Typically, each Validator provides a single validation rule, and these rules can be chained together to form a more complex set of rules.
Note: It's possible to define multiple rules within a single Java class. You might do this for convenience. Each validation rule is a static method and contains no client- specific state.
The framework provides 14 default validation rules, which are sometimes referred to as "basic validators." The basic Validators are listed in Table 1.
Table 1. Basic Validators provided by the framework.
|Checks if the value can safely be converted to the corresponding primitive.|
|Checks if the field is a valid credit card number.|
|Checks if the field is a valid date.|
|Checks if the field is a valid email address.|
|Succeeds if the field matches the corresponding regular expression mask.|
|Checks if the value's length is less than or equal to the given maximum length.|
|Checks if the value's length is greater than or equal to the given minimum length.|
|Checks if the value is within a minimum and maximum range.|
|Checks if the field isn't null and length of the field is greater than zero, not including whitespace.|
As you can see from Table 1, the Validator framework provides many of the validation rules that most Web applications require. You can leverage these existing rules just by using the framework and setting up the proper configuration files. However, as was mentioned earlier and as we'll talk about later, you are free to add more Validators based on the needs of your application.
For now, let's stick with the basic Validators and discuss how you configure them for a Struts-based application.