Simplify Struts development with Validator's rich set of built-in validations.
One major benefit of the Struts framework is its built-in interface for performing data validations on incoming form data. If any validations fail, the application redisplays the HTML form so that the invalid data can be corrected. Otherwise, processing continues. The Struts framework's simple validation interface alleviates much of the headache associated with handling data validation, allowing you to focus on validation code and not on the mechanics of capturing data and redisplaying incomplete or invalid data.
Struts' built-in validation interface, however, has its shortcomings. Often, for example, validation code is heavily duplicated throughout an application, because many fields require the same validation logic. Any change in the validation logic for similar fields requires code changes in several places as well as recompilation of the affected code. To solve this problem and to enhance Struts' validation interface, the Validator framework was created as a third-party add-on to Struts. Validator was later integrated into the core Struts code base and has since been detached from Struts and is now a standalone Jakarta Commons project. Although Validator is an independent framework again, it still comes packaged and seamlessly integrated with Struts.
Validator Overview
Without Validator, you have to code all of your form data validations into the validate( ) methods of your Form Bean objects. Each Form Bean field on which you want to perform a validation requires you to code logic to do so. Additionally, you have to write code that stores error messages for validations that fail.
With Validator you don't have to write any code in your Form Beans for validations or storing error messages. Instead, your Form Beans extend one of Validator's ActionForm subclasses that provide this functionality for you.
The Validator framework is set up as a pluggable system of validation routines that can be applied to Form Beans. Each validation routine is simply a Java method responsible for performing a specific type of validation and can either pass or fail. By default, Validator comes packaged with several useful validation routines that will satisfy most validation scenarios. However, if you need a validation not provided by the Validator framework, you can create your own custom validation routine and plug it into the framework. Additionally, Validator supports both server-side and client-side (JavaScript) validations, whereas Form Beans provide only a server-side validation interface.
Validator uses two XML configuration files to determine which validation routines should be installed and how they should be applied for a given application, respectively. The first configuration file, validator-rules.xml, declares the validation routines that should be plugged into the framework and provides logical names for each of the validations. The validator-rules.xml file also defines client-side JavaScript code for each validation routine. Validator can be configured to send this JavaScript code to the browser so that validations are performed on the client side as well as on the server side.
The second configuration file, validation.xml, defines which validation routines should be applied to which Form Beans. The definitions in this file use the logical names of Form Beans from the struts-config.xml file along with the logical names of validation routines from the validator-rules.xml file to tie the two together.
Using the Validator framework involves enabling the Validator plug-in, configuring Validator's two configuration files, and creating Form Beans that extend the Validator's ActionForm subclasses. The following sections explain in detail how to configure and use Validator.
Enabling the Validator Plug-in
Although the Validator framework comes packaged with Struts, Validator is not enabled by default. To enable Validator, add the following plug-in definition to your application's struts-config.xml file.
<!-- Validator Configuration -->
<plug-in className="org.apache.struts
  <set-property property="pathnames"
  validator-rules.xml, /WEB-INF/
This definition tells Struts to load and initialize the Validator plug-in for your application. Upon initialization, the plug-in loads the comma-delimited list of Validator config files specified by the pathnames property. Each config file's path should be specified by use of a Web application-relative path, as shown in the previous example.
Note that your application's struts-config.xml file must conform to the Struts Configuration Document Type Definition (DTD), which specifies the order in which elements are to appear in the file. Therefore, you have to put the Validator plug-in definition into the proper place in the file. The easiest way to ensure that you are properly ordering elements in the file is to use a tool, such as Struts Console, that automatically formats your configuration file so that it conforms to the DTD.
Configuring validator-rules.xml
The Validator framework is set up as a pluggable system whose validation routines are simply Java methods that are plugged into the system to perform specific validations. The validator-rules.xml file declaratively plugs in the validation routines Validator uses for performing validations. Struts example applications come packaged with preconfigured copies of this file. Under most circumstances, you will not need to modify the preconfigured copies unless you are adding your own custom validations to the framework.
Listing 1 is a sample validator-rules.xml file that illustrates how validation routines are plugged into Validator. Each validation routine in the validator-rules.xml file has its own definition that is declared with a validator tag, which is used to assign a logical name to the routine, using the name attribute, and to specify the class and method for the routine. The routine's logical name is used by other routines in this file as well as by validation definitions in the validation.xml file to refer to the routine.
Note that the validator tag encapsulates a javascript tag, which is used to define client-side JavaScript code for performing the same validation on the client side as on the server side.
Included Validations
By default, Validator comes packaged with several basic validation routines you can use to handle most validation scenarios. These routines have logical names, such as required (for required values), CreditCard (for credit card number values), email (for e-mail address values), and so on.
Creating Form Beans
To use Validator, your application's Form Beans have to subclass one of Validator's ActionForm subclasses instead of ActionForm itself. Validator's ActionForm subclasses provide an implementation for ActionForm's validate( ) method that hooks into the Validator framework. Instead of hard-coding validations into the validate( ) method, you simply omit the method altogether, because Validator provides the validation code for you.
Parallel to the core functionality provided by Struts, Validator gives you two paths to choose from when creating Form Beans. The first path you can choose is to create a concrete Form Bean object like the following:
package com.jamesholmes.minihr;
import org.apache.struts.validator
public class LogonForm extends ValidatorForm {
  private String username;
  private String password;
  public String getUsername() {
    return username;
  public void setUsername(String
username) {
    this.username = username;
  public String getPassword() {
    return password;
public void setPassword(String
password) {
    this.password = password;
This class is similar to one you would create if you were not using Validator, but this class extends ValidatorForm instead of ActionForm. This class also does not provide an implementation for ActionForm's empty reset( ) and validate( ) methods, because ValidatorForm does.
You configure this concrete Form Bean in the struts-config.xml file the same way you would a regular Form Bean:
  <form-bean name="logonForm"
The logical name given to the concrete Form Bean with the form tag's name attribute is the name you use when defining validations in the validation.xml file, as shown here.
<!DOCTYPE form-validation
PUBLIC "-//Apache Software Foundation//
       DTD Commons Validator Rules
       Configuration 1.0//EN"
    <form name="logonForm">
      <field property="username"
        <arg0 key="prompt.username"/>
Validator uses the value of the form tag's name attribute to match validation definitions to the name of the Form Bean to which they should be applied.
The second path you can choose when creating your Form Bean is to define a dynamic Form Bean in the struts-config.xml file, as follows:
  <form-bean name="logonForm"
    <form-property name="username"
    <form-property name="password"