【问题标题】:How to execute task and show its progress in spring web flow如何在 Spring Web Flow 中执行任务并显示其进度
【发布时间】:2013-08-20 03:07:14
【问题描述】:

我必须自动执行任务并在输入流程时向客户端显示其进度。 任务完成后,它应该重定向到另一个流。 进度应该由 PrimeFaces p:progressBar 显示

有什么想法吗?

【问题讨论】:

    标签: java primefaces spring-webflow


    【解决方案1】:

    问题是一年多前提出的,但这里有一些代码 sn-ps,希望将来对其他人有所帮助:

        <h:panelGrid>
            <p:progressBar widgetVar="progressBar" ajax="true" value="#{archiveCopyCreator.progress}" interval="1000" labelTemplate="{value}%" styleClass="animated" style="width: 500px;">
                <p:ajax event="complete" listener="#{archiveCopyCreator.onComplete}"/>
            </p:progressBar>
            <h:panelGrid styleClass="centered">  
                <p:commandButton value="Avbryt" action="cancel" ajax="false" immediate="true" />
            </h:panelGrid>      
        </h:panelGrid>      
    
        <script type="text/javascript">
            $(document).ready(function() {
                progressBar.start();
            });
        </script>
    

    我创建了一个进度条,并根据我的一个 bean 中的值对其进行更新。完成后,将对 bean 进行 ajax 调用。进度条是使用 javascript 手动启动的。

    @Component
    public class ArchiveCopyCreator implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    protected Integer progress;
    
    public Integer getProgress() {
        if (this.progress == null) {
            this.progress = 0;
        } else {
            this.progress = this.progress + (int) (Math.random() * 15);
    
            if (this.progress > 100) {
                this.progress = 100;
            }
        }
    
        return this.progress;
    }
    
    public void setProgress(final Integer progress) {
        this.progress = progress;
    }
    
    public void cancel() {
        this.progress = null;
    }
    
    public void reset() {
        this.progress = null;
    }
    
    public boolean hasProgress() {
        return this.progress != null;
    }
    
    public void onComplete() {
        RequestControlContext requestContext = (RequestControlContext)   RequestContextHolder.getRequestContext();
        this.objektidentiteter = null;
        this.progress = null;
        requestContext.handleEvent(new Event(this, "complete"));
        return;
    }
    

    }

    在本例中使用 Math.random 伪造实际进度。

    请注意,onComplete() 是使用 progressBar 中的 ajax 调用的。该 javacode 会转换到我的下一个 Spring Webflow 状态,其 id 为“完成”。

    记得为每个用户/流创建一个新的支持 bean...在我的示例中,ArchiveCopyCreator 是一个 @Component,因为我想在流和用户之间共享它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-07
      相关资源
      最近更新 更多