JSF, refresh periodically a component with ajax?

Potinos picture Potinos · Jun 27, 2012 · Viewed 33.7k times · Source

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?

Answer

Daniel picture Daniel · Jun 27, 2012

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