Wizard Sultan
Wizard Sultan

Reputation: 918

auto refresh a div using JSF

How to automatically refresh a div every 5 seconds using JSF. I have a div with id = statusBlock, I want the status and comments inside it to get updated automatically every 5 seconds so that user automatically gets all the status updates without refreshing the whole page. I have searched the stackoverflow site but couldn't find anything that would let me do it using JSF.

Upvotes: 1

Views: 14361

Answers (2)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

This can be achieved using setInterval JavaScript function that makes a call to the server once a while.

A simple but not so fancy example would be executing a hidden UICommand in order that user can't update the data by him/herself:

<script type="text/javascript">
    setInterval(function() { 
        document.getElementById('myForm:btnLoadData').submit();
        }, 5*1000);
</script>

<h:panelGroup id="divData" layout="block">
    <!-- content... -->
</h:panelGroup>
<h:form id="myForm">
    <h:commandButton id="btnLoadData" value="Hidden" action="#{bean.loadData}"
        style="display:none">
        <f:ajax execute="@form" render=":divData" />
    </h:commandButton>
</h:form>

Upvotes: 6

Rodrigo Sasaki
Rodrigo Sasaki

Reputation: 7226

Are you using any component lib such as Primefaces?

If you are you could use a polling component.

Here is a showcase link to exemplify: http://www.primefaces.org/showcase/ui/poll.jsf

Upvotes: 2

Related Questions