I have a fileUpload button and when user upload a file the process in ManagedBean start. This proccess can be very slow and i need to show a progress to user. So i have the following code:
<p:fileUpload id="commandButtonIniciarImportacao"
disabled="#{importacaoArMB.produtoSelecionado == null}"
fileUploadListener="#{importacaoArMB.uploadArquivoImportacao}"
mode="advanced" auto="true" cancelLabel="Cancelar"
update=":formManterArquivoImportacao:tabViewManterArquivoImportacao:dataTableArs,
:formManterArquivoImportacao:tabViewManterArquivoImportacao:dataTableArsIgnorados"
label="Iniciar Importação..."
onstart="pbImportacao.start()" />
<p:progressBar widgetVar="pbImportacao" ajax="true" value="#{importacaoArMB.progresso}"
labelTemplate="#{value}%">
</p:progressBar>
In my managedBean i have a getter and setter to progress attribute, and i'm doing the following:
public void uploadArquivoImportacao(FileUploadEvent fileUploadedEvent) {
if (produtoSelecionado == null) {
addErrorMessage("Selecione o Produto antes de iniciar a importação");
FacesContext.getCurrentInstance().validationFailed();
return;
}
try {
addInfoMessage("Iniciando importação ...");
uploadedFile = fileUploadedEvent.getFile();
// Inicializando atributos do bean (ArquivoImportacao)
byte[] conteudoAsBytes = uploadedFile.getContents();
bean.setConteudo(new String(conteudoAsBytes));
bean.setDataHoraImportacao(new Date());
bean.setNome(uploadedFile.getFileName());
bean.setLayoutImportacao(produtoSelecionado.getLayoutImportacao());
arsIgnorados = getBoPadrao().gerarArsFromImportacao(bean, progresso);
addInfoMessage("Importação finalizada");
} catch (BOException bo) {
addErrorMessage(bo.getMessage());
FacesContext.getCurrentInstance().validationFailed();
} catch (Exception e) {
e.printStackTrace();
addErrorMessage(e.getMessage());
FacesContext.getCurrentInstance().validationFailed();
}
}
I pass the "progress" attribute to my BO (Bussiness Object) and there the value of this attribute is incremented in each iterator of FOR.
The problem is: Nothing happens with progressBar, continue in ZERO.
Look at this example built from your code.
It is only a demo, but it's working the way you want ! Hope you can adapt it to your real code.
<h:form id="form1">
<p:growl id="growl" showDetail="true"/>
<p:fileUpload id="fileup1"
fileUploadListener="#{uploadBean.uploadArquivoImportacao}"
mode="advanced"
auto="true"
cancelLabel="Cancelar"
update="growl"
label="Iniciar Importação..."
onstart="pbImportacao.start()" />
<p:progressBar
id="progress1"
widgetVar="pbImportacao"
ajax="true"
value="#{uploadBean.progresso}"
labelTemplate="{value}%">
<p:ajax event="complete" listener="#{uploadBean.onComplete}" update="growl" />
</p:progressBar>
</h:form>
And in the UploadBean class:
public void uploadArquivoImportacao(FileUploadEvent fileUploadedEvent) {
System.out.println("File uploaded...");
//collects information about uloaded file here...
//then do what you have to do (can take time)
doYourStuff();
}
public void doYourStuff() {
//fake job that takes 10 seconds (100 x 100 millis)
System.out.println("Job starts...");
for (int i = 0; i < 100; i++) {
setProgresso(i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
setProgresso(0);
System.out.println("Job done.");
}
private long progresso = 0;
public long getProgresso() {
return progresso;
}
public void setProgresso(long progresso) {
this.progresso = progresso;
}
public void onComplete() {
System.out.println("oncomplete !");
}
Remarks
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.