3

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.

Tim
  • 3,910
  • 8
  • 45
  • 80
  • 2
    Try using `@ViewScoped` on `BatchModel` (and your controller). – Jasper de Vries Oct 12 '16 at 16:51
  • .. 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 Oct 13 '16 at 12:55
  • I changed it like you said, but it does not update the progressbar. I updated the question with my new code. – Tim Oct 19 '16 at 08:16
  • To rule out the scope thing, can you confirm `BatchModel#getProgress()` is giving you the correct values? – Jasper de Vries Oct 19 '16 at 08:19
  • That is precisely what I updated in my question - at least batchModel.getProgress() is giving me correct values. What is with the # ? – Tim Oct 19 '16 at 08:25
  • I was unable to distill that information from your update.. there are just numbers. Based on the INFO lines my guess was they were coming from your controller. # is used Javadoc links. – Jasper de Vries Oct 19 '16 at 08:33
  • Yes, the model is injected in the controller. I set and read it there. – Tim Oct 19 '16 at 08:34
  • 1
    Did you try adding `ajax="true"` to your progress bar? – Jasper de Vries Oct 19 '16 at 08:40
  • That worked. I changed the session back to SessionScoped. Thanks, I don't know why it did not work when I tried before posting here. I guess I had the ajax true, but had forgotten the .start(). Please post this as an answer. – Tim Oct 19 '16 at 09:13

1 Answers1

4

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}%"/>
Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102