我想调用一个控制器未知的子流。它被传递到参数中以开始Flow,我将其保存在流范围内。在goToForm中,我想调用保存在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')
}
}
正如用户列表上所讨论的,这似乎不可能直接实现,因为在构建流结构时(在应用程序启动时)必须知道子流名称。 但是由于流定义DSL是Groovy代码,你可以做这样的事情:
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')
}
}
listOfControllers可以是静态的,或者你可以在流定义的顶部做这样的事情。
def beginFlow = {
def listOfControllers = grailsApplication.controllerClasses.findAll {
it.flows.containsKey('start')
}.collect { it.logicalPropertyName }
enter {
// ...
枚举应用程序中定义 startFlow 的所有控制器。 你可能在你的课堂上需要一个def grailsApplication
,我总是忘记Grails中的哪些地方默认可用,哪些地方没有......