jsf-2 - jsf2.0如何定时更新页面?

标签 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 中定期更新页面?

最佳答案

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

面:

<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>

bean 角,扁 bean :
     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;
            }

关于jsf-2 - jsf2.0如何定时更新页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16196094/

相关文章:

java - JSF2 Form 到 DB 特殊字符处理

jsf-2 - java.lang.NullPointerException : Argument Error: Parameter id is null 异常

html - 在 primefaces 和 css 中垂直对齐元素并响应

templates - 在 JSF 2.0 中将参数作为模板中的参数传递

jquery - 如何使用 jquery 展开/折叠 Primefaces 字段集?

jsf-2 - Primefaces 数据表卡住列未对齐

java - Richfaces poupPanel 在操作执行之前显示

java - 如何访问 UIComponent 属性中链接的 bean?

java - 在 TreeTable 的同一列中添加许多对象类型

ajax - 在 ajax 表单提交后保留 bean 属性