Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding all controllers in application

Tags:

grails

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

like image 982
Primus Avatar asked Sep 03 '10 15:09

Primus


People also ask

What is the use of many controllers in application?

An AngularJS application can contain one or more controllers as needed, in real application a good approach is to create a new controller for every significant view within the application. This approach will make your code cleaner and easy to maintain and upgrade.

Can a controller have multiple models?

Answer: yes. Create a wrapper model, put other two models in it.

Which object is used to access the model data in controllers?

The ViewBag is a dynamic object that provides a convenient late-bound way to pass information to a view. MVC also provides the ability to pass strongly typed objects to a view template. This strongly typed approach enables better compile-time checking of your code and richer IntelliSense in the Visual Studio editor.

Can two controllers have same view?

Yes, put the view in shared folder. This will automatically make view available across multiple controllers.


1 Answers

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
like image 173
ataylor Avatar answered Oct 08 '22 15:10

ataylor