3
votes

My progressbar is not updated, why ? The controller method is called as it should and the progess variable is correctly incremented:

XHTML

<p:dialog>
    <h:outputLabel value="Count:" for="batchCount"/>
    <p:inputText id="batchCount" required="true"
        value="#{batchModel.count}">
    </p:inputText>

    <p:commandButton id="generateBatchButton" value="GO"
        actionListener="#{batchController.sendBatch()}"
        onclick="PF('progressVar').start();"
        oncomplete="PF('dialogBatchParams').hide();"/>

    <p:progressBar id="progressBar"
        widgetVar="progressVar" 
        value="#{batchModel.progress}" 
        labelTemplate="{value}%">
    </p:progressBar>
</p:dialog>

CONTROLLER Method

public void sendBatch() {       
    for (int i = 0; i < batch.size(); i++) {
        batchModel.setProgress(Math.round(100 * (float) i / (float) batch.size()));
        // Do stuff here
    }
}

MODEL

@Named
@ViewScoped // or @SessionScoped 
public class BatchModel implements Serializable {
    private static final long serialVersionUID = 1L;

    private int count = 100;
    private int progress;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
    }
}

My progress is correctly updated, I get this output when logging it:

2016-10-19 10:08:49,707 INFO controller.BatchController -> Sending batch
0
10
20
30
40
50
60
70
80
90
2016-10-19 10:08:57,432 INFO controller.BatchController -> Done sending batch

I am using PF 6. I tried with and without "update" tag, and I played around with the ajax tag, but no dice.

1
Try using @ViewScoped on BatchModel (and your controller).Jasper de Vries
.. each update of your progress bar triggers a request to get the progress. Since it's request scoped you'll keep getting 0.Jasper de Vries
I changed it like you said, but it does not update the progressbar. I updated the question with my new code.Tim
To rule out the scope thing, can you confirm BatchModel#getProgress() is giving you the correct values?Jasper de Vries
Did you try adding ajax="true" to your progress bar?Jasper de Vries

1 Answers

4
votes

Your question started of with a RequestScoped bean. Those are created each request. Since an update of the bar requires a request, you will get a new bean with progress set to 0 again.
It's best to use ViewScoped on your bean (and controller).

Also, you are missing ajax="true" in your progress bar (it expects you to do the updates on the client side). You should change it to:

<p:progressBar id="progressBar"
               ajax="true"
               widgetVar="progressVar" 
               value="#{batchModel.progress}" 
               labelTemplate="{value}%"/>