| By Sonny Hastomo | Article Rating: |
|
| April 6, 2007 10:00 AM EDT | Reads: |
92,990 |
Real-time data validation is one of the advantages of AJAX technology. By applying this technology, the struts validation framework will enrich the struts MVC and move the Web application closer to the desktop application.
The validation framework is used to validate fields. There are many ways to do validation on a Web application. It falls into two categories: server-side and client-side. A struts validation framework is one of the best frameworks for a Java-based Web application environment. It can configure the application using server-side validation and employ the error message that renders on the validation process invoked during the request processing time, or it can do client-side validation by using the JavaScript rendered on the requested page.
AJAX is a JavaScript technology that can asynchronously call the server and fetch the XML documents that are so popular lately. One of its uses is real-time data validation.
This article is concerned with enriching the existing struts validation framework with AJAX. A few components, such as a controller, have to be developed to select the validation framework and render the specific format message for the client side and a taglib to handle the error message rendering.
Prerequisites
You'll need a Windows system with Eclipse and the Tomcat application server. Make sure that the MSXML 3.0 ActiveX object is registered on your operating system. You'll also need the Struts library (http://struts.apache.org) and the JDOM library (www.jdom.org) for XML development (see Figure 1 and Figure 2).
Server-Side Scenario
StrustsActionServlet
We have to extend the class from org.apache.struts.action.ActionServlet to get the servletMapping variable that stores the information on how the extension will be formatted for action classes into the action path as a browser address. When the code is added, we have to configure web.xml as a Web application descriptor for the application server.
The web.xml configuration:
...
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>net.sf.struts.servlet.StrutsActionServlet</servlet-class>
...
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
...
The StrutsActionServlet Java code looks like this:
public class StrutsActionServlet extends the ActionServlet.
{
public String getServletMapping() {
return this.servletMapping;
}
}
AjaxValidationRequestProcessor
To support the existing Struts framework in the first step, we have to extend the RequestProcessor from the Struts package. We have to customize the request processor because we have to distinguish how we're going to do the validation - by using the existing Struts framework or the AJAX concept - and because we'll be making a contract between the server and client on how to interpret the message. In the message rendering we'll use the XML format, which is a good media messaging format. The definition of the XML format we'll apply is:
XML Format
<?xml version="1.0" encoding="UTF-8"?>
<message>
<identity name=messageAreaId>
<description>
MessageValue
</description>
</identity>
</message>
Description
- Identity is the ID for the client JavaScript to acknowledge where the message will be placed.
- Description is the result after the error message rendering from the server side.
The validation process from the TilesRequestProcessor will invoke all the validation based on the struts validation framework and store the action errors into the request. What we need is to parse the action errors into pieces and generate the XML message validation that will be sent to the client side. Since we want to change the behavior of how the validation will be backed, the validation process should check the indicator of the validation framework being used (see Figure 3).
Generate the XML messages using JDOM as the processing engine. As shown in Figure 4, when the process validation is invoked and the condition of the validation framework is equal with the AJAX validation framework, the process will continue to populate the error message and build the XML message validation.
ErrorMessageHandler
This class handles the functionality of the XML message builder. This Java class will be building the XML message based on the identity and description property. After the caller invokes the buildXMLMessage, it will prepare the document and set the root element of the XML message. This class also has an addNextXMLMessage function to add more validation messages into the XML (see Listing 1).
The process method will set the content type of the response as "text/xml" and send the XML message as a string. The function of the process on the AjaxValidationRequestProcessor code will look like Listing 2.
The processValidation method will populate the action errors and construct the message based on the format contract of the XML for the client. The function of processValidation on AjaxValidationRequestProcessor code will be look like:
...
ActionErrors errors = (ActionErrors) request.getAttribute(Globals.ERROR_KEY);
Locale locale = (Locale) request.getAttribute(Globals.LOCALE_KEY);
generateXMLMessage(errors, identity, locale, sbXMLMessage);
...
Client-Side Scenario
Build the Taglib Component
- AjaxJavaScriptLibraryTag: The taglib component to render the JavaScript function at the client side for basic XMLHTTP controller functions.
- AjaxErrorHtmlRenderTag: The taglib component to render the area of the error message at the JSP page.
After developing the taglib component, we need to configure the taglib tld file as shown in Listing 3.
Build JSP and Struts Configuration
To simulate the result of the validation processing, we first need to build the presentation layer by incorporating the taglib that we've build. In this case I'm trying to give an example validation by using the validation rules component from Struts, and also the validation that comes from the form itself. Prepare five textboxs under the JSP page. The first to fourth textbox are using the validation rule configuration, and the fifth textbox is using the validate process from the action form. Other than that, we also need a submit button to simulate that after submitting the form, the existing struts validation are still working without AJAX. The user interface will look like Figure 5.
Published April 6, 2007 Reads 92,990
Copyright © 2007 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
About Sonny Hastomo
Sonny Hastomo is an IT architect at Sun Microsystems, Indonesia, for the telecommunication industry division. His currently is focusing on providing solution design, sizing, implementation, consulting services, and quality support to customers in their evaluation of their IT challenges.
![]() |
hanan mahmoud 07/27/08 03:39:45 AM EDT | |||
hi all |
||||
![]() |
Commenter 05/17/06 01:40:52 PM EDT | |||
I just need examples, and I can´t find it here. This article is weak. |
||||
![]() |
SYS-CON Belgium News Desk 05/02/06 03:27:56 PM EDT | |||
Struts Validations Framework Using AJAX |
||||
![]() |
SYS-CON Australia News Desk 05/02/06 01:44:38 PM EDT | |||
Real-time data validation is one of the advantages of AJAX technology. By applying this technology, the struts validation framework will enrich the struts MVC and move the Web application closer to the desktop application. |
||||
![]() |
AJAX News Desk 05/02/06 12:55:46 PM EDT | |||
AjaxWorld: Struts Validations Framework Using AJAX |
||||
![]() |
Rajesh 05/02/06 12:17:24 PM EDT | |||
The article was really nice.It would have been better if the source code was included. Thanks |
||||
![]() |
waikit 02/08/06 04:11:11 AM EST | |||
can I have a sample code of the example shown in this article, please regards |
||||
![]() |
SYS-CON Australia News Desk 01/27/06 02:21:45 PM EST | |||
Real-time data validation is one of the advantages of AJAX technology. By applying this technology, the struts validation framework will enrich the struts MVC and move the Web application closer to the desktop application. |
||||
![]() |
Artem Vasiliev 01/26/06 06:18:56 AM EST | |||
Sonnie, thank you for your article, it brings pretty interesting idea. |
||||
- Performance of Java Compilers: An Empirical Study
- Java Kicks Ruby on Rails in the Butt
- Ulitzer’s Amazing First 30 Days in Public Beta
- 1st Annual Government IT Expo: Call for Papers Deadline July 15
- REA Is Where RIA Becomes the Norm
- Why an Application Grid?
- Will Ulitzer Dominate News Content on The Web? -Gartner
- Clear Toolkit 4: The Road Map
- Profiling Netbeans within Amazon EC2
- Java Persistence on the Grid: Approaches to Integration
- Performance of Java Compilers: An Empirical Study
- Java Kicks Ruby on Rails in the Butt
- Developing Rich Client Applications Using Swing - II
- The Right Time for Real Time Java
- Xpress Suite Adds Automatic Java to iPhone Conversion
- Initial Thoughts on IBM Acquisition of Sun Microsystems
- Ulitzer’s Amazing First 30 Days in Public Beta
- 1st Annual Government IT Expo: Call for Papers Deadline July 15
- Maximizing Java Performance with Bespoke Programming
- Pet Store with JavaFX 1.0.- Part I
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- The i-Technology Right Stuff
- JavaServer Faces (JSF) vs Struts
- Rich Internet Applications with Adobe Flex 2 and Java
- Java vs C++ "Shootout" Revisited
- Bean-Managed Persistence Using a Proxy List
- Reporting Made Easy with JasperReports and Hibernate
- What's New in Eclipse?
- Creating a Pet Store Application with JavaServer Faces, Spring, and Hibernate








































