4
votes

I have a submit-button on the bottom of my JSF-page, which submits all the inputs (texts, files etc.) to the database and server. Due to the duration this action takes, I want to show the user the progress of the operation, and on finish redirect him on the finish-site.

My bean looks like:

<h:form enctype="multipart/form-data">
    <p:commandButton widgetVar="submitButton" value="Save to DB" action="#{bean.submit}" onclick="PF('submitButton').disable();" />
    <p:progressBar widgetVar="pbAjax" ajax="true" value="#{bean.saveProgress}" interval="1000" labelTemplate="{value}%" styleClass="animated" />            
</h:form>

and my code:

private int saveProgress;

public String submit(){
    for(int i = 0; i < 100; i++){ //dummy values
        //DB operations
        //file uploads
        saveProgress = i;
        System.out.println("Progress: " + saveProgress);
    }

    return "finished.xhtml";
}

//getter for saveProgress

the problem is, that neither the progressbar updates nor the pages navigates to finished.xhtml when done.

What am I doing wrong here? is this a thread-problem (because submit is not thread-safe?) How can I solve this problem?

1
Have you defined the navigation in faces-config.xml??Makky
I use JSF 2.0 where navigation rules are not needed... in the other pages it works (with h:commandbutton), but p:commandbutton ignores the action result after executing the action-methodNiko
OK. You didn't specify that in your question.Makky

1 Answers

4
votes

this solution (using async) is a hack, but it works:

<p:commandButton id="executeButton" action="#{myBean.longOperation}"
    async="true" process="@form" value="start import"
    onclick="progress.start()" global="false" />

<br />

<p:progressBar ajax="true" widgetVar="progress" value="#{myBean.progress}"
    labelTemplate="{value}%" styleClass="animated" global="false" />

<br />

<p:outputPanel id="result" autoUpdate="true">
    <h:outputText value="#{myBean.message}" />
</p:outputPanel>

with this kind of bean

@ManagedBean
@ViewScoped
public class MyBean implements Serializable
{
    private static final long serialVersionUID = 1L;

    private double progress = 0d;
    private String message = "ready";

    public String longOperation() throws InstantiationException, IllegalAccessException
    {
        for(int i = 0; i < 100; i++)
        {
            // simulate a heavy operation
            progress++;
            message = "processing [" + i + "]";
            Thread.sleep(1000);
        }

        message = "completed";

        return "result";
    }

    public double getProgress()
    {
        return progress;
    }

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

    public String getMessage()
    {
        return message;
    }

    public void setMessage(String message)
    {
        this.message = message;
    }
}