In a Grails application we can organize our controllers into packages, but if we use the same name for multiple controllers, placed in different packages, then Grails cannot resolve the correct controller name. Grails ignores the package name when finding a controller by name. But with namespace support since Grails 2.3 we can have controllers with the same name, but we can use a namespace property to distinguish between the different controllers.
We can add a new static property to a controller class with the name namespace
. The value of this property defines the namespace. We can then write new URL mappings in the grails-app/conf/UrlMappings.groovy
file and use the namespace value as a mapping attribute.
Suppose we have two ReportController
classes in our application. One is defined as com.mrhaki.grails.user.ReportController
and the other as com.mrhaki.grails.common.ReportController
. The following code samples show sample implementations for both controllers:
// File: grails-app/controllers/com/mrhaki/grails/user/ReportController.groovy package com.mrhaki.grails.user class ReportController { /** Namespace is set to user. Used in URLMappings. */ static namespace = 'user' def index() { render 'UserReport' } }
And the second controller:
// File: grails-app/controllers/com/mrhaki/grails/common/ReportController.groovy package com.mrhaki.grails.common class ReportController { /** Namespace is set to common. Used in URLMappings. */ static namespace = 'common' def index() { render 'CommonReport' } }
In our UrlMappings.groovy
file we can now add two extra mappings for these controllers and we use the new namespace
attribute to point the mapping to the correct controller implementation.
// File: grails-app/conf/UrlMappings.groovy class UrlMappings { static mappings = { // Define mapping to com.mrhaki.grails.user.ReportController with namespace user. "/user-report/$action?/$id?(.${format})?"(controller: 'report', namespace: 'user') // Define mapping to com.mrhaki.grails.common.ReportController with namespace common. "/common-report/$action?/$id?(.${format})?"(controller: 'report', namespace: 'common') // Other mappings. "/$controller/$action?/$id?(.${format})?"() "/"(view: "/index") "500"(view: '/error') } }
The namespace support is also useful in building RESTful APIs with Grails. We can use the namespace attribute to have different versions for the same controller. For example in the following UrlMappings.groovy
configuration we have two mappings to a controller with the same name, but the namespace attribute defines different version values:
// File: grails-app/conf/UrlMappings.groovy class UrlMappings { static mappings = { // Define mapping to com.mrhaki.grails.api.v1.UserController with namespace v1. "/api/v1/users"(resource: 'user', namespace: 'v1') // Define mapping to com.mrhaki.grails.api.v2.UserController with namespace v2. "/api/v2/users"(resource: 'user', namespace: 'v2') // Other mappings. "/"(controller: 'apiDoc') "500"(controller: 'error') } }
To create links to controllers with a namespace we can use the new namespace
attribute in the link
and createLink
tags. The following GSP page part shows how we can set the namespace so a correct link is generated:
<h2>Links</h2> <ul> <li><g:link controller="report" namespace="user">User Reports</g:link></li> <li><g:link controller="report" namespace="common">Common Reports</g:link></li> <li><g:createLink controller="report" namespace="user"/></li> <li><g:createLink controller="report" namespace="common"/></li> </ul>
We get the following HTML:
<h2>Links</h2> <ul> <li><a href="/namespace-controller/user-report/index">User Reports</a></li> <li><a href="/namespace-controller/common-report/index">Common Reports</a></li> <li>/namespace-controller/user-report/index</li> <li>/namespace-controller/common-report/index</li> </ul>
Code written with Grails 2.3.2.