0

I'm pretty new to JSF so I need someone who can help me. I'm trying to implement a progress bar in a page, so I've followed this example; it's clear enough but my problem is that the value for the "value" attribute is not calculated in the bean (CLASS_A), but in an util class (CLASS_B) invoked from it. In this context, I've created a "accomplishedPercentage" variable in CLASS_A and updated it from CLASS_B so I can use it in a xhtml page but is not getting updated; I also tried creating a ProgressBarBean class (anotated with @ManagedBean and @SessionScoped) and invoke the method to calculate the progress from CLASS_B but I don't know why I'm always getting the ProgressBarBean as null (I'm injecting it with @ManagedProperty). Could someone help me? some example or point me what I'm doing wrong? Thanks in advance.

Some pieces of code:
This is the progress bar in the view:

<rich:progressBar value="#{progressBar.accomplishedPercentage}"
  interval="2000" label="#{progressBar.accomplishedPercentage} %"
  enabled="#{oaBean.showProgressBar}" minValue="-1" maxValue="100"
  reRenderAfterComplete="createFormPanel">
</rich:progressBar>


ProgressBarBean

@ManagedBean(name="progressBar")
@SessionScoped
public class ProgressBarBean implements Serializable {

private static final long serialVersionUID = 1L;
private Long accomplishedPercentage;

@PostConstruct
public void init() {
    accomplishedPercentage = 0L;
}

public void incrementAccomplishedPercentage (Long inc) {
    this.setAccomplishedPercentage(this.getAccomplishedPercentage() + inc);
}
//getter and setter for accomplishedPercentage
}


The progress bar is then injected in CLASS_B through @ManagedProperty(value="#{progressBar}") private ProgressBarBean progressBar; and invoke the method incrementAccomplishedPercentage, but as I said, I'm getting the progressBar with a null value.


EDIT 1
Now I'm receiving an instance of ProgressBarBean, but the problem is that never gets updated the value of the progress bar in the UI: the method getAccomplishedPercentage is only invoked when loading the page (so it is always 0), not during the processing every 2 seconds.
I suppose that the progress bar itself is no related to the button which should start it. Here is full definition of the progress bar:

<p:dialog modal="true" widgetVar="statusDialog" header="Status"
draggable="false" closable="false" resizable="false">
<rich:progressBar value="#{oaBean.accomplishedPercentage}"
    interval="1000" label="#{oaBean.accomplishedPercentage} %"
    enabled="#{oaBean.showProgressBar}" minValue="-1" maxValue="100"
    reRenderAfterComplete="createFormPanel">
</rich:progressBar>
</p:dialog>


And this is the definition of the command button:

<p:commandButton id="generate" value="Generate" 
  oncomplete="generateDialogDocumento.hide() onclick="PrimeFaces.monitorDownload(start, stop)"
  icon="ui-icon-arrowthichk-s" ajax="false">
    <p:fileDownload value="#{oaBean.file}" />
</p:commandButton>

I've tried to link both controls but I couldn't, does anybody know what should I do?
Regards
EDIT 2 Based on @Andy's suggestion I changed the progress bar to primefaces, but in this case I don't see any progress being displayed (not even 0). This is now the code for the commandButton:

<p:commandButton id="generate" value="Generate" 
oncomplete="generateDialogDocumento.hide()" onclick="PrimeFaces.monitorDownload(start, stop); statusDialog.show();"
icon="ui-icon-arrowthichk-s" ajax="false">
<p:dialog modal="false" widgetVar="statusDialog" header="Status"
    draggable="false" closable="false" resizable="false">
    <p:growl id="growl" />
    <p:progressBar widgetVar="pbAjax" ajax="true" value="#{progressBar.accomplishedPercentage}" labelTemplate="#{progressBar.accomplishedPercentage}%" >  
        <p:ajax event="complete" listener="#{progressBar.onComplete}" update="growl" oncomplete="statusDialog.hide()"/>  
</p:progressBar> 
</p:dialog>
<p:fileDownload value="#{oaBean.file}" />

maxivis
  • 1,727
  • 3
  • 21
  • 35
  • What's the scope of `CLASS_B`? – dratewka Aug 01 '13 at 12:31
  • Hi @dratewka, actually it has no scope specified, is just a simple class used as a utility – maxivis Aug 01 '13 at 12:33
  • If it's not a managed bean then injection (`@ManagedProperty`) doesn't work - thus the null `progressBar` value. Change it to a managed bean and see if it helps – dratewka Aug 01 '13 at 12:50
  • Thanks @dratewka, I'm creating the beans with `@Scope` and `@Named` and injected the dependency with `@Autowired` (not using ManagedBean or ManagedProperty), so now I'm receiving the correct instance of ProgressBarBean but the updated value is not reflected in the UI. I've tried accessing to ProgressBarBean in CLASS_B (#{classb.progressBar.accomplishedPercentage}) and also creating a new variable "accomplishedPercentage" in CLASS_B and invoking it from UI (#{classb.accomplishedPercentage}), but still can't see the updated value. Any tip? – maxivis Aug 01 '13 at 14:57
  • Two questions: 1) does `getAccomplishedPercentage` get called every 2 seconds (place a breakpoint inside)? 2) is your progress bar inside a `h:form`? – dratewka Aug 01 '13 at 16:50
  • No, `getAccomplishedPercentage` is not called at all during the rendering of the progress bar, and for 2) yes, is not the first child of the form but is inside it. – maxivis Aug 01 '13 at 17:49
  • It's a little weird that you're using both primefaces and richfaces. Why not use primefaces progressbar – Andy Aug 01 '13 at 19:16
  • Hi @Andy, thanks for your response, please take a look to the last update, regards. – maxivis Aug 01 '13 at 19:47
  • in your `onclick` add `pbAjax.start()` and let me know. – Andy Aug 01 '13 at 19:52
  • One more suggesstion, I think your mixing both tutorials or I might be wrong. why don't you read this quick one instead (the last answer) http://stackoverflow.com/questions/17657041/progress-bar-primefaces-on-backend-processing/17690809#17690809 – Andy Aug 01 '13 at 19:55
  • Also, is the `dialog` inside your `commandButton` or is that a typo ? And why have `oncomplete` if `ajax = "false"` – Andy Aug 01 '13 at 19:59
  • Thanks for all your suggestions, so, from the beginning, the pbAjax.start() didn't make any difference (tested with and without the dialog), regarding the tutorial I'll start reading right now, the `dialog` is included into the `commandButton` and finally I setted `ajax="true"` but also removed the `oncomplete` invocation because I was having some errors with it. – maxivis Aug 01 '13 at 20:07
  • Ok, if you still can't figure it out let me know and I'll try to create a sample on my end. Make sure you update the relevant part in your xhtml – Andy Aug 01 '13 at 20:09
  • Hi @Andy, I still can't update the progress bar but don't worry, I temporarily suspended it; nevertheless there is something I want to comment: I couldn't even add a static progress bar (``), I mean, I didn't saw the value, could this be caused by some parent component (some form or maybe a dialog)? Thanks for all your help – maxivis Aug 06 '13 at 12:54
  • Sorry for the late reply. I'm not sure. Try adding the progressbar inside a 'p:dialog' – Andy Aug 07 '13 at 01:08

0 Answers0