6.17.  < a4j:poll >

6.17.1. Description

The <a4j:poll> component allows periodical sending of Ajax requests to a server and is used for a page updating according to a specified time interval.

Table 6.33. a4j : poll attributes

Attribute NameDescription
actionMethodBinding pointing at the application action to be invoked, if this UIComponent is activated by you, during the Apply Request Values or Invoke Application phase of the request processing lifecycle, depending on the value of the immediate property
actionListenerMethodBinding pointing at method accepting an ActionEvent with return type void
ajaxSingleif "true", submits ONLY one field/link, instead of all form controls
bindingThe attribute takes a value-binding expression for a component property of a backing bean
bypassUpdatesIf "true", after process validations phase it skips updates of model beans on a force render response. It can be used for validating components input
dataSerialized (on default with JSON) data passed on the client by a developer on AJAX request. It's accessible via "data.foo" syntax
enabledEnable/disable polling
eventsQueueName of requests queue to avoid send next request before complete other from same event. Can be used to reduce number of requests of frequently events (key press, mouse move etc.)
focusid of element to set focus after request completed on client side
idEvery component may have a unique id that is automatically created if omitted
ignoreDupResponsesAttribute allows to ignore an Ajax Response produced by a request if the newest 'similar' request is in a queue already. ignoreDupResponses="true" does not cancel the request while it is processed on the server, but just allows to avoid unnecessary updates on the client side if the response isn't actual now
immediateTrue means, that the default ActionListener should be executed immediately (i.e. during Apply Request Values phase of the request processing lifecycle), rather than waiting until the Invoke Application phase
intervalInterval (in ms) for call poll requests. Default value 1000 (1 sec)
limitToListIf "true", updates on client side ONLY elements from this 'reRender' property. If "false" (default) updates all rendered by ajax region components
onbeforedomupdateJavaScript code for call before DOM has been updated on client side
oncompleteJavaScript code for call after request completed on client side
onsubmitJavaScript code for call before submission of ajax event
processId['s] (in format of call UIComponent.findComponent()) of components, processed at the phases 2-5 in case of AjaxRequest caused by this component. Can be single id, comma-separated list of Id's, or EL Expression with array or Collection
renderedIf "false", this component is not rendered
reRenderId['s] (in format of call UIComponent.findComponent()) of components, rendered in case of AjaxRequest caused by this component. Can be single id, comma-separated list of Id's, or EL Expression with array or Collection
statusID (in format of call UIComponent.findComponent()) of Request status component
timeoutTimeout (in ms) for request

Table 6.34. Component identification parameters

NameValue
component-typeorg.ajax4jsf.Poll
component-familyorg.ajax4jsf.components.AjaxPoll
component-classorg.ajax4jsf.component.html.AjaxPoll
renderer-typeorg.ajax4jsf.components.AjaxPollRenderer

6.17.2. Creating on a page

To create the simplest variant on a page use the following syntax:

Example:


<a4j:poll interval="500" reRender="grid"/>

6.17.3. Creating the Component Dynamically Using Java

Example:


import org.ajax4jsf.component.html.AjaxPoll;
...
AjaxPoll myPoll = new AjaxPoll();
...

6.17.4. Key attributes and ways of usage

The <a4j:poll> componet is used for periodical polling of server data. In order to use the component it's necessary to set an update interval. The "interval" attribute defines an interval in milliseconds between the previous response and the next request. The total period beetween two requests generated by the <a4j:poll> component is a sum of an "interval" attribute value and server response time. Default value for "interval" attribute is set to "1000" milliseconds (1 second). See an example of definition in the "Creating on a page" section.

The "timeout" attribute defines response waiting time in milliseconds. If a response isn't received during this period a connection is aborted and the next request is sent. Default value for "timeout" attribute isn't set.

The "enabled" attribute defines should the <a4j:poll> send request or not. It's necessary to render the <a4j:poll> to apply the current value of "enabled" attribute. You can use an EL-expression for "enabled" attribute to point to a bean property. An example of usage of mentioned above attributes is placed below:

Example:


...
    <a4j:region>
        <h:form>
            <a4j:poll id="poll" interval="1000" enabled="#{userBean.pollEnabled}" reRender="poll,grid"/>
        </h:form>
    </a4j:region>
    <h:form>
        <h:panelGrid columns="2" width="80%" id="grid">
            <h:panelGrid columns="1">
                <h:outputText value="Polling Inactive" rendered="#{not userBean.pollEnabled}"></h:outputText>
                <h:outputText value="Polling Active" rendered="#{userBean.pollEnabled}"></h:outputText>
                <a4j:commandButton style="width:120px" id="control"
                                   value="#{userBean.pollEnabled?'Stop':'Start'} Polling"
                                   reRender="poll, grid">
                    <a4j:actionParam name="polling" value="#{!userBean.pollEnabled}"
                                     assignTo="#{userBean.pollEnabled}"/>
                </a4j:commandButton>
            </h:panelGrid>
                <h:outputText id="serverDate" style="font-size:16px" value="Server Date: #{userBean.date}"/>
        </h:panelGrid>  
    </h:form>
...

The example shows how date and time are updated on a page in compliance with data taken from a server. The <a4j:poll> componet sends requests to the server every second. "reRender" attribute for <a4j:poll> contains value of its own Id. Hence, it renders itself for applying the current value of "enabled" attribute.

Note:

The form around the <a4j:poll> component is required.

Information about the "process" attribute usage you can find here.

6.17.5. Relevant resources links

Here you can see the example of <a4j:poll> usage and sources for the given example.

The aditional information about component usage you can find here : RichFaces Users Forum.