如何找到应用程序中运行的所有 Controller ?
我正在尝试使用 YUI 创建一个菜单,其中只有注册的 Controller 才会显示菜单。 Controller 类将创建一个静态列表,其中包含详细说明名称、操作等的各种属性(很像 grails-nav 插件)。
我想创建一个标签库,它可以找到所有 Controller ,识别哪些 Controller 具有此静态列表,然后查看每个列表并构建一个菜单。
我认为我可以使用 ControllerGrailsClass.metaClass.hasProperty 来识别给定 Controller 是否具有静态属性 - 但如何找到所有要询问的 Controller 类?
提前致谢
最佳答案
您可以从 GrailsApplication 对象获取一个列表。示例:
class TestController {
def grailsApplication // gets injected automatically
def test = {
grailsApplication.controllerClasses.each { controllerArtefact ->
def controllerClass = controllerArtefact.getClazz()
println "$controllerArtefact, $controllerClass"
}
}
}
如果您不在 Controller 中,您可以像这样获取 grails 应用程序对象:
import org.codehaus.groovy.grails.commons.ApplicationHolder
def grailsApplication = ApplicationHolder.application
关于grails - 查找应用程序中的所有 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3637046/