This page is for anyone looking for information on using the AJAX(Asynchronous Javascript + XML) methodology, specifically of course within the Struts domain.


To get things started, I have written an article and a fairly interesting (I think so!) example web app. Here is the relevant link:

The article gives some good introductory-level info on using the XMLHttpRequest object and explains a few bits of the sample web app.

The sample web app shows six different usages of the object, including dynamic sorting of a table, dynamic updating of a <select> element, dynamic loading of a <textarea> and an RSS feed parser (two versions in fact!).

Here are some diagrams to help you conceptualize how AJAX works:



Thanks to Jesse James Garrett at Adaptive Path for these pics (

It is important to note that AJAX is not a specific technology implementation, it is an approach, a technique, a way of looking at things. While it is true that the use of !XMLHttpRequest sending and receiving XML is the generally accepted way to do AJAX-like things, it is not the only way. While the above diagrams are accurate, they do not reflect this. No one should be under the impression that you have to deal in XML or that you have to use the XMLHttpRequest object at all, contrary to the meaning of the AJAX moniker :) (one could argue it isn't called AJAX at that point, but that is a debate for another day!)

In the end, an AJAX request is no different from any other HTTP request. Keeping this in mind, it is easy to see that virtually any AJAX library will work just fine with Struts (with some exceptions of course, and perhaps a few caveats here and there). The real difference is in what gets submitted with that request. It may just be a collection of request parameters, in which case you would literally write your Struts code the same as always, that code doesn't know the difference. It may be XML in the POST body. It may be JSON, which looks something like this:

{ "firstName":"Frank","lastName":"Zammetti" }

This is fairly trivial to parse on the server-side, and is even easier on the client:

eval("json = (" + INPUT_JSON + ")");

Assuming INPUT_JSON is the JSON shown above, you would then have access to a Javascript variable named json, from which you can retrieve pieces of data like so:

var firstName = json.firstName;
var lastName = json.lastName;

This simplicity, especially on the client-side (where parsing XML can be a bit expensive) is becoming quite popular. For more information on JSON, including a number of Java classes to help both parsing and creating JSON easily, see

However, the key point is that if its XML, JSON, or some other format you create, then you will of course have to parse it on the server and do something with it, which means you won't be able to use all of Struts capabilities, i.e., form auto-population, validation, etc.


There are literally hundreds of AJAX libraries out there now that cover all sorts of things, whether it is an abstraction layer on top of XMLHttpRequest (and possibly other implementations underneath that), AJAX-enabled widgets, and so on. All of them will work with Struts (there might be some caveats for some of them). Here are just a few of perhaps the most popular, and some info on them:

* The AjaxParts Taglib ( The beauty of APT is that it is completely declarative, just like Struts! Unlike most other AJAX libraries, APT is very much Java-only because it uses a custom taglib to do its work. You simply drop a tag onto a page to "attach" an AJAX event to an element on the page, you define how that event works via an XML config file, and that's it! No Javascript whatsoever to write! APT is powerful out of the box, in fact, most of the most common AJAX functions are included. If you need something more though, APT is fully extensible to allow you to do anything you want. Rick Reumann has written a very nice introductory article to using APT in a Struts app here: (note that APT used to be called AjaxTags, so don't be confused as you read this article!)

* Java Web Parts ( Aside from APT, JWP offers some things that might be of interest. One is a Javascript implementation of Commons Digester. It doesn't have all the features of it's big brother, but if your sending XML from the server, it might be worth looking at. Also of note is the RequestHelpers class, which provides some useful utility functions such as getPostBody(), which returns the contents of the requests' POST body as a String (good when your sending XML or JSON from the client for instance).

* Prototype ( Prototype does a lot more than just Java, it is really a useful Javascript library in general.

* Dojo ( Dojo is quickly becoming one of the most most Javascript libraries out there. Like Prototype, it not only does AJAX, but a ton more. It contains a number of GUI widgets, utility classes to do client-side persistent storage, DOM manipulation functions, Javascript collections implementations, and of course very solid AJAX functionality.

* DWR ( DWR is a very cool library that allows you to make remote method calls from Javascript on objects on your server, and the code looks like what you would write in Java! It also integrates with many popular frameworks, including Struts, JSF, Spring, Webwork and others.

* Scriptaculous ( This library is especially good when it comes to adding various effects to your pages. It also provides some handy Javascript unit testing code. Note that Scriptaculous uses Prototype under the covers.

* AjaxTags ( Note that this is different than the AjaxParts Taglib mentioned above (even though APT used to be called AjaxTags as noted). This AjaxTags is another taglib that provides more along the lines of AJAX widgets. It allows you to easily do many of the most common AJAX kinds of things through just the custom tags. Please do be aware that while it sounds similar, the focus of AjaxTags and APT are really quite different, so be sure to evaluate both if you are looking for a taglib-based solution.

Other Resources

* There is also a blog entry dealing with an Ajax/Struts approach here:

* A full-blown application to explorer: A Struts-based chat application using AJAX ( This application uses "naked" AJAX, i.e., no library, and demonstrates a number of different ways you can interact with Struts via AJAX.

* A great book on AJAX is Ajax In Action, published by Manning ( Not only will you learn a lot about AJAX, but about Javascript and client-side development in general. High recommended by everyone I know who has ever read it!

* You didn't think I wasn't going to plug my own book, did you? Here it is: Practical Ajax Projects with Java Tecnologies, published by Apress ( This book should appeal to the developers out there who learn best by having real code in front of them to play with and explore. We can read as many white papers as we want, as many introductory articles as we want, as many tutorials with contrived theoretical examples as we want, and we'll still be asking "ok, now how to I actually use this stuff in a real application?" Well, this book offers seven such applications! There are also the obligatory introductory chapters on AJAX, Javascript, Ant, Tomcat, JSP, Servlets, and more (although it's geared towards people who aren't absolute beginners).

AjaxStruts (last edited 2009-09-20 23:11:45 by localhost)