简体   繁体   English

Primefaces长时间运行服务器作业时的进度条

[英]progress bar while long running server job with Primefaces

I'd like a bar showing progress of a long-running server job launched with commandButton in jsf / Primefaces. 我想要一个显示在jsf / Primefaces中使用commandButton启动的长时间运行的服务器作业的进度条。

The showcase for Primefaces shows how to create a pb which updates according to the state of some variable on server side, with Ajax: http://www.primefaces.org/showcase/ui/misc/progressBar.xhtml Primefaces的展示会展示如何使用Ajax根据服务器端某些变量的状态创建更新的pb: http : //www.primefaces.org/showcase/ui/misc/progressBar.xhtml

<h3>Ajax ProgressBar</h3>
<p:commandButton value="Start" type="button" onclick="PF('pbAjax').start();PF('startButton2').disable();" widgetVar="startButton2" />
<p:commandButton value="Cancel" actionListener="#{progressBarView.cancel}" oncomplete="PF('pbAjax').cancel();PF('startButton2').enable();" />
<br /><br />
<p:progressBar widgetVar="pbAjax" ajax="true" value="#{progressBarView.progress}" labelTemplate="{value}%" styleClass="animated" global="false">
    <p:ajax event="complete" listener="#{progressBarView.onComplete}" update="growl" oncomplete="startButton2.enable()"/>
</p:progressBar>

I try to add an action on the commandButton , that should have for effect to update the progress value: 我尝试在commandButton上添加一个action ,该action应有效地更新进度值:

<p:commandButton value="Start" type="button" onclick="PF('pbAjax').start();
                    PF('startButton2').disable();" widgetVar="startButton2" action="#{computer.compute()}"/>

The Computer bean: 电脑豆:

@ManagedBean
@SessionScoped
public class Computer {

    long i;

    public Computer() {
    }

    public String compute() throws InterruptedException {
        i = 1;
        while (i < 10) {
            i++;
            Thread.sleep(1000);
        }
        return "welcomePrimefaces.xhtml";
    }
}

The ControllerBean: ControllerBean:

ManagedBean
@ViewScoped
public class ControllerBean {

@Inject Computer computer;

    public ControllerBean() {
    }

    private Integer progress;


    public Integer getProgress() {
        if (computer.i == 1){
            progress = 30;
        }
        if (computer.i == 2){
            progress = 60;
        }
        if (computer.i == 3){
            progress = 90;
        }
        if (computer.i == 4){
            progress = 100;
        }
        return progress;
    }

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

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

    public void cancel() {
        progress = null;
    }

}

But the compute method is never called, i is never updated. compute方法不会被调用, i永远不会更新。 Surely this is this logic (injecting computer into controllerbean ) which is incorrect. 当然,这是错误的逻辑(将computer注入controllerbean )。 Any pointer to get it to work is appreciated! 任何使它工作的指针都值得赞赏!

Two possible problems with your code 您的代码有两个可能的问题

  1. A button of type button will not execute a server-side action, it's meant only for navigation and client-side business (mostly javascript). 类型为button将不会执行服务器端操作,仅用于导航和客户端业务(主要是javascript)。 Unless you really mean to, you shouldn't have to set the type attribute on your buttons (it defaults to type="submit" , the kind that executes server-side actions). 除非您确实要这么做,否则不必在按钮上设置type属性(默认为type="submit" ,即执行服务器端操作的类型)。

  2. Depending on your version of JSF, you probably won't have a successful bean injection when combining @Inject and @ManagedBean -type beans. 根据您的JSF版本,结合使用@Inject@ManagedBean类型的bean时,可能不会成功进行bean注入。 Prior to JSF-2.2, the handshake between CDI ( @Inject ) and JSF( @ManagedBean etc) was very buggy. 在JSF-2.2之前,CDI( @Inject )和JSF( @ManagedBean等)之间的握手非常麻烦。 The most effective way to inject JSF-managed beans is using the @ManagedProperty annotation. 注入JSF管理的bean的最有效方法是使用@ManagedProperty批注。


Putting both together, you should have: 将两者放在一起,您应该具有:

<p:commandButton value="Start" onclick="PF('pbAjax').start();
                PF('startButton2').disable();" widgetVar="startButton2" action="#{computer.compute()}"/>

And in your backing bean 并在您的支持豆

@ManagedProperty(value="#{computer}")
Computer computer;

The @ManagedProperty annotation will use an all-lowercase version of your class name since you didn't explicitly specify a name for that managed bean @ManagedProperty批注将使用类名的全小写形式,因为您没有为该托管Bean明确指定名称

Related Reading: 相关阅读:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM