开发者

Finding all controllers in application

How do i find all the controllers running in an application ?

I am trying to create a menu using YUI where only registered controllers will have a menu shown. A controller class will create a static list with various properties detailing name, action, etc. (much like grails-nav plugin).

I want to create a taglib that can find all controllers, identify which ones have this static list then look into each list and build up a menu.

I think i can use ControllerGrailsClass.metaClass.hasProperty to identify whether a given controller has the static property - but how do I find all the Controller classes to interrogate ?

Thanks in advance开发者_Python百科


You can get a list from the GrailsApplication object. Example:

class TestController {

    def grailsApplication // gets injected automatically

    def test = {
        grailsApplication.controllerClasses.each { controllerArtefact ->
            def controllerClass = controllerArtefact.getClazz()
            println "$controllerArtefact, $controllerClass"
        }
    }
}

If you're not in a controller, you can get a hold of the grails application object like so:

import org.codehaus.groovy.grails.commons.ApplicationHolder

def grailsApplication = ApplicationHolder.application
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜