Thursday, January 19th, 2006

Apache Struts-Shale and Simple Ajax

Category: Java, Library, Remoting

David Geary has announced a new remoting package for Shale, the Java framework that sits on top of JavaServer Faces.

Now with one JS function you can access your managed beans on the server side:


  1. function zipChanged(zip) {
  2.    sendRequest("http://localhost:8080/struts-shale-usecases/" +
  3.                "dynamic/remoting$business/cityAndStateForZip.faces" +
  4.                "?zip=" + escape(zip),
  5.                processZipCodeSelection);
  6. }

The preceeding code uses an XMLHttpRequest object to invoke a URL on the server. Shale intercepts the URL and turns it into a cityAndStateForZip method call on a managed bean named remoting$business (the $ is a Shale convention that has no bearing here).

The remoting$business.cityAndStateForZip method calls FacesContext.responseComplete, which effectively halts the JSF lifecycle, and writes an XML response to the response stream. On the client, I have some JavaScript to parse that XML response and store the corresponding city and state values in the appropriate HTML elements in the page.

Posted by Dion Almaer at 9:37 am
Comment here

2.7 rating from 56 votes

Comments Here »

Comments feed TrackBack URI

Leave a comment

You must be logged in to post a comment.