Welcome!

AJAX & REA Authors: Elizabeth White, Trevor Parsons, ChandraShekar Dattatreya, Liz McMillan, David H Deans

Related Topics: AJAX & REA, Java

AJAX & REA: Article

Struts Validations Framework Using AJAX

Real-time data validation is one of the advantages of AJAX technology

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.
First we need to get the servlet mapping configuration from the Web descriptor before moving onto the process mapping. After invoking the process, the application will prepare the instance of the form that inherits from the AjaxForm class. The processing manages AJAX validation and should check to ensure that the request from the client isn't using the struts validation framework. Other processes to perform during the request are process populate for collecting the information sent by the client into the action form, and process validation by using the method from the existing Struts validation framework that already exists in the parent class of AjaxValidationRequestProcessor (TilesRequestProcessor).

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.
Configuring the Taglib Definition
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.

More Stories By 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.

Comments (9) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
hanan mahmoud 07/27/08 03:39:45 AM EDT

hi all
its realy great article and it helped me to understand validation in ajax better ,i just hope if any one have sample code about that topic because till now i don't know how to find one and also i don't know where to put the code samples in the article so plz help me

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

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

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.
It would be great to see some sample application (with source code of course) implementing this approach. I even thought that I would find as a source code for the article, but seems like JDJ doesn't have such practice. Sonnie, can you provide that? It could be an archive hosted somewhere, here or at some free hosting, e.g. http://rapidshare.de/

@CloudExpo Stories
SYS-CON Media announced today that Skytap blog on "DevOps Journal" exceeded 84,000 story reads. DevOps Journal is focused on this critical enterprise IT topic in the world of cloud computing. DevOps Journal brings valuable information to DevOps professionals who are transforming the way enterprise IT is done. Noel Wurst is the managing content editor at Skytap. Skytap provides SaaS-based dev/test environments to the enterprise. Skytap solution removes the inefficiencies and constraints that comp...
ScriptRock makes GuardRail, a DevOps-ready platform for configuration monitoring. Realizing we were spending way too much time digging up, cataloguing, and tracking machine configurations, we began writing our own scripts and tools to handle what is normally an enormous chore. Then we took the concept a step further, giving it a beautiful interface and making it simple enough for our bosses to understand. We named it GuardRail after its function - to allow businesses to move fast and stay sa...
SYS-CON Events announced today that Cloudian, Inc., the leading provider of hybrid cloud storage solutions, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Cloudian, Inc., is a Foster City, California - based software company specializing in cloud storage software. The main product is Cloudian, an Amazon S3-compliant cloud object storage platform, the bedrock of cloud computing systems, that enables c...
SYS-CON Media announced today that Sematext launched a popular blog feed on DevOps Journal with over 6,000 story reads over the weekend. DevOps Journal is focused on this critical enterprise IT topic in the world of cloud computing. DevOps Journal brings valuable information to DevOps professionals who are transforming the way enterprise IT is done. Sematext is a globally distributed organization that builds innovative Cloud and On Premises solutions for performance monitoring, alerting an...
SYS-CON Events announced today Isomorphic Software, the global leader in high-end, web-based business applications, will exhibit at SYS-CON's DevOps Summit 2015 New York, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Isomorphic Software is the global leader in high-end, web-based business applications. We develop, market, and support the SmartClient & Smart GWT HTML5/Ajax platform, combining the productivity and performance of traditional desktop software ...
Leysin American School is an exclusive, private boarding school located in Leysin, Switzerland. Leysin selected an OpenStack-powered, private cloud as a service to manage multiple applications and provide development environments for students across the institution. Seeking to meet rigid data sovereignty and data integrity requirements while offering flexible, on-demand cloud resources to users, Leysin identified OpenStack as the clear choice to round out the school's cloud strategy. Additional...
The BPM world is going through some evolution or changes where traditional business process management solutions really have nowhere to go in terms of development of the road map. In this demo at 15th Cloud Expo, Kyle Hansen, Director of Professional Services at AgilePoint, shows AgilePoint’s unique approach to dealing with this market circumstance by developing a rapid application composition or development framework.
SYS-CON Events announced today that Windstream, a leading provider of advanced network and cloud communications, has been named “Silver Sponsor” of SYS-CON's 16th International Cloud Expo®, which will take place on June 9–11, 2015, at the Javits Center in New York, NY. Windstream (Nasdaq: WIN), a FORTUNE 500 and S&P 500 company, is a leading provider of advanced network communications, including cloud computing and managed services, to businesses nationwide. The company also offers broadband, p...
In high-production environments where release cycles are measured in hours or minutes — not days or weeks — there's little room for mistakes and no room for confusion. Everyone has to understand what's happening, in real time, and have the means to do whatever is necessary to keep applications up and running optimally. DevOps is a high-stakes world, but done well, it delivers the agility and performance to significantly impact business competitiveness.
"Our premise is Docker is not enough. That's not a bad thing - we actually love Docker. At ActiveState all our products are based on open source technology and Docker is an up-and-coming piece of open source technology," explained Bart Copeland, President & CEO of ActiveState Software, in this SYS-CON.tv interview at DevOps Summit at Cloud Expo®, held Nov 4-6, 2014, at the Santa Clara Convention Center in Santa Clara, CA.
The Internet of Things is not new. Historically, smart businesses have used its basic concept of leveraging data to drive better decision making and have capitalized on those insights to realize additional revenue opportunities. So, what has changed to make the Internet of Things one of the hottest topics in tech? In his session at @ThingsExpo, Chris Gray, Director, Embedded and Internet of Things, discussed the underlying factors that are driving the economics of intelligent systems. Discover ...
"BSQUARE is in the business of selling software solutions for smart connected devices. It's obvious that IoT has moved from being a technology to being a fundamental part of business, and in the last 18 months people have said let's figure out how to do it and let's put some focus on it, " explained Dave Wagstaff, VP & Chief Architect, at BSQUARE Corporation, in this SYS-CON.tv interview at @ThingsExpo, held Nov 4-6, 2014, at the Santa Clara Convention Center in Santa Clara, CA.
The major cloud platforms defy a simple, side-by-side analysis. Each of the major IaaS public-cloud platforms offers their own unique strengths and functionality. Options for on-site private cloud are diverse as well, and must be designed and deployed while taking existing legacy architecture and infrastructure into account. Then the reality is that most enterprises are embarking on a hybrid cloud strategy and programs. In this Power Panel at 15th Cloud Expo (http://www.CloudComputingExpo.com...
Verizon Enterprise Solutions is simplifying the cloud-purchasing experience for its clients, with the launch of Verizon Cloud Marketplace, a key foundational component of the company's robust ecosystem of enterprise-class technologies. The online storefront will initially feature pre-built cloud-based services from AppDynamics, Hitachi Data Systems, Juniper Networks, PfSense and Tervela. Available globally to enterprises using Verizon Cloud, Verizon Cloud Marketplace provides a one-stop shop fo...
The move in recent years to cloud computing services and architectures has added significant pace to the application development and deployment environment. When enterprise IT can spin up large computing instances in just minutes, developers can also design and deploy in small time frames that were unimaginable a few years ago. The consequent move toward lean, agile, and fast development leads to the need for the development and operations sides to work very closely together. Thus, DevOps become...
SYS-CON Events announced today that IDenticard will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. IDenticard™ is the security division of Brady Corp (NYSE: BRC), a $1.5 billion manufacturer of identification products. We have small-company values with the strength and stability of a major corporation. IDenticard offers local sales, support and service to our customers across the United States and Canada...
SYS-CON Events announced today that AIC, a leading provider of OEM/ODM server and storage solutions, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. AIC is a leading provider of both standard OTS, off-the-shelf, and OEM/ODM server and storage solutions. With expert in-house design capabilities, validation, manufacturing and production, AIC's broad selection of products are highly flexible and are conf...

ARMONK, N.Y., Nov. 20, 2014 /PRNewswire/ --  IBM (NYSE: IBM) today announced that it is bringing a greater level of control, security and flexibility to cloud-based application development and delivery with a single-tenant version of Bluemix, IBM's

AppZero has announced that its award-winning application migration software is now fully qualified within the Microsoft Azure Certified program. AppZero has undergone extensive technical evaluation with Microsoft Corp., earning its designation as Microsoft Azure Certified. As a result of AppZero's work with Microsoft, customers are able to easily find, purchase and deploy AppZero from the Azure Marketplace. With just a few clicks, users have an Azure-based solution for moving applications to the...
“In the past year we've seen a lot of stabilization of WebRTC. You can now use it in production with a far greater degree of certainty. A lot of the real developments in the past year have been in things like the data channel, which will enable a whole new type of application," explained Peter Dunkley, Technical Director at Acision, in this SYS-CON.tv interview at @ThingsExpo, held Nov 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA.