操作方法运行时jsf primefaces progressbar更新值

操作方法运行时jsf primefaces progressbar更新值,jsf,primefaces,navigation,progress-bar,submit,Jsf,Primefaces,Navigation,Progress Bar,Submit,我在JSF页面的底部有一个提交按钮,它将所有输入(文本、文件等)提交到数据库和服务器。由于此操作所需的持续时间,我希望向用户显示操作的进度,并在完成时在完成站点上重定向他 我的豆子看起来像: <h:form enctype="multipart/form-data"> <p:commandButton widgetVar="submitButton" value="Save to DB" action="#{bean.submit}" onclick="PF('subm

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

我的豆子看起来像:

<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
private int saveProgress;
公共字符串提交(){
对于(int i=0;i<100;i++){//伪值
//数据库操作
//文件上传
saveProgress=i;
System.out.println(“进度:+saveProgress”);
}
返回“finished.xhtml”;
}
//保存进度的getter
问题是,完成后progressbar不会更新,页面也不会导航到finished.xhtml。

我做错了什么?这是线程问题吗(因为提交不是线程安全的?) 如何解决此问题?

此解决方案(使用异步)是一种黑客攻击,但它可以:

<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;
    }
}
@ManagedBean
@视域
公共类MyBean实现了可序列化
{
私有静态最终长serialVersionUID=1L;
私人双进度=0d;
私有字符串message=“就绪”;
公共字符串longOperation()引发实例化异常,IllegaAccessException
{
对于(int i=0;i<100;i++)
{
//模拟繁重的操作
进步++;
message=“处理[”+i+“]”;
睡眠(1000);
}
message=“已完成”;
返回“结果”;
}
公共双getProgress()
{
返回进度;
}
公共进度(双进度)
{
这个。进步=进步;
}
公共字符串getMessage()
{
返回消息;
}
公共无效设置消息(字符串消息)
{
this.message=消息;
}
}

progressbar现在可以工作了,但是导航还没有完成:(。你知道这也是一个解决方案吗?这很奇怪……对我来说,导航也可以工作。progressbar是否达到100%?@MicheleMariotti这是一个整洁的解决方案,不是一个黑客。异步属性太好了!导航在你的示例中无法工作,因为它不返回字符串(action method)…PS:它达到了100%,但重定向没有执行到xhtml页面OK,我解决了它;问题是我在action()中使用了外部URL。我将其替换为“../../somepage.xhtml?faces redirect=true”;现在它可以工作了。最后一个问题;我应该在actionListener或action方法中包含busy操作吗?您是否在faces config.xml中定义了导航?我在不需要导航规则的地方使用JSF 2.0…在它工作的其他页面中(使用h:commandbutton),但p:commandbutton在执行操作方法OK后忽略操作结果。您在问题中没有指定该结果。