Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Grails Goodness: Grouping URL Mappings

DZone's Guide to

Grails Goodness: Grouping URL Mappings

· Java Zone
Free Resource

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

We can group URL mappings defined in grails-app/conf/UrlMappings.groovy using the group() method defined for the URL mapping DSL. The first argument is the first part of the URL followed by a closure in which we define mappings like we are used to.

Suppose we have defined the following two mappings in our UrlMappings.groovy file, both starting with /admin:

// File: grails-app/conf/UrlMappings.groovy
class UrlMappings {

    static mappings = {
        // Mappings starting both with /admin:
        "/admin/report/$action?/$id?(.${format})?"(controller: 'report')
        "/admin/users/$action?/$id?(.${format})?"(controller: 'userAdmin')

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

We can rewrite this and use the group() method to get the following definition:

// File: grails-app/conf/UrlMappings.groovy
class UrlMappings {

    static mappings = {
        // Using grouping for mappings starting with /admin:
        group("/admin") {
            "/report/$action?/$id?(.${format})?"(controller: 'report')
            "/users/$action?/$id?(.${format})?"(controller: 'userAdmin')
        }

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

When we use the createLink and link tags the group is taken into account. For example when we use <g:createLink controller="userAdmin"/> we get the following URL if the application name is sample/sample/admin/users.

Code written with Grails 2.3.

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}