3

For some reason I get a huge list of errors when using following code:

class UrlMappings {
    static grailsApplication
    static mappings = {

    grailsApplication.controllerClasses.each { controllerClass -> // FAILS!
        println(controllerClass.name)
    }

    "/$controller/$action?/$id?"{}

    "/"(view:"/index")
    "500"(view:'/error')
}

Errors: http://pastebin.com/tiEsENie


Where as following code works just fine and prints all the controller names:

class UrlMappings {
    static grailsApplication
static mappings = {

    "/$controller/$action?/$id?"{
        grailsApplication.controllerClasses.each { controllerClass -> // WORKS!
            println(controllerClass.name)
        }
    }

    "/"(view:"/index")
    "500"(view:'/error')
}
}

Isn't it possible to access the static grailsApplication from inside static mappings?

(I need to be able to get the controller names in order to dynamically create urlmappings)

Faizan S.
  • 8,634
  • 8
  • 34
  • 63
  • isn't using `$controller` essentially what you want? Why do you need to loop over the controllers and print them in the mappings anyway? – omarello Dec 17 '11 at 00:42
  • Also, possible duplicate? [http://stackoverflow.com/questions/4232884/dynamic-grails-url-mapping-config](http://stackoverflow.com/questions/4232884/dynamic-grails-url-mapping-config) – omarello Dec 17 '11 at 00:46
  • 1
    because I want to split all ModeratorControllers into /mod/action – Faizan S. Dec 17 '11 at 00:52
  • I looked into the given example but does not work since `ApplicationHolder` is deprecated in the current 2.0 release – Faizan S. Dec 17 '11 at 00:53
  • It's deprecated, but it's still there and works fine. – Burt Beckwith Dec 17 '11 at 01:06
  • have you looked at the source for the url mapping report generation? `./grailsw url-mappings-report` may give you what you want. – tmarthal Aug 18 '14 at 17:50

1 Answers1

2

While ApplicationHolder still works, the grails docs state this for the in the deprecation comments


deprecated: Use dependency injection or implement GrailsApplicationAware instead


Since grailsUrlMappingsHolderBean implements GrailsApplicationAware, I found that the code below works in 2.0 as well

class UrlMappings {
    static mappings = {        
        getGrailsApplication().controllerClasses.each{ controllerClass -> 
            if(controllerClass.name./*your logic here*/){
                "/mod/action" {
                    controller = "${controllerClass.name}"
                }
            }
        }
    }
}
omarello
  • 2,683
  • 1
  • 23
  • 23
  • 2
    this brings up an issue with my integration tests not working anymore.. could it be due to the fact that we are calling a nonstatic thing from a static environment? – Faizan S. Dec 21 '11 at 10:49
  • Breaks my integration tests as well. Worked around by wrapping with if (Environment.current != Environment.TEST) {..} – rlovtang Oct 05 '12 at 13:33