Sending an Ajax Request
To activate Ajax functionality, the web application must create an Ajax request and
send it to the server. The server then processes the request.
The application uses the attributes of the f:ajax tag listed in Table 11-1 to
create the Ajax request. The following sections explain the process of creating and sending
an Ajax request using some of these attributes.
Note -Behind the scenes, the jsf.ajax.request() method of the JavaScript resource library collects the
data provided by the f:ajax tag and posts the request to the
JavaServer Faces lifecycle.
Using the event Attribute
The event attribute defines the event that triggers the Ajax action. Some of
the possible values for this attribute are click, keyup, mouseover, focus, and
blur.
If not specified, a default event based on the parent component will be
applied. The default event is action for javax.faces.component.ActionSource components such as a
commandButton, and valueChange for javax.faces.component.EditableValueHolder components such as inputText. In the following example,
an Ajax tag is associated with the button component, and the event that
triggers the Ajax action is a mouse click:
Note -You may have noticed that the listed events are very similar to JavaScript
events. In fact, they are based on JavaScript events, but do not have
the on prefix.
For a command button, the default event is click, so that you do
not actually need to specify event="click" to obtain the desired behavior.
Using the execute Attribute
The execute attribute defines the component or components to be executed on the
server. The component is identified by its id attribute. You can specify more
than one executable component. If more than one component is to be executed,
specify a space-delimited list of components.
When a component is executed, it participates in all phases of the
request processing lifecycle except the Render Response phase.
The execute attribute can also be a keyword, such as @all, @none, @this, or
@form. The default value is @this, which refers to the component within which
the f:ajax tag is nested.
The following code specifies that the h:inputText component with the id value of
userNo should be executed when the button is clicked:
title="Type a number from 0 to 10:"
value="#{userNumberBean.userNumber}">
...
Using the immediate Attribute
The immediate attribute indicates whether user inputs are to be processed early in
the application lifecycle or later. If the attribute is set to true, events
generated from this component are broadcast during the Apply Request Values phase. Otherwise,
the events will be broadcast during the Invoke Applications phase.
If not defined, the default value of this attribute is false.
Using the listener Attribute
The listener attribute refers to a method expression that is executed on the
server side in response to an Ajax action on the client. The listener’s
javax.faces.event.AjaxBehaviorListener.processAjaxBehavior method is called once during the Invoke Application phase of the lifecycle.
In the following example, a listener attribute is defined by an f:ajax tag,
which refers to a method from the bean.
The following code represents the someaction method in mybean.
public void someaction(AjaxBehaviorEvent event) {
dosomething;
}