Ajax Integration with JSF 2.0
In JSF 2.0, An Ajax engine will be implemented on the client side. It will be responsible for the following:
1. Sending an Ajax request to the server.
2. Receiving the partial response then updating or inserting new element(s) in the client DOM.
At the client, the Ajax engine (which will be represented in the (Ajax.js) file) will construct the Ajax request which will contain the following:
1. The name and value of the source element that triggers the Ajax request.
2. A single parameter that says that this request is an Ajax request. This parameter is called (javax.faces.ajax.partial) and its value is set to true.
3. The encoded view state.
4. The set of client ids that must be processed in the partial view execute phases, and the set of client ids that must be processed in the partial view render phase.
At the server, the request will be recognized whether it is an Ajax request or not by checking the (javax.faces.ajax.partial) request parameter. If it is an Ajax request, a partial view execute and a partial view render will be applied on the components sent in the Ajax request (check the previous step 4). Finally the server will generate the content data in a language understandable by the client (may be XML or JSON).
At the client, the Ajax client engine will receive the server response, and would interpret it to know whether to update or insert new elements to the DOM.
Notes:
1. The FacesContext.isAjaxRequest() method can be used to check whether the current request is an Ajax request or not.
2. All of the Ajax engine requests should be asynchronous, and to ensure their correct execution order, the request will be enqueued (once the component initiate it) and dequeued (once the request completed execution) from a request queue.
A simple example to illustrate the idea (From the JSF 2.0 specs ED2):
<h:commandbutton id="submit" value="submit"
onclick="javax.faces.ajax.ajaxRequest(this, event,
转自:http://www.jroller.com/HazemBlog/entry/ajax_integration_with_jsf_2
{execute:'submit',render:'outtext'}); return false;" />
This will execute the partial view execute on the ‘submit’ button and the partial view render on the ‘outtext’ component at the end.
Finally, I would like to mention that the script resources in JSF 2.0 are relocatable meaning that you can control their encoding place by setting the target attribute of the resource component. The @ResourceDependency annotation can be also used before the component renderer class to achieve the same functionality.