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
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:
|
|
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:
<!-- 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.

|
Current Comments
62 comments so far (post your own) View All Comments Latest 10 Comments:nice tutorial for the beginers
Posted by vamsikrishna on Tuesday, 02.5.08 @ 15:13pm | #47284
yes, you can
greetings,
Hamid
Posted by Hamid Komairi on Friday, 01.18.08 @ 10:44am | #45495
Hi..
I have 1 queston.that is how to do user defined validations in validation frame work of struts frame work?
Posted by sekhar on Saturday, 12.15.07 @ 17:53pm | #42500
It is veryuseful to the beginner. And it gives the clear picture of the usage.
hates of to author....
Posted by Jilla Anil on Thursday, 11.29.07 @ 17:55pm | #40896
it,s really helped me lot.
Thanks
Posted by veeresh on Tuesday, 11.20.07 @ 11:30am | #37928
this is not appropriate place to post this query,but i am in urgent need.can we use commons validator without struts framework.
Posted by chaitanya on Saturday, 11.17.07 @ 16:35pm | #37625
i'm a beginner!
very goog for me!
thanks!
Posted by Dai Phuc on Wednesday, 10.17.07 @ 08:12am | #34246
Its good.But it need some flow for examples.this will be help ful for beginners.
Posted by lakshmi on Wednesday, 08.22.07 @ 16:22pm | #23908
It is good for beginers , but it requred some more examples.
Posted by susanta sahoo on Wednesday, 08.1.07 @ 20:33pm | #22440
hi
i have to use validator for validating the client side data suppose when validating the form data it throw some error that is username is not valid that error displayed on one jsp page that is routine work but i want to throw that error in alert box using java script it is possiple.
Posted by dhandapani on Tuesday, 07.31.07 @ 15:10pm | #22299