如何防止在浏览器中导航到另一个页面后停止p:poll

时间:2016-08-16 16:54:18

标签: jsf primefaces

我的页面中有p:poll。当页面加载到浏览器轮询时应该是stat,点击开始按钮后显示运行计数器。当用户导航到另一个页面时,通过单击“转到第一页”,我的问题就出现了。因为导航后轮询时间刻度停止。当你回来时,你应该再次点击开始按钮重启计数器。请帮助我,如何防止在导航到另一个页面后停止投票。在任何情况下我都希望p:poll继续。我正在使用primefaces和Apache myfaces 2-2-10。这是一个必须分页的样本。在第一页中有两个按钮。点击“开始”按钮后,计数器开始显示数字。和按钮'转到第一页'将页面更改为one.xhtml。在第一页中单击“转到索引页面”后,将页面更改为index.xhtml页面。进出后,您将看到计数器停止运行。再次单击“开始”按钮时,计数器开始再次显示枚举。我的gole是当用户在每个页面上导航时,计数器继续而不是停止。感谢您的回复。

此Index.xhtml页面:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui">

<h:head>
    <title>Home Page</title>
</h:head>

<h:body>

    <h:form id="mainForm">

        <p:panelGrid id="pnl" columns="3" rendered="true" columnClasses="ui-column-p-2">

            <p:outputLabel id="lblMessage" value="#{test.count}"/>
            <p:commandButton id="btnStart" value="Begin" onclick="PF('poll').start();"/>
            <p:commandButton id="btnTest" actionListener="#{test.gotoOne}" value="Go To Page One"/>

        </p:panelGrid>

        <p:poll id="poll"  autoStart="false" listener="#{test.counter}"
                update="mainForm:lblMessage" widgetVar="poll"/>
    </h:form>

</h:body>

</html>

这是one.xhtml页面:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui">

<h:head>
    <title>Home Page</title>
</h:head>

<h:body>

    <h:form>
        <p:commandButton id="pn" actionListener="#{test.gotoIndex}" value="Go To Page Index"/>
    </h:form>

</h:body>

</html>

最后这是我的bean,生成数字:

package bean;


import org.primefaces.context.RequestContext;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import java.io.IOException;

@ManagedBean
@ApplicationScoped
public class Test {

    private int count;

    public void counter() {
        count++;
        System.out.println("count = " + count);
    }

    public void gotoIndex() {
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void gotoOne() {
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("one.xhtml");
            RequestContext.getCurrentInstance().execute("PF('poll').start();");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}

1 个答案:

答案 0 :(得分:0)

最后我找到了解决方案。它使用时间调度程序而不是p:poll。我可以使用Quartz Time调度程序,Spring Framework Time Scheduler或EJB Time Scheduler。

相关问题