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: 

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

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.

Element

Attributes and Description

1

form-validation

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

Constant properties are specified within this element for pattern matching.

3

constant-name

Name of the constant property is specified here

constant-value

4

Value of the constant property is specified here.

formset

This element contains multiple <form> elements

5

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

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

arg

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.

var-name

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

9

var-value

The value of the field is specified here

 

0

Example of  form in the validation.xml file:

<!-- An example form -->
<form name="logonForm">
<field property="username"
   depends="required">
   <arg key="logonForm.username"/>
  </field>
   <field property="password"
   depends="required,mask">
   <arg key="logonForm.password"/>
   <var>
  <var-name>mask</var-name>
  <var-value>^[0-9a-zA-Z]*$</var-value>
  </var>
  </field>
  </form>

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

 

     

Tutorials

  1. Validations using Struts 2 Annotations
  2. Struts 1.1 Tutorials
  3. CRUD application in hibernate annotation
  4. Understanding Spring Struts Hibernate DAO Layer
  5. DAO Layer explained
  6. Developing Forgot Password Form
  7. Welcome to the Apache Struts Tutorial
  8. Developing Simple Struts Tiles Application
  9. Understanding Struts Controller
  10. Struts Hibernate Integration
  11. Developing Struts PlugIn
  12. Developing Struts Hibernate and Spring Based Login/Registration Application
  13. Struts File Upload and Save
  14. Struts 2 Features
  15. Struts 2 - History of Struts 2
  16. Struts 2 Architecture - Detail information on Struts 2 Architecture
  17. Download and Installing Struts 2
  18. Struts 2 Hello World Application Example, Learn how to develop Hello World application in struts 2.
  19. Developing JSP, Java and Configuration for Hello World Application
  20. Struts Configuration file - struts.xml
  21. Introduction to Struts 2 Tags
  22. Struts Logic Tags: An Introduction
  23. Logic Empty Tag (...)
  24. Logic Equal Tag (...)
  25. Logic greaterEqual Tag (... )
  26. Logic LessEqual Tag (...)
  27. Logic Match Tag (...)
  28. Logic Present Tag (...)
  29. Struts2 Actions
  30. Static Parameter
  31. Accessing Session Object
  32. Access Request and Response
  33. Control Tags-If / Else If / Else
  34. Append Tag (Control Tags) Example
  35. Generator Tag (Control Tags) Example
  36. Generator Tag (Control Tags) Using Count Attributes
  37. Generator Tag (Control Tags) Using an Iterator with Id Attributes
  38. Iterator Tag (Control Tags) Example
  39. Merge Tag (Control Tags) Example
  40. Subset Tag (Control Tags) Example