Client Side Address Validation using Struts: A Step-by-Step Guide, Lecture notes of Java Programming

Learn how to create a jsp page for entering an address and use the struts validator framework to validate user input on the browser. This tutorial covers enabling the validator plug-in, creating message resources, developing validation rules, and applying rules to jsp. Follow the steps to build and test the application.

Typology: Lecture notes

2011/2012

Uploaded on 08/09/2012

dhanyaa
dhanyaa 🇮🇳

4.7

(3)

60 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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.
<!-- Validator plugin -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
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.
<!-- Address form Validation-->
<form name="AddressForm">
<field property="name"
depends="required">
docsity.com
pf3

Partial preview of the text

Download Client Side Address Validation using Struts: A Step-by-Step Guide and more Lecture notes Java Programming in PDF only on Docsity!

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

<%@ taglib uri="/tags/struts-bean" prefix="bean" %> <%@ taglib uri="/tags/struts-html" prefix="html" %>

<bean:message key="welcome.title"/>

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 the form.