Thursday, January 19th, 2006
Apache Struts-Shale and Simple Ajax
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:
-
-
function zipChanged(zip) {
-
sendRequest("http://localhost:8080/struts-shale-usecases/" +
-
"dynamic/remoting$business/cityAndStateForZip.faces" +
-
"?zip=" + escape(zip),
-
processZipCodeSelection);
-
}
-
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.












Leave a comment