Вопрос

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?

Это было полезно?

Решение

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;
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top