Author Biography
Based in Dublin, Ireland, Paul Browne has been consulting in Enterprise Java with
FirstPartners.net for almost 7 years. When not promoting the Red Piranha (Search and
Knowledge Management) and NoUnit (Java Development Process) projects , he can be
found blogging online. This article was originally published on the Sun Java Website.
Ajax is the latest revolution in web development circles, allowing rich dynamic interfaces deployed
within a normal web browser. Struts has been one of the de-facto standards for Java-Web development
for a number of years, with a large amount of applications already deployed. This article is going to
show you how to combine the richness of an Ajax user interface to your existing Struts applications.
This article shows a simple and elegant way to do this by includ- Whitepapers, Consulting and
ing a couple of lines of Javascript on your JSP (Java Server) Products at
pages. While we show how to re-use existing Struts Actions, the
techniques are equally applicable to the Java-Web Framework of
your choice. The method proposed will also allow a move to the
next version of Struts (Shale) or Java Server Faces (JSF) in the fu-
Enterprise Project Rescue
ture.
Business Knowledge
Management
What is Ajax?
Development Process
Ajax is a term standing for 'Asynchronous Javascript and XML'. It Security, Performance and
is a technique, rather than a framework (such as Struts). The reas- Architecture
on for the 'buzz' around both it and Web 2.0 is that it allows web
Dynamic Web 2.0 Solutions
pages to behave less like flat documents and more like dynamic
GUI apps that users might expect from their desktop environ-
ments. Ajax techniques can be used for all recent browsers (in- ©Firstpartners.net 2005. Article may be
reproduced in full with this notice.
cluding Internet Explorer and Netscape/Mozilla). It is already used
by (among others) Microsoft (for it's Outlook web client) and Google (for its Maps and Mail applica-
tions).
Ajax 101
Ajax is similar to existing Dynamic HTML techniques, with the addition of a 'background' call to the
server to get new / updated information as required. The mechanics of Ajax have already been covered
in detail elsewhere - take a look at the resources at the bottom of this article. As an 'Ajax 101' the min-
imum you need to know is:
1. The XMLHttpRequest (or Microsoft.XMLHTTP ActiveX object if you are using Internet
Explorer). These objects can be called from the Javascript on your web page. They
allow you to request content from your web server as a background call (i.e. the screen
does not 'go blank' as usually happens during a form submit).
2. The content that the XMLHttpRequest and Microsoft.XMLHTTP objects return can be treated
as either XML or plain text. Javascript (on your web page) can then update the page
with this new content as required.
3. The whole process can be triggered by the usual Javascript events - onclick, onchange,
onblur etc.
To use simply add the retrieveURL() function to the onclick() / on- Dynamic Web 2.0 Solutions
Change() method of the event you wish to trigger the screen up-
date. ©Firstpartners.net 2005. Article may be
reproduced in full with this notice.
// Non-IE browsers
req = new XMLHttpRequest();
req.onreadystatechange = processStateChange;
try {
req.open("GET", url, true);
} catch (e) {
alert("Server Communication Problem\n"+e);
}
req.send(null);
} else if (window.ActiveXObject) {
// IE
if (req.readyState == 4) { // Complete
if (req.status == 200) { // OK response
} else {
alert("Problem with server response:\n " _
+ req.statusText);
}
}
}
function replaceExistingWithNewHtml _
(newTextElements){
3. This Javascript function makes a (background) call to the Struts Action on the server,
passing in all the form variables in a way that Struts will understand.
4. This Javascript function also sets the name of a 2nd Javascript function which will be
called when the server response is finished. In this case it is set to the
processStateChange()method
• Within the JSP, use Struts <logic:equal> or JSTL tags to decide if we need to render a
section of HTML or not.
An updated version of this project, with Ajax enabled, can be downloaded here struts-Ajax.zip
Conclusion
Ajax techniques promise to completely revolutionise how we build and use web applications. This art-
icle showed a simple technique to add Ajax behaviour to existing Struts applications. It allows us to re-
use our existing investment, not only in code but also in developer skills. As a nice by product, it also
allows us to write cleaner, more reusable, Java Struts applications.
Resources
• Sample code for this article
• Definition of Ajax
• Article: Using Ajax to catch Javascript errors
• Ajax and DWR (Java.net Article)
• BEA Ajax article
• IBM article on using Ajax with Webservices
• O'Reilly Article - Ruby on Rails
• DWR - Direct Web Remoting Project
• Struts Framework Project
• JSF - Java Server Faces Project