Grails 2.2.0 : Namespacing to resolve between controllers of same name

24 / Sep / 2012 by Vivek Krishna 0 comments

Grails 2.2.0.RC1 was released last week and a new feature to make an entry into the version of the framework is the support for namespaces for controllers and services so that there is no naming conflicts while adding artefacts of the same name from different plugins or within the application.

I created a sample app and installed the grails console plugin. This brought a ConsoleController to the application.

[java]
grails install-plugin console
[/java]

Next, I added a controller with the same name, ConsoleController.groovy in the application.

[java]
grails create-controller console
[/java]

To resolve the two at a mappings level, we need to add an entry in UrlMappings.groovy so that the two URLs are different.

[java]
//UrlMappings.groovy
class UrlMappings {

static mappings = {
"/gconsole"{ //Resolve gconsole to ConsoleController in the plugin
controller = "console"
plugin = "console"
}
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}

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

[/java]

Now, if I want to hit the console controller from the application, I can navigate to: http://<app_base_url>/console

And to navigate to console controller from the plugin, I can navigate to: http://<app_base_url>/gconsole

To use this in the <g:link> tag, all we need to do is provide an extra attribute plugin and grails will create the URL accordingly.

This means that we can have choose better namings for our controllers without losing control(pun unintended) over their names.

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *