如何在jsf2.0中定期更新页面?

时间:2013-04-24 15:30:26

标签: jsf-2 primefaces

我的JSF 2.0页面包含一些数据,这些数据是动态的,因此需要在某个预定义的时间间隔内自动刷新(例如每10秒)。 我使用PrimeFaces 3.5作为强大的组件套件。以下是托管bean -

@ManagedBean
@ViewScoped
public Monitor implements Serializable {

    //max,min,avg,stdDev,reason are caluclated based on some dynamic data
    private int max;
    private int min;
    private double avg;
    private double stdDev;
    private String reason;

    public int getMax() {
            //Here before returning I am calling some methods to get the max from dynamic data
        return max;
    }

    public int getMin() {
            //Here before returning I am calling some methods to get the min from dynamic data
        return min;
    }

    public double getAvg() {
            //Here before returning I am calling some methods to get the avg from dynamic data
        return avg;
    }

    public String getReason() {
            //Here before returning I am calling some methods to get the reason from dynamic data
        return reason;
    }

    public double getStdDev() {
            //Here before returning I am calling some methods to get the stdDev from dynamic data
        return stdDev;
    }

    public void setMax(int max) {
        this.max = max;
    }

    public void setMin(int min) {
        this.min = min;
    }

    public void setAvg(double avg) {
        this.avg = avg;
    }

    public void setStdDev(double stdDev) {
        this.stdDev = stdDev;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }
}

到目前为止,我还没有最终确定布局,但我会是这样的 -

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Insert title here</title>
    </h:head>
    <h:body>
        <p:panel>
            <h:outputLabel value="#{monitor.min}" />
            <h:outputLabel value="#{monitor.max}" />
            <h:outputLabel value="#{monitor.avg}" />
            <h:outputLabel value="#{monitor.stdDev}" />
            <h:outputLabel value="#{monitor.reason}" />
        </p:panel>
    </h:body>
</html>

如何在JSF 2.0中定期更新页面?

1 个答案:

答案 0 :(得分:2)

您可以使用 Poll 自动刷新页面,投票可以通过javascript启动停止: 例如,当count(bean变量)&gt; = 10时,poll将停止: 您可以像@Ravi post一样使用停止,我的示例演示可以启动停止(通过:pol.stop()和pol.start())。

Facelets的:

<h:form id="form">  
            <p:panel id="pntest">
              // content here
            </p:panel>
            <h:outputText id="txt_count" value="#{tabview.count}" />
            <p:poll interval="1" listener="#{tabview.increment}" update="txt_count" widgetVar="pol" oncomplete="test(xhr, status, args);"/>
            <script type="text/javascript">
                //<![CDATA[
                function test(xhr, status, args){
                    if(args.sotest >= 10){
                        pol.stop();
                    }else{
                       location.reload(); // refresh page                        
                     }
                }
                //]]>
            </script>
        </h:form>

豆:

     private int count = 0;
        public void increment() {
                count++;
                RequestContext reqCtx = RequestContext.getCurrentInstance();
                reqCtx.addCallbackParam("sotest", count);
                //1. update by using PrimeFaces specific API, use [RequestContext#update()][3].
                RequestContext.getCurrentInstance().update("form:pntest");
                //2. update by using standard JSF API, add the client ID to [PartialViewContext#getRenderIds()][4].             

FacesContext.getCurrentInstance().getPartialViewContext().getRenderIds().add("form:pntest");
            }

            public int getCount() {
                return this.count;
            }

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