Struts Validator Framework

This lesson introduces you the Struts Validator Framework.

Struts Validator Framework

Struts Validator Framework


This lesson introduces you the Struts Validator Framework. In this lesson you will learn how to use Struts Validator Framework to validate the user inputs on the client browser. 

Introduction to Validator Framework

Struts Framework provides the functionality to validate the form data. It can be use to validate the data on the users browser as well as on the server side. Struts Framework emits the java scripts and it can be used to validate the form data on the client browser. Server side validation of the form can be accomplished by sub classing your From Bean with DynaValidatorForm class. 

The Validator framework was developed by David Winterfeldt as third-party add-on to Struts. Now the Validator framework is a part of Jakarta Commons project and it can be used with or without Struts. The Validator framework comes integrated with the Struts Framework and can be used without doing any extra settings.

Using Validator Framework

Validator uses the XML file to pickup the validation rules to be applied to an form. In XML validation requirements are defined applied to a form. In case we need special validation rules not provided by the validator framework, we can plug in our own custom validations into Validator.

The Validator Framework uses two XML configuration files validator-rules.xml and validation.xml. The validator-rules.xml defines the standard validation routines, these are reusable and used in validation.xml. to define the form specific validations. The validation.xml defines the validations applied to a form bean.

Structure of validator-rule.xml

The validation-rules.xml is provided with the Validator Framework and it declares and assigns the logical names to the validation routines. It also contains the client-side javascript code for each validation routine. The validation routines are java methods plugged into the system to perform specific validations.

Following table contains the details of the elements in this file: 


Attributes and Description


This is the root node. It contains nested elements for all of the other configuration settings.


The validator details specified within this, are global and are accessed by all forms.


The validator element defines what validators objects can be used with the fields referenced by the formset elements.

The attributes are:

  • name: Contains a logical name for the validation routine

  • classname: Name of the Form Bean class that extends the subclass of ActionForm class

  • method: Name of the method of the Form Bean class

  • methodParams: parameters passed to the method

  • msg:Validator uses Struts' Resource Bundle mechanism for externalizing error messages. Instead of having hard-coded error messages in the framework, Validator allows you to specify a key to a message in the file that should be returned if a validation fails. Each validation routine in the validator-rules.xml file specifies an error message key as value for this attribute.

  • depends: If validation is required, the value here is specified as 'required' for this attribute.

  • jsFunctionName: Name of the javascript function is specified here. 


Contains the code of the javascript function used for client-side validation. Starting in Struts 1.2.0 the default javascript definitions have been consolidated to commons-validator.  The default can be overridden by supplying a <javascript> element with a CDATA section, just as in struts 1.1.

The Validator plug-in (validator-rules.xml) is supplied with a predefined set of commonly used validation rules such as Required, Minimum Length, Maximum length, Date Validation, Email Address validation and more. This basic set of rules can also be extended with custom validators if required.

Structure of validation.xml 0

This validation.xml configuration file defines which validation routines that is used to validate Form Beans. You can define validation logic for any number of  Form Beans in this configuration file. Inside that definition, you specify the validations you want to apply to the Form Bean's fields. 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.


Attributes and Description 1


This is the root node. It contains nested elements for all of the other configuration settings

global 2

The constant details are specified in <constant> element within this element.


Constant properties are specified within this element for pattern matching. 3


Name of the constant property is specified here

constant-value 4

Value of the constant property is specified here.


This element contains multiple <form> elements 5


This element contains the form details.
The attributes are:

:Contains the form name. Validator uses this logical name to map the validations to a Form Bean defined in the struts-config.xml file

field 6

This element is inside the form element, and it defines the validations to apply to specified Form Bean fields.

The attributes are:

  • property: Contains the name of a field in the specified Form Bean

  • depends: Specifies the logical names of validation routines from the validator-rules.xml file that should be applied to the field. 7


A key for the error message to be thrown incase the validation fails, is specified here

var 8

Contains the variable names and their values as nested elements within this element.


The name of the criteria against which a field is validated is specified here as a variable 9


The value of the field is specified here


Example of  form in the validation.xml file:

<!-- An example form -->
<form name="logonForm">
<field property="username"
   <arg key="logonForm.username"/>
   <field property="password"
   <arg key="logonForm.password"/>

The <html:javascript> tag to allow front-end validation based on the xml in validation.xml. For  example the code: <html:javascript formName="logonForm" dynamicJavascript="true" staticJavascript="true" /> generates the client side java script for the form "logonForm" as defined in the validation.xml file. The <html:javascript> when added in the jsp file generates the client site validation script.

In the next lesson we will create a new form for entering the address and enable the client side java script with the Validator Framework. 1