Download - Struts JavaScript Validation
-
7/28/2019 Struts JavaScript Validation
1/13
Struts javaScript Validation
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 thatfollowing 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, Addressand E-mail address. Open the Struts\strutstutorial\web\WEB-
INF\MessageResources.properties file and add the following lines:
AddressForm.name=NameAddressForm.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 shouldnot be blank. Add the following code in the validation.xml.
-
7/28/2019 Struts JavaScript Validation
2/13
depends="required">
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 messageare taken from the key defined in the 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:
This application shows the use of Struts Validator.
The following form contains fields that are processed by Struts Validator.
Fill in the form and see how JavaScript generated by Validator Framework validates theform.
-
7/28/2019 Struts JavaScript Validation
3/13
Please Enter the Following Details
Name
Address
E-mail address
Save
Cancel
http://www.roseindia.net/struts/address_struts_validator.shtmlhttp://www.roseindia.net/struts/address_struts_validator.shtmlhttp://www.roseindia.net/struts/address_struts_validator.shtmlhttp://www.roseindia.net/struts/address_struts_validator.shtml -
7/28/2019 Struts JavaScript Validation
4/13
The code is used to plug-in theValidator 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.
Client Side Validationfor Address Form
The Address Form that validates the data on the client side using Stuts Validatorgenerated JavaScript.
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 navigateto the AddressJavascriptValidation.jsp page. Your browser should show the
following out put.
http://www.roseindia.net/struts/address_struts_validator.shtmlhttp://www.roseindia.net/struts/address_struts_validator.shtml -
7/28/2019 Struts JavaScript Validation
5/13
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.
Apache Struts has changed the way we develop a Web application. Since its inception as
an MVC architecture, Struts has been extensively used in J2EE world to develop robust,extendable and effective web applications.
Introduction to Struts Validation Framework
One of the important features of Struts framework is Struts Validation
frameworkthat performs validation on incoming form data. Validation framework was
-
7/28/2019 Struts JavaScript Validation
6/13
introduced by David Winterfeldt as an external plugin to Struts framework. Its
functionality has since been split so that validator can serve as the basis for a independant
component and is now part ofJakarta Commons.
The Struts frameworks simple validation interface alleviates much of the headache
associated with handling data validation, allowing you to focus on validation code andnot on the mechanics of capturing data and redisplaying incomplete or invalid data.
In order to do form validation without Validator framework, one has to use validate()method of the form bean (ActionForm class) to perform this task. Also one has to handle
error messages during manual validation. Lot of fields that we validate require same logic
to validate them, hence code is unneccessarily duplicated (if not managed properly).
Validation framework comes with set of useful routines to handle form validationautomatically and it can handle both server side as well as client side form validation.
If certain validation is not present, you can create your own validation logic and plug it
into validation framework as a re-usable component.
Validator uses two XML configuration files to determine which validation routinesshould be installed and how they should be applied for a given application, respectively.
The first configuration file, validator-rules.xml, declares the validation routines that
should be plugged into the framework and provides logical names for each of thevalidations. The validator-rules.xml file also defines client-side JavaScript code for each
validation routine. Validator can be configured to send this JavaScript code to the
browser so that validations are performed on the client side as well as on the server side.
The second configuration file, validation.xml, defines which validation routines should
be applied to which Form Beans. The definitions in this file use the logical names ofForm 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.
Using the Validator framework involves enabling the Validator plug-in, configuringValidators two configuration files, and creating Form Beans that extend the Validators
ActionForm subclasses. The following sections explain in detail how to configure and
use Validator.
Create a Struts project
Create a struts web application project. I assume you have working environment set for aStruts project. If not then go through the tutorial: Creating Struts application using
Eclipse and create a struts project.
Create Form Beans
http://viralpatel.net/blogs/2008/12/tutorial-creating-struts-application-in-eclipse.htmlhttp://viralpatel.net/blogs/2008/12/tutorial-creating-struts-application-in-eclipse.htmlhttp://viralpatel.net/blogs/2008/12/tutorial-creating-struts-application-in-eclipse.htmlhttp://viralpatel.net/blogs/2008/12/tutorial-creating-struts-application-in-eclipse.html -
7/28/2019 Struts JavaScript Validation
7/13
Create a form bean in your project called CustomerForm and copy following code in it.
view source
print?01 packagenet.viralpatel.struts.validation.form;
02
03 importorg.apache.struts.validator.ValidatorForm;
04
05 publicclassCustomerForm extendsValidatorForm {
06 07 privateString name;08 privateString telephone;09 privateString email;10 privateintage;
11
12 publicString getName() {13 returnname;14 }
15
16 publicvoidsetName(String name) {
17 this.name = name;18 }
19
20 publicString getTelephone() {21 returntelephone;22 }
23
24 publicvoidsetTelephone(String telephone) {25 this.telephone = telephone;
26 }
27
28 publicString getEmail() {29 returnemail;30 }
31
32 publicvoidsetEmail(String email) {33 this.email = email;
34 }
35
http://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#viewSource%23viewSourcehttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#printSource%23printSourcehttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#about%23abouthttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#viewSource%23viewSourcehttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#printSource%23printSourcehttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#about%23about -
7/28/2019 Struts JavaScript Validation
8/13
36 publicintgetAge() {37 returnage;38 }
39
40 publicvoidsetAge(intage) {41 this.age = age;
42 }
43 }
We will use this validator plugin to validate this form. Note that the form bean is
extended from class ValidatorForm and not ActionForm as we generally do in Struts
project.
Add Validator Plug-in in struts-config.xml
In order to use Validator in our project we need to configure it in struts-config.xml file.
For this add following code in your struts-config.xml file.
view source
print?1
2 3
6
This definition tells Struts to load and initialize the Validator plug-in for your application.Upon initialization, the plug-in loads the comma-delimited list of Validator config files
specified by the pathnames property. Each config files path should be specified by use ofa Web application-relative path, as shown in the previous example.
Define validations for the form
Create a file validation.xml in your applications WEB-INF directory. And copy following
content in it.
http://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#viewSource%23viewSourcehttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#printSource%23printSourcehttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#about%23abouthttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#viewSource%23viewSourcehttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#printSource%23printSourcehttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#about%23about -
7/28/2019 Struts JavaScript Validation
9/13
view source
print?01 02
05
06
07
08 telephoneFormat
09 ^\d{5,10}$
10
11
12
13
14 15 16
17 18 19 20 21
22 min
23 1
24
25
26 max
27 125
28
29
30 31 32 33
34 mask
35 ${telephoneFormat}
36
37 38 39 40 41
42
43
44
http://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#viewSource%23viewSourcehttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#printSource%23printSourcehttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#about%23abouthttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#viewSource%23viewSourcehttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#printSource%23printSourcehttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#about%23about -
7/28/2019 Struts JavaScript Validation
10/13
In the above xml file, we have defined the rules for form validation. Note that we are
validating form CustomerForm and the fields being validated are name, age, telephone
and email. tag defines the validation for a property of form. We can specifydifferent rules like required, integer, email, intRange, mask etc in depends attribute of
field tag..
Also you can define constants that can be reused in the validation xml using global
constants tag.
Struts-config.xml entry for the action
Following is the entry in struts-config.xml file which maps the Action to our Validator
form.
view source
print?01
02 04
05 ...
06 ...
07 ...
08
09 ...
10
13 14 15
16 ...
17
Configuring ApplicationResources.properties
Struts validation framework uses externalization of the error messages. The messages are
stored in a property file (ApplicationResource.properties) and are referred by the keyvalues. Copy following in your ApplicationResource.properties (or
MessageResource.properties).
view source
print?01 label.name= Name
http://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#viewSource%23viewSourcehttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#printSource%23printSourcehttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#about%23abouthttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#viewSource%23viewSourcehttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#printSource%23printSourcehttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#about%23abouthttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#viewSource%23viewSourcehttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#printSource%23printSourcehttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#about%23abouthttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#viewSource%23viewSourcehttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#printSource%23printSourcehttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#about%23about -
7/28/2019 Struts JavaScript Validation
11/13
02 label.email= Email
03 label.telephone= Telephone
04 label.age= Age
05
06 # general error msgs
07 errors.header=08 errors.prefix=09 errors.suffix=
10 errors.footer=
11 errors.invalid={0} is invalid.
12 errors.maxlength={0} can not be greater than {1} characters.
13 errors.minlength={0} can not be less than {1} characters.
14 errors.range={0} is not in the range {1} through {2}.
15 errors.required={0} is required.
16 errors.byte={0} must be an byte.
17 errors.date={0} is not a date.
18 errors.double={0} must be an double.
19 errors.float={0} must be an float.20 errors.integer={0} must be an integer.
21 errors.long={0} must be an long.
22 errors.short={0} must be an short.
Create JSP to display the form
Create a JSP file and copy following content in it.
view source
print?01 02
03
04
05 Struts Validation Framework example.
06
07
08
09 10
11
12
13
14 15 16 17 18 19
http://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#viewSource%23viewSourcehttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#printSource%23printSourcehttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#about%23abouthttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#viewSource%23viewSourcehttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#printSource%23printSourcehttp://viralpatel.net/blogs/2009/01/struts-validation-framework-tutorial-example-validator-struts-validation-form-validation.html#about%23about -
7/28/2019 Struts JavaScript Validation
12/13
20 21 22 23 24 25
26
27
28
29
30
31
Running the application
We are done with our application. Now execute it from any web container (Tomcat in my
case) and open in browser.
Enter any invalid value in the form and press submit.
-
7/28/2019 Struts JavaScript Validation
13/13