JBoss.orgCommunity Documentation

Chapter 4. Actions

4.1. <a4j:ajax>
4.1.1. Reference data
4.2. <a4j:actionParam>
4.2.1. Basic usage
4.2.2. Interoperability
4.2.3. Reference data
4.3. <a4j:commandButton>
4.3.1. Basic usage
4.3.2. Reference data
4.4. <a4j:commandLink>
4.4.1. Basic usage
4.4.2. Reference data
4.5. <rich:componentControl>
4.5.1. Basic usage
4.5.2. Attaching to a component
4.5.3. Parameters and JavaScript
4.5.4. Timing
4.5.5. Reference data
4.6. <a4j:jsFunction>
4.6.1. Basic usage
4.6.2. Parameters and JavaScript
4.6.3. Reference data
4.7. <a4j:poll>
4.7.1. Timing options
4.7.2. Reference data
4.8. <a4j:push>
4.8.1. Timing options
4.8.2. Reference data

This chapter details the basic components that respond to a user action and submit an Ajax request.

The <a4j:ajax> component allows Ajax capability to be added to any non-Ajax component. It is placed as a direct child to the component that requires Ajax support. The <a4j:ajax> component uses the common attributes listed in Chapter 2, Common Ajax attributes.

Attaching JavaScript functions

When attaching the <a4j:ajax> component to non-Ajax JavaServer Faces command components, such as <h:commandButton> and <h:commandLink>, it is important to set disabledDefault="true". If this attribute is not set, a non-Ajax request is sent after the Ajax request and the page is refreshed unexpectedly.


The <a4j:actionParam> behavior combines the functionality of the JavaServer Faces (JSF) components <f:param> and <f:actionListener>.

The <a4j:commandButton> is similar to the JavaServer Faces (JSF) component <h:commandButton>, but additionally includes Ajax support. When the command button is clicked it submits an Ajax form, and when a response is received the command button can be dynamically rendered.

Figure 4.1. <a4j:commandButton>


The <a4j:commandButton> requires only the value and render attributes to function. The value attribute specifies the text of the button and the render attribute specifies which areas are to be updated. The <a4j:commandButton> uses the click event instead of the submit event, but otherwise uses all common Ajax attributes as listed in Chapter 2, Common Ajax attributes.

Set disabledDefault="true"

When attaching a JavaScript function to a <a4j:commandButton> with the help of a <rich:componentControl>, do not use the attachTo attribute of <rich:componentControl>. The attribute adds event handlers using Event.observe but <a4j:commandButton> does not include this event.

The <a4j:commandLink> is similar to the JavaServer Faces (JSF) component <h:commandLink>, but additionally includes Ajax support. When the command link is clicked it generates an Ajax form submit, and when a response is received the command link can be dynamically rendered.

Figure 4.2. <a4j:commandLink>


The <rich:componentControl> allows JavaScript API functions to be called on components after defined events. Initialization variants and activation events can be customized, and parameters can be passed to the target component.

The <a4j:jsFunction> component allows Ajax requests to be performed directly from JavaScript code, and server-side data to be invoked and returned in JavaScript Object Notation (JSON) format to use in client-side JavaScript calls.

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 periodically performs an Ajax request to the server, simulating "push" functionality. While it is not strictly pushing updates, the request is made to minimal code only, not to the JSF tree, checking for the presence of new messages in the queue. The request registers EventListener, which receives messages about events, but does not poll registered beans. If a message exists, a complete request is performed. This is different from the <a4j:poll> component, which performs a full request at every interval.