Sending an AJAX Request
a4j:support
Sends an AJAX request based on a DHTML event supported by the parent component. In this example, the AJAX request will be triggered after the user types a character in the text box:
| a4j:support |
<h:inputText value=”#{echoBean.text}”>
<a4j:support event=”onkeyup” action=”#{echoBean.count}”
reRender=”echo, cnt”/>
</h:inputText>
<h:outputText id=”echo” value=”Echo: #{echoBean.text}”/>
<h:outputText id=”cnt” value=”Count: #{echoBean.textCount}”/></td>
|
a4j:support can be attached to any html tag that supports DHTML events, such as:
<table cellpadding="0" cellspacing="0"> <tr> <td class="light_blue">a4j:support</td> </tr> <tr> <td class="light_cream">
<h:selectOneRadio value=”#{colorBean.color}”>
<f:selectItems value=”#{colorBean.colorList}” />
<a4j:support event=”onclick” reRender=”id” />
</h:selectOneRadio></td>
</table>
a4j:commandButton, a4j:commandLink
Similar to h:commandButton and h:commandLink but with two major differences. They trigger an AJAX request and allow partial JSF component tree rendering.
The request goes through the standard JSF life cyle. During the Render Response, only components whose client ids are listed in the reRender attribute (echo, count) are rendered back the the browser.
| a4j:commandButton, a4j:commandLink |
<h:inputText value=”#{echoBean.text}”/>
<h:outputText id=“echo” value=“Echo: #{echoBean.text}”/>
<h:outputText id=“cnt” value=“Count: #{echoBean.textCount}”/>
<a4j:commandButton value=“Submit” action=“#{echoBean.count}”
reRender=“echo, cnt”/>
|
Basic Concepts, Continued
When the response is received, the browser DOM is updated with the new data i.e ‘RichFaces is neat’ and ‘17’.

a4j:commandLink works exactly the same but renders a link instead of a button.
a4j:poll
Enables independent periodic polling of the server via an AJAX request. Polling interval is defined by the interval attribute and enable/disable polling is configured via enabled attribute (true|fase).
| a4j:poll |
<a4j:poll id=”poll” interval=”500” enabled=”#{pollBean.enabled}”
reRender=”now” />
<a4j:commandButton value=”Start” reRender=”poll”
action=”#{pollBean.start}” />
<a4j:commandButton value=”Stop” reRender=”poll”
action=”#{pollBean.stop}” />
<h:outputText id=”now” value=”#{pollBean.now}” />
|
| a4j:poll |
public class PollBean {
private Boolean enabled=false; // setter and getter
public void start () {enabled = true;}
public void stop () {enabled = false;}
public Date getNow () {return new Date();}
}
|
a4j:jsFunction
Allows sending an AJAX request directly from any JavaScript function (built-in or custom).
| a4j:jsFunction |
<td onmouseover=”setdrink(‘Espresso’)”
onmouseout=”setdrink(‘’)”>Espresso</td>
...
<h:outputText id=”drink” value=”I like #{bean.drink}” />
<a4j:jsFunction name=”setdrink” reRender=”drink”>
<a4j:actionparam name=”param1” assignTo=”#{bean.drink}”/>
</a4j:jsFunction>
|
When the mouse hovers or leaves a drink, the setdrink() JavaScript function is called. The function is defined by an a4j:jsFunction tag which sets up the AJAX call. It can call listeners and perform partial page rendering. The drink parameter is passed to the server via a4j:actionparam tag.
a4j:push
a4j:push works similarly to a4j:poll; however, in order to check the presence of a message in a queue, it only makes a minimal HEAD request(ping-like) to the server without invoking the JSF life cycle. If a message exists, a sandard JSF request is sent to the server.
Partial View (Page) Rendering
There are two ways to perform partial view rendering when AJAX requests return.
ReRender Attribute
Most RichFaces components support the reRender attribute to define the set of client ids to reRender.
| Attribute |
Can bind to |
| reRender/td> |
Set, Collection, Array, comma-delimited String |
ReRender can be set statically as in the examples above or with EL:
<a4j:commandLink reRender=”#{bean.renderControls}”/>
Basic Concepts, Continued
It’s also possible to point to parent components to rerender all child components:
<a4j:commandLink value=”Submit” reRender=”panel” />
<h:panelGrid id=”panel”>
<h:outputText />
<h:dataTable>...</h:dataTable>
</h:panelGrid>
In the example above the child components of the outputPanel will be rerendered when the commandLink is submitted.
a4j:outputPanel
All child components of an a4j:outputPanel will be rerendered automatically for any AJAX request.
<a4j:commandLink value=”Submit” />
<a4j:outputPanel ajaxRendered=”true”>
<h:outputText/>
<h:dataTable></h:dataTable>
</a4j:outputPanel>
In the example above the child components of the outputPanel will be rerendered when the commandLink is submitted.

If ajaxRendered=”false” (default) the a4j:outputPanel behaves just like h:panelGroup.
To limit rendering to only components set in the reRender attribute, set limitToList=”true”. In this example, only h:panelGrid will be rendered:
<a4j:commandLink reRender=”panel” limitToList=”true”/>
<h:panelGrid id=”panel”>
<h:dataTable>...</h:dataTable>
</h:panelGrid>
<a4j:outputPanel ajaxRendered=”true”>
lt;h:dataTable>...</h:dataTable>
</a4j:outputPanel>
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}