This Confluence has been LDAP enabled, if you are an ASF Committer, please use your LDAP Credentials to login. Any problems file an INFRA jira ticket please.

Child pages
  • AjaxStruts
Skip to end of metadata
Go to start of metadata


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 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!)

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 does a lot more than just Java, it is really a useful Javascript library in general.

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

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.

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

  • No labels