Struts Validator Framework

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:

Element Attributes and Description
form-validation This is the root node. It contains nested elements for all of the other configuration settings.
global The validator details specified within this, are global and are accessed by all forms.
validator 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 ApplicationResources.properties 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.

javascript 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
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.

Element Attributes and Description
form-validation This is the root node. It contains nested elements for all of the other configuration settings
global The constant details are specified in <constant> element within this element.
constant Constant properties are specified within this element for pattern matching.
constant-name Name of the constant property is specified here
constant-value Value of the constant property is specified here.
formset This element contains multiple <form> elements
form This element contains the form details.
The attributes are:
name
: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 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.

arg A key for the error message to be thrown incase the validation fails, is specified here
var Contains the variable names and their values as nested elements within this element.
var-name The name of the criteria against which a field is validated is specified here as a variable
var-value The value of the field is specified here
Example of form in the validation.xml file:

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.
Client Side Address Validation in Struts

In this lesson we will create JSP page for entering the address and use the functionality provided by Validator Framework to validate the user data on the browser. Validator Framework emits the JavaScript code which validates the user input on the browser. To accomplish this we have to follow the following steps:
1. Enabling the Validator plug-in: This makes the Validator available to the system.
2. Create Message Resources for the displaying the error message to the user.
3. Developing the Validation rules We have to define the validation rules in the validation.xml for the address form. Struts Validator Framework uses this rule for generating the JavaScript for validation.
4. Applying the rules: We are required to add the appropriate tag to the JSP for generation of JavaScript.
5. Build and test: We are required to build the application once the above steps are done before testing.
Enabling the Validator plug- in
To enable the validator plug-in open the file struts-config.xml and make sure that following line is present in the file.

Creating Message Resources
Message resources are used by the Validator Framework to generate the validation error messages. In our application we need to define the messages for name, Address and E-mail address. Open the Struts\strutstutorial\web\WEB-INF\MessageResources.properties file and add the following lines:
AddressForm.name=Name
AddressForm.address=Address
AddressForm.emailAddress=E-mail address
Developing Validation rules
In this application we are adding only one validation that the fields on the form should not be blank. Add the following code in the validation.xml.

The above definition defines the validation for the form fields name, address and emailAddress. The attribute depends=”required” instructs the Validator Framework to generate the JavaScript that checks that the fields are not left blank. If the fields are left blank then JavaScript shows the error message. In the error message the message are taken from the key defined in the <arg key=”..”/> tag. The value of key is taken from the message resources (Struts\strutstutorial\web\WEB-INF\MessageResources.properties).
Applying Validation rules to JSP
Now create the AddressJavascriptValidation.jsp file to test the application. The code for AddressJavascriptValidation.jsp is as follows:

The code <html:javascript formName=”AddressForm”/> is used to plug-in the Validator JavaScript.
Create the following entry in the struts-config.xml for the mapping the /AddressJavascriptValidation url for handling the form submission through AddressJavascriptValidation.jsp.

Add the following line in the index.jsp to call the form.

Building Example and Testing
To build and deploy the application go to Struts\strutstutorial directory and type ant on the command prompt. This will deploy the application. Open the browser and navigate to the AddressJavascriptValidation.jsp page. Your browser should show the following out put.

struts_validator

If the fields are left blank and Save button is clicked, browser shows the error message.
In this lesson you learned how to use Struts Validator Framework to validate the form on client browser.

   

Comments are closed