[英]jsf primefaces progressbar update value while action method is running
我的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
問題是,完成后,進度條更新和頁面都不會導航到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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.