本文介绍了Grails Webflow:在操作或转换状态之外访问流程范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想调用控制器未知的子流。它通过参数传递给beginFlow,并将其保存在流程范围中。在goToForm里我想调用保存在flow.theController中的控制器。




  def beginFlow = {
输入{
action如果(flow.theController()){
if(params?.redirectTo!= null){
String flow.theController = params.redirectTo
}

{ (begin)
}
beginPage {
on ('next')。('goToForm')
}
goToForm {
//我想这样:
//子流(controller:flow.theController,action :'start'

//这个子流程可以工作,但不适用于所有情况
subflow(controller:'AZ_A4',action:'start')
on ('notDone')。('beginPage')

$ b showResults {
redirect(action :'results')
}
}


解决方案

这是不可能的,因为在构建流结构时(在应用程序启动时)必须知道子流名称。但是由于流程定义DSL是Groovy代码,你可以这样做:

  beginPage {
on('next ').to('selectSubflow')
}
selectSubflow {
action {
returnsubflow _ $ {flow.theController}()
} $ b $ (子控制器)的子控制器($ {
on(subflow _ $ {subController})。(subflow _ $ {subController})
}
}
('done')。('showResults')$ {
subflowler} {
subflowler(controller:subController,action:'start')
on ('notDone')。('beginpage')
}
}

listOfControllers可以是静态的,或者你可以在流程定义的顶部做这样的事情。

c> def beginFlow = {
def listOfControllers = grailsApplication.controllerClasses.findAll {
it.fl ows.containsKey('start')
} .collect {it.logicalPropertyName}
输入{
// ...

枚举定义startFlow的应用程序中的所有控制器。您可能需要在您的课程中使用 def grailsApplication ,我总是会忘记Grails中哪些地方默认提供它,哪些地方不提供...


I want to call a subflow where the controller is not known. It is passed in parameters to beginFlow and I save that in flow scope. Inside goToForm I'd like to call use the controller that is saved in flow.theController.


def beginFlow = {
    enter {
        action {
            if (params?.redirectTo != null) {
                String flow.theController = params.redirectTo
            }

            if ( flow.theController() ) { 
                success()
            }
        }
        on("success").to("beginPage")
    }
    beginPage {
        on('next').to('goToForm')
    }       
    goToForm {
                    // I'd like this:
                    // subflow(controller: flow.theController, action:'start'

                    // this subflow works, but won't work for all cases
        subflow(controller: 'AZ_A4', action:'start')
        on('done').to('showResults')
        on('notDone').to('beginPage')
    }

    showResults {
        redirect(action: 'results')
    }
}
解决方案

As discussed on the user list, it appears that this isn't possible directly, as the subflow name has to be known at the time when the flow structure is being built (at application startup). But since the flow definition DSL is Groovy code you can do something like this:

beginPage {
    on('next').to('selectSubflow')
}
selectSubflow {
    action {
        return "subflow_${flow.theController}"()
    }
    for(subController in listOfControllers) {
        on("subflow_${subController}").to("subflow_${subController}")
    }
}
for(subController in listOfControllers) {
    "subflow_${subController}" {
        subflow(controller:subController, action:'start')
        on('done').to('showResults')
        on('notDone').to('beginPage')
    }
}

The listOfControllers could be a static somewhere, or you could possibly do something like this at the top of the flow definition

def beginFlow = {
    def listOfControllers = grailsApplication.controllerClasses.findAll {
        it.flows.containsKey('start')
    }.collect { it.logicalPropertyName }
    enter {
        // ...

to enumerate all the controllers in the application that define a startFlow. You might need a def grailsApplication in your class, I always forget which places in Grails have it available by default and which don't...

这篇关于Grails Webflow:在操作或转换状态之外访问流程范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 23:05