简体   繁体   English

Progressbar在primefaces中不起作用

[英]Progressbar don't work in primefaces

I have a fileUpload button and when user upload a file the process in ManagedBean start. 我有一个fileUpload按钮,当用户上传文件时,ManagedBean中的进程启动。 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: 在我的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();
        }

    }   

I pass the "progress" attribute to my BO (Bussiness Object) and there the value of this attribute is incremented in each iterator of FOR. 我将“progress”属性传递给我的BO(Bussiness Object),并且在FOR的每个迭代器中都会增加此属性的值。

The problem is: Nothing happens with progressBar, continue in ZERO. 问题是:progressBar没有任何反应,继续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: 在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 !");
}

Remarks 备注

  • ProgressBar updates everery 2 or 3 seconds (not so high refresh rate) ProgressBar更新2或3秒(不那么高的刷新率)
  • onComplete is not called (I don't see why) 没有调用onComplete(我不明白为什么)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM