【问题标题】:jsf primefaces progressbar update value while action method is running动作方法运行时jsf primefaces进度条更新值
【发布时间】:2014-03-31 08:33:18
【问题描述】:

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

我的 bean 看起来像:

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

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

【问题讨论】:

  • 你在 faces-config.xml 中定义了导航吗??
  • 我在不需要导航规则的地方使用 JSF 2.0...在其他页面中它可以工作(使用 h:commandbutton),但 p:commandbutton 在执行动作方法后忽略动作结果跨度>
  • 好的。您没有在问题中指定。

标签: jsf primefaces navigation progress-bar submit


【解决方案1】:

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

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

【讨论】:

  • 进度条现在可以工作,但导航没有完成:(。你知道这也是一个解决方案吗?
  • 这很奇怪......对我来说导航也能正常工作。 progressBar 达到 100% 了吗?
  • @MicheleMariotti 这是一个简洁的解决方案,而不是黑客攻击。 async 属性太好了!
  • 导航不能在你的例子中工作,因为它没有返回字符串(动作方法)... PS:达到100%,但是重定向没有执行到xhtml页面跨度>
  • 好的,我解决了;问题是我在 action() 中使用了外部 url。我将其替换为 "../../somepage.xhtml?faces-redirect=true";现在它可以工作了。最后一个问题;我应该在 actionListener 或 action-method 中包含繁忙的操作吗?
猜你喜欢
  • 2014-06-11
  • 1970-01-01
  • 1970-01-01
  • 2012-12-04
  • 2019-05-18
  • 1970-01-01
  • 2018-12-25
  • 2021-05-29
  • 2015-07-04
相关资源
最近更新 更多