Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Client Side Address Validation in Struts 
 

Client Side Address Validation in Struts 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.

 

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">
         <arg key="AddressForm.name"/>
    </field>
    <field  property="address"
         depends="required">
         <arg key="AddressForm.address"/>
     </field>
     <field  property="emailAddress"
         depends="required">
        <arg key="AddressForm.emailAddress"/>
     </field>
</form>

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 <arg key=".."/> 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" %>

<html:html locale="true">
<head>
    <title><bean:message key="welcome.title"/></title>
<html:base/>
</head>
<body bgcolor="white">
<html:form action="/AddressJavascriptValidation" method="post" onsubmit="return validateAddressForm(this);">

<div align="left">
<p>
This application shows the use of Struts Validator.<br>
The following form contains fields that are processed by Struts Validator.<br> 
Fill in the form and see how JavaScript generated by Validator Framework validates the form.
</p>

<p>
<html:errors/>
</p>
<table>

<tr>
<td align="center" colspan="2">
<font size="4"><b>Please Enter the Following Details</b></font>
</tr>
<tr>
<td align="right">
<b>Name</b>
</td>
<td align="left">
<html:text property="name" size="30" maxlength="30"/>
</td>
</tr>
<tr>
<td align="right">
<b>Address</b>
</td>
<td align="left">
<html:text property="address" size="30" maxlength="30"/>
</td>
</tr>

<tr>
<td align="right">
<b>E-mail address</b>
</td>
<td align="left">
<html:text property="emailAddress" size="30" maxlength="30"/>
</td>
</tr>

<tr>
<td align="right">
<html:submit>Save</html:submit>
</td>
<td align="left">
<html:cancel>Cancel</html:cancel>
</td>
</tr>
</table>
</div>

<!-- Begin Validator Javascript Function-->
<html:javascript formName="AddressForm"/>
<!-- End of Validator Javascript Function-->

</html:form>
</body>
</html:html>

The code <html:javascript formName="AddressForm"/> is used to plug-in the Validator JavaScript.

Create the following entry in the struts-config.xml for the mapping the /AddressJavascriptValidation url for handling the form submission through AddressJavascriptValidation.jsp.

<action
     path="/AddressJavascriptValidation"
     type="roseindia.net.AddressAction"
     name="AddressForm"
     scope="request"
     validate="true"
     input="/pages/AddressJavascriptValidation.jsp">
     <forward name="success" path="/pages/success.jsp"/>
</action>

Add the following line in the index.jsp to call the form.

<li>
<html:link page="/pages/AddressJavascriptValidation.jsp">Client Side Validation for Address Form</html:link>
<br>
The Address Form that validates the data on the client side using Stuts Validator generated JavaScript.
</li>

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 navigate to the AddressJavascriptValidation.jsp page. Your browser should show the following out put.

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.

                         

» View all related tutorials
Related Tags: c programming apache web com development asp ui framework build application io components make sed get ip hook vi concepts

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

106 comments so far (
post your own) View All Comments Latest 10 Comments:

Can u please send me a understandably sample project using jsp, servlets, java script, java, stuts. Please....

Posted by Perinbarani on Sunday, 04.5.09 @ 10:53am | #86517

I want to fill dependent select by first select onchange event,I used AJAX in servlets,but how it is possible in action class without Page refresh

Posted by gunjan on Sunday, 12.21.08 @ 07:47am | #83012

In our work space i don't have this file .
Struts\strutstutorial\web\WEBINF\MessageResources.properties
I have included error.Name in ApplicationResources.properties.
In structs.config.xml this file is added.
all are done according to instruction..

It gives gives nullpointer exceptiop in _Addressform.jsp

Posted by GUnjan on Saturday, 12.13.08 @ 03:46am | #82709

<html:form action="/AddressJavascriptValidation" method="post" onsubmit="return validateAddressForm(this);">

where is the validateAddressForm(this) funtion?

Posted by Rm.Kulanthaivelu on Friday, 12.12.08 @ 04:37am | #82679

Hai plz answer my question

<html:submit property="delMaterial" styleClass="button3" onclick="return delMaterials('<%=count%>');">

i already save the value of count in codings.I want to pass the count value in function.What can i do for this?

Posted by Raja on Wednesday, 07.30.08 @ 16:48pm | #69976

Gettng error "Error 500 Cannot retrieve definition for form bean AddressForm "
After clicking the save button without entering any data. Any help is appreciated

Posted by Suzana on Tuesday, 07.29.08 @ 16:06pm | #69778

got object expected error in the following line:

<html:form action="/AddressJavascriptValidation" method="post" onsubmit="return validateAddressForm(this);">

Posted by heidey on Monday, 07.28.08 @ 17:31pm | #69556

hi,

I tried this example but getting error message with in page only.i want it to display as alert in message box. what i have to do to get that??

Posted by vasu on Thursday, 06.19.08 @ 13:19pm | #63849

<html:form action="/AddressJavascriptValidation" method="post" onsubmit="return validateAddressForm(this);">

where is the validateAddressForm(this) funtion?

Posted by rakesh on Thursday, 05.8.08 @ 18:27pm | #58924

Hello,

Here you do not need to define validateAddressForm(this) method. All you need to do is add vaidate to your bean form. here bean form name is AddressForm so it would be validateAddressForm(this).

Rajavardhan Sarkapally

Posted by Raj on Friday, 04.25.08 @ 04:44am | #57824

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.