13.3 Using Ajax with Facelets
As mentioned in the previous section, JavaServer Faces technology supports Ajax by using a built-in JavaScript resource library that is provided as part of the JavaServer Faces core libraries. This built-in Ajax resource can be used in JavaServer Faces web applications in one of the following ways.
-
By using the
f:ajaxtag along with another standard component in a Facelets application. This method adds Ajax functionality to any UI component without additional coding and configuration. -
By using the JavaScript API method
jsf.ajax.request()directly within the Facelets application. This method provides direct access to Ajax methods and allows customized control of component behavior.
13.3.1 Using the f:ajax Tag
The f:ajax tag is a JavaServer Faces core tag that provides Ajax functionality to any regular UI component when used in conjunction with that component. In the following example, Ajax behavior is added to an input component by including the f:ajax core tag:
<h:inputText value="#{bean.message}">
<f:ajax />
</h:inputText>
In this example, although Ajax is enabled, the other attributes of the f:ajax tag are not defined. If an event is not defined, the default action for the component is performed. For the inputText component, when no event attribute is specified, the default event is valueChange. Table 13-1 lists the attributes of the f:ajax tag and their default actions.
Table 13-1 Attributes of the f:ajax Tag
| Name | Type | Description |
|---|---|---|
|
|
|
A |
|
|
|
A |
|
|
|
A |
|
|
|
A |
|
|
|
The name of the listener method that is called when a |
|
|
|
The name of the JavaScript function that handles UI events. |
|
|
|
The name of the JavaScript function that handles errors. |
|
|
|
A |
The keywords listed in Table 13-2 can be used with the execute and render attributes of the f:ajax tag.
Table 13-2 Execute and Render Keywords
| Keyword | Description |
|---|---|
|
|
All component identifiers |
|
|
The form that encloses the component |
|
|
No component identifiers |
|
|
The element that triggered the request |
Note that when you use the f:ajax tag in a Facelets page, the JavaScript resource library is loaded implicitly. This resource library can also be loaded explicitly as described in Loading JavaScript as a Resource.
