grails - 查找应用程序中的所有 Controller

标签 grails

如何找到应用程序中运行的所有 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/

相关文章:

grails - 在一个字段grails域上应用AND条件

database - 使用 ORM 进行数据验证

jquery - Spring Security Core authAjax,如何忽略 Referer

ajax - Grails文件下载-客户端

javascript - 如何捕获javascript中异步调用的错误?

chalice : Difference between\${} and ${}

grails - 如何在Grails中设置服务方法缓存

grails - 2.5.4 Assets 管道无法正常工作

java - chalice /hibernate : No row with the given identifier exists

rest - Grails应用程序可以同时包含REST客户端和服务器吗?