2014-03-31 38 views
3

我的JSF页面底部有一个提交按钮,它将所有的输入(文本,文件等)提交给数据库和服务器。由于此操作的持续时间,我想向用户显示操作的进度,并在完成网站上完成重定向。jsf primefaces progressbar update value while action method is running

我豆腐的样子:

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

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

+0

是否定义faces-config.xml中的导航? – Makky

+0

我在不需要导航规则的情况下使用JSF 2.0 ...在其他页面中它工作(使用h:commandbutton),但p:commandbutton在执行动作方法 – Niko

+0

后忽略动作结果。你没有在你的问题中指定。 – Makky

回答

3

该解决方案(使用异步)是一个黑客,但它的工作原理:

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

进度条现在工作,但导航没有完成:(你知道这也是一个解决方案吗? – Niko

+0

这是非常奇怪的...对我来说,导航也在工作,progressBar达到100%吗? –

+0

@MicheleMariotti它是一个整洁解决方案不是黑客,异步属性太好了! – Makky