JBoss.orgCommunity Documentation
This chapter details the basic components that respond to a user action and submit an Ajax request.
The <a4j:ajax>
behavior allows Ajax capability to be added to a non-Ajax component. The non-Ajax component must implement the ClientBehaviorHolder
interface for all the event attributes that support behavior rendering.
The <a4j:ajax>
behavior is placed as a direct child to the component that requires Ajax support.
Point the event
attribute to the standard JSF event that triggers the behavior. If the event
attribute is not defined, the behavior is triggered on the event that normally provides interaction behavior for the parent component.
Example 3.1. <a4j:ajax>
example
<h:panelGrid columns="2">
<h:inputText id="myinput" value="#{userBean.name}">
<a4j:ajax event="keyup" render="outtext" />
</h:inputText>
<h:outputText id="outtext" value="#{userBean.name}" />
</h:panelGrid>
client-behavior-renderer-type
: org.ajax4jsf.behavior.Ajax
behavior-id
: org.ajax4jsf.behavior.Ajax
handler-class
: org.richfaces.view.facelets.html.AjaxHandler
behavior-class
: org.ajax4jsf.component.behavior.AjaxBehavior
client-behavior-renderer-class
: org.ajax4jsf.renderkit.AjaxBehaviorRenderer
The <a4j:param>
behavior combines the functionality of the JavaServer Faces (JSF) components <f:param>
and <f:actionListener>
.
Basic usage of the <a4j:param>
requires three main attributes:
The value
attribute is the initial value of the parameter.
The assignTo
attribute defines the bean property. The property is updated if the parent command component performs an action event during the Process Request phase.
Example 3.2, “<a4j:param> example” shows a simple implementation along with the accompanying managed bean.
Example 3.2. <a4j:param>
example
<h:form id="form">
<a4j:commandButton value="Set name to Alex" reRender="rep">
<a4j:param name="username" value="Alex" assignTo="#{paramBean.name}"/>
</a4j:commandButton>
<h:outputText id="rep" value="Name: #{paramBean.name}"/>
</h:form>
public class ParamBean {
private String name = "John";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
When the name
parameter of the bean to Alex
, and displays the name in the output field.
The <a4j:param>
tag can be used with non-Ajax components in addition to Ajax components. This includes components which are working through the GET
request, such as the <h:link>
and <h:button>
components. In this way, data model values can also be updated without any Java code on the server side.
The converter
attribute can be used to specify how to convert the value before it is submitted to the data model. The property is assigned the new value during the Update Model phase.
If the validation of the form fails, the Update Model phase will be skipped and the property will not be updated.
Variables from JavaScript functions can be used for the value
attribute. In such an implementation, the noEscape
attribute should be set to true
. Using noEscape="true"
, the value
attribute can contain any JavaScript expression or JavaScript function invocation, and the result will be sent to the server as the value
attribute.
Example 3.3. Passing client-side parameters
<h:form>
<a4j:commandButton value="Show Screen Size" render="infoPanel">
<a4j:param name="w" value="screen.width"
assignTo="#{paramBean.screenWidth}" noEscape="true" />
<a4j:param name="h" value="screen.height"
assignTo="#{paramBean.screenHeight}" noEscape="true" />
</a4j:commandButton>
<h:panelGrid columns="2" id="infoPanel">
<h:outputText value="Width:" />
<h:outputText value="#{paramBean.screenWidth}" />
<h:outputText value="Height:" />
<h:outputText value="#{paramBean.screenHeight}" />
</h:panelGrid>
</h:form>
The command button triggers the <a4j:param>
behaviors and renders the output text. The <a4j:param>
behaviors pass client-side parameters for the screen width and height through the backing bean. These parameters are then used to populate the output text values.
Use the <a4j:actionListener>
tag to register an ActionListener
class on a parent action component. The class provided as a listener must implement the javax.faces.event.ActionListener
interface. Multiple listener methods can be registered on an action component in this way.
The <a4j:actionListener>
tag differs from the standard JSF tag by allowing a listener method to be defined instead of just a class. Use the listener
attribute to define the listener method.
The <a4j:commandButton>
component is similar to the JavaServer Faces (JSF) <h:commandButton>
component, but additionally includes Ajax support.
Button controls are typically used to perform complete form submissions for data storing. As a consequence, the <a4j:commandButton>
component has the execute="@form"
setting by default. To limit rendering to a different scope, redefine the execute
attribute.
The <a4j:commandButton>
requires only the value
attribute to function. Use the value
attribute to specify the text of the button.
By default, the <a4j:commandButton>
uses the click
event instead of the submit
event.
The <a4j:commandLink>
component is similar to the JavaServer Faces (JSF) <h:commandLink>
component, except that it includes plugged-in Ajax behavior.
Link controls are typically used to perform complete form submissions for data storing. As a consequence, the <a4j:commandLink>
component has the execute="@form"
setting by default. To limit rendering to a different scope, redefine the execute
attribute.
The <a4j:commandLink>
requires only the value
attribute to function. Use the value
attribute to specify the text of the link.
The <a4j:commandLink>
uses the click
event instead of the submit
event.
The <a4j:jsFunction>
component performs Ajax requests directly from JavaScript code and retrieves server-side data. The server-side data is returned in JavaScript Object Notation (JSON) format prior to the execution of any JavaScript code defined using the oncomplete
attribute.
The <a4j:jsFunction>
component requires the data
attribute. Use the data
attribute to define where the retrieved server-side data is stored.
Example 3.4, “<a4j:jsFunction> example” shows how an Ajax request can be initiated from the JavaScript and a partial page update performed. The JavaScript function can be invoked with the data returned by the Ajax response.
Example 3.4. <a4j:jsFunction>
example
<table width="400">
<tbody>
<tr>
<td>
<span onmouseover="updateName('Kate')"
onmouseout="updateName('')">Kate</span>
</td>
<td>
<span onmouseover="updateName('John')"
onmouseout="updateName('')">John</span>
</td>
<td>
<span onmouseover="updateName('Alex')"
onmouseout="updateName('')">Alex</span>
</td>
</tr>
<tr>
<td colspan="3">
Name: <b><h:outputText id="showname" value="#{functionBean.text}" /></b>
</td>
</tr>
</tbody>
</table>
<h:form>
<a4j:jsFunction name="updateName" render="showname">
<a4j:param name="name" assignTo="#{functionBean.text}"/>
</a4j:jsFunction>
</h:form>
The output text for the name is changed depending on which table cell the user hovers over with the mouse. The <a4j:jsFunction>
component manages the updating and display of the name.
The <a4j:jsFunction>
component allows the use of the <a4j:param>
component or the JavaServer Faces <f:param>
component to pass any number of parameters for the JavaScript function.
The <a4j:poll>
component allows periodical sending of Ajax requests to the server. It is used for repeatedly updating a page at specific time intervals.
The interval
attribute specifies the time in milliseconds between requests. The default for this value is 1000 ms (1 second).
The <a4j:poll>
component can be enabled and disabled using the enabled
attribute. Using Expression Language (EL), the enabled
attribute can point to a bean property to apply a particular attribute value.
The <a4j:push>
component performs real-time updates on the client side from events raised at the server side. The events are pushed out to the client through the Java Message Service (JMS). When the <a4j:push>
component is triggered by a server event, it can in turn cause Ajax updates and changes.
The <a4j:push>
component uses the Comet model for pushing data to the client.
RichFaces requires the use of the jquery-atmosphere.js
client plug-in for push functionality. Add the server-side Atmosphere framework as a build dependency to your project.
The JMS instance on the back-end must be configured to work with your <a4j:push>
components. Refer to the JBoss Application Server Administration Console Guide for details on managing JBoss Application Server through the Administration Console.
Example 3.5. JMS server configuration
This example describes the JMS server configuration required for a simple chat room. The chat room requires topics on the JMS server for the push functionality to check for new messages. Create a new JMS topic using the following settings:
Name: chat
JNDI name: /topic/chat
Use the default settings for other options.
Add a single role for the topic in the same form using the following settings:
Name: guest
Send: true
Consume: true
Create subscriber: true
Delete subscriber: true
Create durable subscriber: true
Delete durable subscriber: true
Ensure the Create durable subscriber and Delete durable subscriber options are set to true for push functionality. Durable subscriptions receive all events, including those events which were sent while the push component was not connected.
Refer to JMS Documentation for details on configuring the JMS Server.
With the JMS server configured, use the <a4j:push>
component's address
attribute to reference the topic on the JMS server that contains the pushed messages.
Example 3.6. Basic usage
<rich:list value="#{chatBean.users}" var="user" id="users" type="unordered">
#{user.nick}
</rich:list>
...
<a4j:push address="#{chatBean.listSubtopic}@chat"
onerror="alert(event.rf.data)">
<a4j:ajax event="dataavailable" render="users" execute="@none" />
</a4j:push>
public String getListSubtopic() {
return this.getUserName() + SUBTOPIC_SEPARATOR + channelName + "List";
}
@Override
protected void onUserList(String channel, User[] users) {
try {
getTopicsContext().publish(new TopicKey("chat", getListSubtopic()), null);
} catch (MessageException e) {
LOGGER.error(e.getMessage(), e);
}
}
@Override
protected void onJoin(String channel, String sender, String login, String hostname) {
try {
getTopicsContext().publish(new TopicKey("chat", getListSubtopic()), null);
Message messageObject = new Message("joined channel", sender,
DateFormat.getInstance().format(new Date()));
getTopicsContext().publish(new TopicKey("chat", getMessagesSubtopic()), messageObject);
} catch (MessageException e) {
LOGGER.error(e.getMessage(), e);
}
}
The example demonstrates a simple use of the <a4j:push>
component to manage a list of users in a chat room. The <a4j:push>
component refers to the #{chatBean.listSubtopic}@chat
address, which has been created on the JMS server. It then uses the sub-topics to separate messages across different topics.
When a new message arrives, the <a4j:ajax>
behavior causes the user list to update. If an error occurs, the user is alerted.
A push notification sent to the <a4j:push>
behavior will cause it to trigger any event handlers defined using the ondataavailable
event attribute.
The <a4j:push>
component should also include the onerror
event attribute to inform the user when an error has occurred with the push notifications.
Example 3.7. Handling a push notification
<a4j:push address="#{chatBean.messagesSubtopic}@chat"
onerror="alert(event.rf.data)"
ondataavailable="jQuery('<div/>').
prependTo('.#{chatBean.channelName}Output').text(
getMessageString(event.rf.data))" />
The example uses the dataavailable
event attribute with JavaScript to update messages in a chat room. The event.rf.data
parameter contains JMS message data serialized to JavaScript.