Activate your free membership today | Log-in

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:

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

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

++---
2.7 rating from 50 votes

Comments Here »

Comments feed TrackBack URI

Leave a comment

You must be logged in to post a comment.