JSF 2: Ajax Events and Errors
JSF 2's Ajax support includes a very basic facility to listen for events and errors within JSF's client side processing code. It's envisioned that this will primarily be used by component developers that wish to monitor events - for instance, turning an image yellow when there's an outstanding Ajax request, and black when there isn't.
So, without further ado, here's a few snips of code from Mojarra's ajax-queue demo:
In a page with some Ajax calls, we have the following field:
A simple textarea, not even hooked into the backend server data model.
Then in our javascript (for the demo, in a separately loaded file, though it could just as easily be in page) we have:
When run, you'll see a stream of data going through the textarea as ajax events happen elsewhere in the page. Try out the demo to see more. (Again, it's in the Mojarra codebase, under jsf-demo/ajax-queue)
What's happening: first we define a JavaScript function, place it in a variable, and then call two JSF ajax api functions:
That data object, along with the functions themselves, are defined in Section 14.4 of the JSF 2 specification. I'll outline a little of what's in there here, but be sure to check it out for the full scoop - it's a pretty easy to read and understand part of the spec.
For Events, there are three named events: begin, complete, and success.
This means that for a normal request, all three will be called, while if there is some sort of error, then only begin and complete will be called.
For Errors, there are four named errors possible.
The data payload consists of:
<h3> Status:</h3>
<textarea id="statusArea" cols="40" rows="10" readonly="readonly" />
var statusUpdate = function statusUpdate(data) {
var statusArea = document.getElementById("statusArea");
var text = statusArea.value;
text = text + "Name: "+data.source.id;
if (data.type === "event") {
text = text +" Event: "+data.name+"\n";
} else { // otherwise, it's an error
text = text + " Error: "+data.name+"\n";
}
statusArea.value = text;
};
// Setup the statusUpdate function to hear all events on the page
jsf.ajax.addOnEvent(statusUpdate);
jsf.ajax.addOnError(statusUpdate);
addOnEvent and addOnError. These functions use the statusUpdate function as their callback, passing the data object as it's first parameter.
So, when to you need to use this functionality? Probably not very often. But since it's not really documented anywhere else yet, I wanted to get some description out there for what it does.






Comments
AJAX Exceptions on the Server side
by alfonx2 - 2010-08-18 10:38
Hi and thanks for your great blog. Can you give a hint on how to catch/process Excetions that happen during an AJAX request on the server side? All I get is logging output, like:com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback visitSEVERE: javax.el.ELException: /some.xhtmlThanks, Steve
by driscoll - 2009-05-26 14:16
The tutorial will be done for Java EE 6, just as for Java EE 5 - at the end of the process. In the meaning, there's javadoc, pdldoc, renderdoc and jsdoc, all available as part of the spec bundle.by b_faissal - 2009-05-23 06:17
I want to ask you if there is a reference documentation for JSF 2.0 as it's for JSF 1.2 (at SUN JEE tutorials) ? Thanks