JSF, refresh periodically a component with ajax?

34,701

Solution 1

Poll is what you need to use

Poll component make ajax calls in a specified interval.

For example Primefaces Poll

<h:form id="form">
    <h:outputText id="txt_count" value="#{counterBean.count}" />
    <p:poll interval="3" listener="#{counterBean.increment}" update="txt_count" />
</h:form>

Link to showcase Primefaces Ajax Poll

Pure JSF approach would be to use js timer that will invoke periodical document.getElementById('someFormId:idOfButton').click();

or jquery $("#someFormId\\:idOfButton").click();

while the button will look like this

<h:commandButton id="idOfButton">
    <f:ajax render="txt_count"></f:ajax>
</h:commandButton>

something like this

setInterval(function(){$("idOfButton").click()},3000);

More about timer JavaScript Timing Events

Solution 2

In RichFaces there is a poll component as well. Here is a nice example they provide

<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 value="Polling Active" rendered="#{userBean.pollEnabled}" />
                <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>
Share:
34,701
Potinos
Author by

Potinos

Updated on February 05, 2020

Comments

  • Potinos
    Potinos over 4 years

    I'm working on an application JSF and i want to refresh periodically a component with ajax Like facebook notification zone behaviour. How can i do that?

  • BalusC
    BalusC almost 12 years
    Based on OP's question history, he's using PrimeFaces, so that's a perfect suit.