动作方法运行时jsf primefaces进度条更新值 [英] jsf primefaces progressbar update value while action method is running

查看:17
本文介绍了动作方法运行时jsf primefaces进度条更新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 JSF 页面底部有一个提交按钮,它将所有输入(文本、文件等)提交到数据库和服务器.由于此操作需要的持续时间,我想向用户显示操作的进度,并在完成时将他重定向到完成站点.

I have a submit-button on the bottom of my JSF-page, which submits all the inputs (texts, files etc.) to the database and server. Due to the duration this action takes, I want to show the user the progress of the operation, and on finish redirect him on the finish-site.

我的 bean 看起来像:

My bean looks like:

<h:form enctype="multipart/form-data">
    <p:commandButton widgetVar="submitButton" value="Save to DB" action="#{bean.submit}" onclick="PF('submitButton').disable();" />
    <p:progressBar widgetVar="pbAjax" ajax="true" value="#{bean.saveProgress}" interval="1000" labelTemplate="{value}%" styleClass="animated" />            
</h:form>

和我的代码:

private int saveProgress;

public String submit(){
    for(int i = 0; i < 100; i++){ //dummy values
        //DB operations
        //file uploads
        saveProgress = i;
        System.out.println("Progress: " + saveProgress);
    }

    return "finished.xhtml";
}

//getter for saveProgress

问题是,完成后进度条和页面都不会导航到finished.xhtml.

我在这里做错了什么?这是一个线程问题(因为提交不是线程安全的?)我该如何解决这个问题?

What am I doing wrong here? is this a thread-problem (because submit is not thread-safe?) How can I solve this problem?

推荐答案

这个解决方案(使用异步)是一个 hack,但它有效:

this solution (using async) is a hack, but it works:

<p:commandButton id="executeButton" action="#{myBean.longOperation}"
    async="true" process="@form" value="start import"
    onclick="progress.start()" global="false" />

<br />

<p:progressBar ajax="true" widgetVar="progress" value="#{myBean.progress}"
    labelTemplate="{value}%" styleClass="animated" global="false" />

<br />

<p:outputPanel id="result" autoUpdate="true">
    <h:outputText value="#{myBean.message}" />
</p:outputPanel>

用这种豆子

@ManagedBean
@ViewScoped
public class MyBean implements Serializable
{
    private static final long serialVersionUID = 1L;

    private double progress = 0d;
    private String message = "ready";

    public String longOperation() throws InstantiationException, IllegalAccessException
    {
        for(int i = 0; i < 100; i++)
        {
            // simulate a heavy operation
            progress++;
            message = "processing [" + i + "]";
            Thread.sleep(1000);
        }

        message = "completed";

        return "result";
    }

    public double getProgress()
    {
        return progress;
    }

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

    public String getMessage()
    {
        return message;
    }

    public void setMessage(String message)
    {
        this.message = message;
    }
}

这篇关于动作方法运行时jsf primefaces进度条更新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆