Friday, April 1, 2011

Grails gotcha - controllers with same name in different packages

Well... it's tough, isn't it? You get a controller with the plugin you use and this damn controller has the same damn name as your controller. And this name is just perfect for what your controller does...

I've just found myself in this kind of situation. I asked on the Grails users group and found out only what I already knew - those damn classes have to have different names. Period.

But I'm the wild one. I need to bend software to my will. I just have to do it :D After a couple of hours of reading Grails sources and experimenting with Grails console (fricken best thing EVER!!!) I came up with this:
class BootStrap {

def grailsApplication

def init = { servletContext ->
grailsApplication.getArtefactInfo("Controller").grailsClasses.each {
it.uri2closureMap.put('/' + it.fullName, 'index')
it.uri2closureMap.put('/' + it.fullName + '/', "index")
it.uri2closureMap.put('/' + it.fullName + '/index', "index")
it.uri2closureMap.put('/' + it.fullName + '/index/**', "index")

def destroy = {
With this I can use fully qualified class names in my UrlMappings.groovy!

And as usual life is good again :D


Matthias Hryniszak said...

This solution is not without a glitch. The problem is that you need to manually call the render method to be able to spit out some results. That's bad, I know.
Another thing is that depending on which mapping kicked in (the default one or the one with long controller class name as controller name) the views folder will be different.
If the long one will be used the whole class name will have to be the folder name for the system to find the view.

Matthias Hryniszak said...

Naturally you can specify the full path to view (as in with the "/" at the beginning) and that would solve the issue of where to find the view file to render.