user3243494
user3243494

Reputation: 55

how to call JSF bean method repeatedly and dynamically?

I want to do something like that.

public class Bean
{
public void test()
{
System.out.println(System.currentTimeMillis());
}
}




<script>
window.setInterval(function()
{
//bean.test() 

}, 1000);

</script>

I already played around with simulating button click etc. But it never really worked as it was supposed to. Either it didn't work at all or the test()-method was only called on page load.

I would be glad if someone has a working example :)

Upvotes: 0

Views: 715

Answers (2)

Bj&#246;rn Landmesser
Bj&#246;rn Landmesser

Reputation: 953

If you are using Primefaces, you can use the <p:remoteCommand> to call the backing bean via JavaScript.

<p:remoteCommand name="callback" actionListener="#{bean.test}"/>

<h:outputScript>
  window.setInterval(function() {
    callback();
  }, 1000);
</h:outputScript>

Upvotes: 1

Michele Mariotti
Michele Mariotti

Reputation: 7469

this is what you are looking for:

<h:form id="form">
    <h:outputText id="txt_count" value="#{counterBean.count}" />

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

Upvotes: 1

Related Questions