Dear Wiki user, You have subscribed to a wiki page or wiki category on "Struts Wiki" for change notification.
The following page has been changed by FrankZammetti: http://wiki.apache.org/struts/AjaxStruts ------------------------------------------------------------------------------ - This page is for anyone looking for information on using the Ajax (Asynchronous Javascript + XML) methodology, specifically of course within the Struts domain. + === Intro === - It is my hope that people will add different implementations that they come up with so that others can see how this powerful technique can be applied. If you use Ajax in a project in a neat way, please feel free to post what you did here! + This page is for anyone looking for information on using the AJAX(Asynchronous Javascript + XML) methodology, specifically of course within the Struts domain. + + === Basics === To get things started, I have written an article and a fairly interesting (I think so!) example web app. Here is the relevant link: - url:http://www.omnytex.com/articles + http://www.omnytex.com/articles 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: - Hope this is useful to someone! :) - - I have also written a taglib that allows you to quickly and easily add Ajax functionality to a JSP with NO coding on your part (it's all declarative, just like Struts)... there originally was a Strtus-only version (an extended version of the HTML taglib) which is still available on the Struts Apps Sourceforge page. However, that version is no longer being developed and has been superceeded by the Ajax Tags sub-component of the Java Web Parts project (url:http://javawebparts.sourceforge.net). This version of the taglib, which is very similar to the original Struts-only version, is not tied to Struts at all and has a little more flexibility out-of-the-box. inline:ajax-fig1_small.png inline:ajax-fig2_small.png - Thanks to Jesse James Garrett at Adaptive Path for these pics, url:http://www.adaptivepath.com/publications/essays/archives/000385.php + Thanks to Jesse James Garrett at Adaptive Path for these pics (http://www.adaptivepath.com/publications/essays/archives/000385.php) - There is also a blog entry dealing with an Ajax/Struts approach here: url:http://www.researchkitchen.co.uk/blog/archives/60 + 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!) - It is worth noting 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 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. + + === Libraries === + + 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 (http://javawebparts.sourceforge.net) + 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: http://www.learntechnology.net/struts-ajax-crud.do (note that APT used to be called AjaxTags, so don't be confused as you read this article!) + + * Java Web Parts (http://javawebparts.sourceforge.net) + 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 (http://prototype.conio.net) + Prototype does a lot more than just Java, it is really a useful Javascript library in general. + + * Dojo (http://dojotoolkit.org) + 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 (http://getahead.ltd.uk/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 (http://script.aculo.us) + 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 (http://ajaxtags.sourceforge.net) + 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: 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. + + * A great book on AJAX is Ajax In Action, published by Manning (http://www.amazon.com/gp/product/1932394613/sr=8-1/qid=1153807209/ref=pd_bbs_1/104-5102147-9017526?ie=UTF8). 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 (http://www.amazon.com/gp/product/1590596951/ref=pd_rvi_gw_3/104-5102147-9017526?%5Fencoding=UTF8&v=glance&n=283155). 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). +