JBoss.orgCommunity Documentation

Chapter 3. Actions

3.1. <a4j:ajax>
3.1.1. Basic usage
3.1.2. Reference data
3.2. <a4j:param>
3.2.1. Basic usage
3.2.2. Interoperability
3.2.3. Passing client-side parameters
3.2.4. Reference data
3.3. <a4j:actionListener>
3.4. <a4j:commandButton>
3.4.1. Basic usage
3.4.2. Reference data
3.5. <a4j:commandLink>
3.5.1. Basic usage
3.5.2. Reference data
3.6. <a4j:jsFunction>
3.6.1. Basic usage
3.6.2. Parameters and JavaScript
3.6.3. Reference data
3.7. <a4j:poll>
3.7.1. Timing options
3.7.2. Reference data
3.8. <a4j:push>
3.8.1. Basic usage
3.8.2. Handling a push notification
3.8.3. Reference data

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:param> behavior combines the functionality of the JavaServer Faces (JSF) components <f:param> and <f:actionListener>.

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.

Figure 3.1. <a4j:commandButton>


The <a4j:commandButton> component executes the complete form

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:commandLink> component is similar to the JavaServer Faces (JSF) <h:commandLink> component, except that it includes plugged-in Ajax behavior.

Figure 3.2. <a4j:commandLink>


The <a4j:commandLink> component executes the complete form

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: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.


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 <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.


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.