如何从命令

时间:2019-12-23 17:00:26

标签: jsf primefaces

我想显示进度。

我在页面上有progressBar:

<?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://java.sun.com/jsf/html"
        xmlns:p="http://primefaces.org/ui">

<h:body>

    <h:form>
        <p:growl id="growl" life="2000" />

        <p:outputPanel id="statusPanel" widgetVar="statusPanel">
            <div class="ui-g">
                <div class="ui-g-12">
                    <p:progressBar widgetVar="pbAjax" ajax="true" value="#{myView.progress}" labelTemplate="{value}%" styleClass="animated" global="false">
                        <p:ajax event="complete" listener="#{myView.onComplete}" update="growl"/>
                    </p:progressBar>
                </div>
            </div>
        </p:outputPanel>

        <p:commandButton id="refresh" value="Refresh" action="#{myView.refresh}" update="@form, statusPanel" />

    </h:form>
</h:body>

</html>

MyView看起来像:

package app;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;

@Named
public class MyView {

    Integer progress;

    int actualCount;
    int overallCount = 4;

    public void refresh() {
        try {
            for (int i = 0; i < overallCount; i++) {
                Thread.sleep(1000); // just to make it slower
                ++actualCount;
                progress = 100 * actualCount / overallCount;
                System.out.println("progress: " + progress);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public Integer getProgress() {
        System.out.println("getProgress(): " + progress + "%");
        return progress;
    }

    public void setProgress(Integer progress) {
        this.progress = progress;
    }

    public void onComplete() {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Refresh completed"));
    }

}

但是进度条只有在refresh()调用结束时才会更新。

我想在两者之间进行更新...

我试图打电话给更新

RequestContext.getCurrentInstance().update("pbAjax");

但这不起作用。

编辑:

我检查了Progress bar primefaces on backend processing,但这只是展示示例的描述(对我的情况不是很有帮助)

我还查看了Primefaces progressbar not updating?,它看起来更接近于我想要实现的目标,但是它对我没有用:

package app;

import javax.faces.bean.ViewScoped;
import javax.inject.Named;
import java.util.Date;

@Named
@ViewScoped
public class BatchModel {

    Integer progress;

    public Integer getProgress() {
        System.out.println(new Date() + " get progress");
        return progress;
    }

    public void setProgress(Integer progress) {
        System.out.println(new Date() + " set progress: " + progress);
        this.progress = progress;
    }

}

我将视图修改为:

package app;

import org.primefaces.context.RequestContext;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

@Named
@ViewScoped
public class MyView {

    @Inject
    BatchModel batchModel;

    int actualCount;
    int overallCount = 4;

    public void refresh() {
        try {
            for (int i = 0; i < overallCount; i++) {
                Thread.sleep(1000);
                ++actualCount;
                batchModel.setProgress( 100 * actualCount / overallCount );
                RequestContext.getCurrentInstance().update("pbAjax");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void onComplete() {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Refresh completed"));
    }

}

...添加了batchModel + @ViewScope

在xhtml中,我将refresh()称为actionListener

<?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://java.sun.com/jsf/html"
        xmlns:p="http://primefaces.org/ui">

<h:body>

    <h:form>
        <p:growl id="growl" life="2000" />

        <p:outputPanel>
            <div class="ui-g">
                <div class="ui-g-12">
                    <p:progressBar widgetVar="pbAjax" ajax="true" value="#{batchModel.progress}" labelTemplate="{value}%" styleClass="animated" global="false">
                        <p:ajax event="complete" listener="#{myView.onComplete}" update="growl"/>
                    </p:progressBar>
                </div>
            </div>
        </p:outputPanel>

        <p:commandButton id="refreshBt" value="Refresh" actionListener="#{myView.refresh}" onclick="PF('pbAjax').start()" />

    </h:form>
</h:body>

</html>

但是日志描述的最好:

Mon Dec 23 18:45:58 CET 2019 get progress
Mon Dec 23 18:45:58 CET 2019 get progress
Mon Dec 23 18:46:00 CET 2019 set progress: 25
Mon Dec 23 18:46:01 CET 2019 set progress: 50
Mon Dec 23 18:46:02 CET 2019 set progress: 75
Mon Dec 23 18:46:03 CET 2019 set progress: 100
Mon Dec 23 18:46:03 CET 2019 get progress
Mon Dec 23 18:46:03 CET 2019 get progress

因此,get进度在开始时才被调用,然后仅在结尾时被调用...

1 个答案:

答案 0 :(得分:1)

第一步应该检查是否定期调用getProgress()(即使未设置进度)。默认间隔为3000毫秒。

就我而言,我在XHTML显示中缺少<head>时遇到了问题

  

PrimeFaces未定义

在浏览器控制台中。