Progressbar在primefaces中不起作用

时间:2013-12-26 16:28:13

标签: java jsf primefaces

我有一个fileUpload按钮,当用户上传文件时,ManagedBean中的进程启动。这个过程可能非常慢,我需要向用户显示进度。所以我有以下代码:

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

在我的managedBean中,我有一个getter和setter to progress属性,我正在做以下事情:

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

    }   

我将“progress”属性传递给我的BO(Bussiness Object),并且在每个迭代器中,该属性的值都会递增。

问题是:progressBar没有任何反应,继续ZERO。

1 个答案:

答案 0 :(得分:1)

查看从您的代码构建的示例。

这只是一个演示,但是它正在以你想要的方式工作!希望您能够根据您的真实代码进行调整。

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

在UploadBean类中:

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 !");
}

说明

  • ProgressBar更新2或3秒(不那么高的刷新率)
  • onComplete未被调用(我不明白为什么)