| HTML Tutorials |
|
|
| XML Tutorials |
|
|
| Browser Scripting |
|
|
| Server Scripting |
|
|
| .NET (dotnet) |
|
|
| Multimedia |
|
|
| Web Building |
|
|
| Java Tutorials |
|
|
| Programming Langauges |
|
|
| Soft Skills |
|
|
|
| Understanding The Struts Validator Framework |
|
Struts Validator Framework
|
|
To validate the form data,Struts Framework provides
the functionality.It can be use to validate the data on client side as well as on the server side. Struts Framework can be
used to validate the form data on the client
browser and emits java scripts.By sub classing your From Bean
with the DynaValidatorForm class,server side validation of the form can
be accomplished .
David Winterfeldt developed the Validator framework
as third-party add-on to the Struts.Now the Validator
framework can be used with or without the Struts and it is a part of Jakarta Commons project
.The Validator framework can be used without doing
any extra settings and comes integrated with
Struts Framework.
|
|
Using Validator Framework
|
|
To pickup validation rules to be
applied to an form,validator uses the XML
file. In XML validation requirements are applied to the form and aslo can be defined.In validator framework,we can plug
in our own custom validations into Validator.
The Validator Framework
uses two XML configuration files called
validator-rules.xml and validation.xml.
Standard validation routines are defined by the validator-rules.xml
and used in validation.xml. To define
form specific validations,validation.xml is used.To define the validations applied to a form bean,the validation.xml
|
|
Structure of validator-rule.xml
|
|
The validation-rules.xmldeclares and assigns
the logical names to the validation routines and is provided with Validator Framework.
For each validation routine,it also contains client-side javascript code .
To perform specific validations,the validation routines are the java methods plugged into the system .
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 which is also known as validator-rules.xml is supplied with the predefined set of commonly used validation rules
such as Required, Minimum Length, Maximum length, Date Validation, Email Address validation and more.
If required,this basic set of rules can also be extended with the custom validators .
|
|
Structure of validation.xml
|
|
This validation.xml configuration file define which validation routines that is used to validate Form Beans and also you can define validation logic for any number of Form Beans in this configuration file .
Inside that definition, you specify validations you want to apply to the Form Bean's fields and the definition 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.
|
|
The following table shows the elements of the validation.xml 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 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 is as follows:
|
<!-- 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>
|
|
|
To allow front-end validation based on xml
in validation.xml,the <html:javascript> tag is used.In the validation.xml file,for example the code:
<html:javascript formName="logonForm"
dynamicJavascript="true" staticJavascript="true"
/> generate the
client side java script for the form "logonForm"
. The <html:javascript>
generates the client site validation script when added in jsp file.
|
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords:
struts validator framework,regular expression validator,web application framework,struts web application,xml validator,struts validator tutorial,struts tutorial,struts jakarta,struts validator requiredif,struts validator example,struts validator xml,struts apache,apache framework,struts validation
|
|
| HTML Quizes |
|
|
| XML Quizes |
|
|
| Browser Scripting Quizes |
|
|
| Server Scripting Quizes |
|
|
| .NET (dotnet) Quizes |
|
|
| Multimedia Quizes |
|
|
| Web Building Quizes |
|
|
| Java Quizes |
|
|
| Programming Langauges Quizes |
|
|
| Soft Skills Quizes |
|
|
|