Home » JVM Languages » Groovy » Grails Goodness: Namespace Support for Controllers

About Hubert Ikkink

My name is Hubert A. Klein Ikkink also known as mrhaki. I work at the great IT company JDriven. Here I work on projects with Groovy & Grails, Gradle and Spring. At JDriven we focus on SpringSource technologies. All colleagues want to learn new technologies, support craftmanship and are very eager to learn. This is truly a great environment to work in.

Grails Goodness: Namespace Support for Controllers

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.
            controller: 'report',
            namespace: 'user'
        // Define mapping to com.mrhaki.grails.common.ReportController with namespace common.
            controller: 'report',
            namespace: 'common'
        // Other mappings.
        "/"(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:

    <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>

We get the following HTML:

    <li><a href="/namespace-controller/user-report/index">User Reports</a></li>
    <li><a href="/namespace-controller/common-report/index">Common Reports</a></li>
  • Code written with Grails 2.3.2.


Reference: Grails Goodness: Namespace Support for Controllers from our JCG partner Hubert Ikkink at the JDriven blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....


Leave a Reply

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


seven − = 2

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.