DevOps Zone is brought to you in partnership with:

Hi all, my name is Hubert A. Klein Ikkink. Not a very common name, right? To make things easier I just picked the first letters of my firstname and surname and came up with haki. So there you have it, now I am also known as Mr. Haki or mrhaki for short. You can read more blog postings at www.mrhaki.com. I am a passionate Groovy and Java developer based in Tilburg, The Netherlands. My goal is to write clean, elegant, user-centered and high quality software. You can find me on Google+ and Twitter. Hubert is a DZone MVB and is not an employee of DZone and has posted 162 posts at DZone. You can read more from them at their website. View Full User Profile

Grails Goodness: Namespace Support for Controllers

11.18.2013
| 6556 views |
  • submit to reddit

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.



Published at DZone with permission of Hubert Klein Ikkink, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)