【问题标题】:Grails GSP display Service variable where process time is being calculatedGrails GSP 显示正在计算处理时间的服务变量
【发布时间】:2015-03-12 13:48:55
【问题描述】:

我试图在视图中显示在服务中运行的进程的进度。我的控制器调用第一个向第三方发送信息的服务,我通过调用第二个服务来记录每次发生这种情况,该服务进行计算以显示请求完成的百分比。

我有我想在第一个服务中更新的值,但我不确定如何用这个值更新视图。在工作完成之前,我不会返回控制器。

第一次服务

    def batch(toCalculate,otherStuffDoneTotal otherData) {

        def processPercentage

        toCalculate.eachWithIndex(){ data, i ->

            def otherMethod = otherMethod1(doStuffHere)
            def progress = secondService.progressCalc(otherStuffDone, otherStuffDoneTotal, processPercentage)
            }
     }

二次服务

    def progressCalc(otherStuffDoneTotal, otherStuffDone, processPercentage) {

        def retItems
        def retObjs = [:]

        processPercentage = 100 / otherStuffDoneTotal * otherStuffDone

        retItems = [processPercentage: processPercentage]
        retObjs.putAll(retItems)
        return(retOjbs)

    }

如何让视图动态更新第一个服务的值(progress.progressPercentage)

【问题讨论】:

    标签: grails service gsp


    【解决方案1】:

    任务不是那么简单。

    这里有 3 个选项:

    1. 短轮询:创建控制器动作以返回当前百分比,并使用 AJAX 函数调用动作并在视图中显示进度。
    2. 长轮询/异步更新。有许多框架支持这一点,在 Java 世界中,我推荐 vertx.io,它为您提供了在服务器状态更改时更新视图的功能
    3. 假的。让您的 javascript 假装从服务器轮询数据,并以某种速度更新百分比。这个解决方案远非完美,但从用户体验的观点来看已经足够好了

    【讨论】:

    • 不幸的是,我无法伪造它,因为处理时间取决于我在处理时获得的值。该服务还可以通过我的应用程序中的不同控制器访问,因此我试图将所有内容保持在最低限度。听起来短轮询是要走的路,如何在将数据处理到控制器中的不同方法的同时返回值 processPercentage,然后我可以使用 AJAX。还是应该因为多次使用而只为轮询创建一个不同的控制器而不必复制代码?
    • 我建议使用strategies。你要的是implementation details。
    猜你喜欢
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 2011-11-18
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多