The a4j:poll component defines a way to periodically poll a server in order to trigger state changes, or update parts of your page. You can use the same options, and settings as the a4j:ajax or a4j:commandButton. So you can set execute, and render attributes, as well as any normal JSF action attributes. RichFaces uses a standard form based request, so the form around the a4j:poll component is required.
The example below updates the date and time on the page based on the data on the server. To turn off polling, press "Stop Polling" button.
<!DOCTYPE html> <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:a4j="http://richfaces.org/a4j"> <h:form id="form"> <a4j:poll id="poll" interval="2000" enabled="#{pollBean.pollEnabled}" render="poll,grid" /> <h:panelGrid columns="2" width="80%" id="grid"> <h:panelGrid columns="1"> <h:outputText value="Polling Inactive" rendered="#{not pollBean.pollEnabled}" /> <h:outputText value="Polling Active" rendered="#{pollBean.pollEnabled}" /> <a4j:commandButton style="width:120px" id="control" value="#{pollBean.pollEnabled?'Stop':'Start'} Polling" render="poll, grid"> <a4j:param name="polling" value="#{!pollBean.pollEnabled}" assignTo="#{pollBean.pollEnabled}" /> </a4j:commandButton> </h:panelGrid> <h:outputText id="serverDate" style="font-size:16px" value="Server Date: #{pollBean.date}" /> </h:panelGrid> </h:form> </ui:composition>
package org.richfaces.demo.poll; import java.io.Serializable; import java.util.Date; import javax.faces.bean.ManagedBean; import javax.faces.bean.ViewScoped; /** * @author Ilya Shaikovsky */ @ManagedBean @ViewScoped public class PollBean implements Serializable { private static final int POLL_DISABLEMENT_INTERVAL = 60000; private static final long serialVersionUID = 7871292328251171957L; private Date pollStartTime; private boolean pollEnabled; public PollBean() { pollEnabled = true; } public Date getDate() { Date date = new Date(); if (null == pollStartTime) { pollStartTime = new Date(); return date; } if ((date.getTime() - pollStartTime.getTime()) >= POLL_DISABLEMENT_INTERVAL) { setPollEnabled(false); } return date; } public boolean getPollEnabled() { return pollEnabled; } public void setPollEnabled(boolean pollEnabled) { if (pollEnabled) { setPollStartTime(null); } this.pollEnabled = pollEnabled; } public Date getPollStartTime() { return pollStartTime; } public void setPollStartTime(Date pollStartTime) { this.pollStartTime = pollStartTime; } }