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 (http://www.adaptivepath.com/publications/essays/archives/000385.php)
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:
This is fairly trivial to parse on the server-side, and is even easier on the client:
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 http://www.json.org
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:
- Java Web Parts (http://javawebparts.sourceforge.net)
- Prototype (http://prototype.conio.net)
- Dojo (http://dojotoolkit.org)
- Scriptaculous (http://script.aculo.us)
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.
- There is also a blog entry dealing with an Ajax/Struts approach here: http://www.researchkitchen.co.uk/blog/archives/60
- A full-blown application to explorer: A Struts-based chat application using AJAX (http://struts.sourceforge.net/ajaxchat/index.html) This application uses "naked" AJAX, i.e., no library, and demonstrates a number of different ways you can interact with Struts via AJAX.