Grails UrlMappings for an unknown number of variables

I am using url mappings to translate the directory structure of URLs into categories within the site, currently using:

class UrlMappings {

    static excludes = ['/css/*','/images/*', '/js/*', '/favicon.ico']
    static mappings = {       

        "/$category1?/$category2?/$category3?/"(controller: 'category')

        "500"(view:'/error')
        "404"(view:'/notFound')
    }
}

It currently supports categories at three levels. I would like to be able to support levels of categories N, where N> = 1.

How can this be achieved?

+5
source share
2 answers

An asterisk, both single and double, is used for wilcard url mapping .

A single asterisk will match anything at this level:

static mappings = {
    "/images/*.jpg"(controller:"image")
}

// Matches /images/logo.jpg, images/header.jpg and so on

A double asterisk will match any value on more than one level:

static mappings = {
    "/images/**.jpg"(controller:"image")
}

// Matches /images/logo.jpg, /images/other/item.jpg and so on

? :

class UrlMappings {

    static excludes = ['/css/*','/images/*', '/js/*', '/favicon.ico', '/WEB-INF/*']
    static mappings = {
        "/**?"(controller: 'category')

        "500"(view:'/error')
        "404"(view:'/notFound')       
    }
}
+6

, URL

"/categories/$categories**?"(controller:categories)

URI param

  /categories/animals/dogs/retrievers

  ///goes to categories controller and has...
  params.categories //= "animals/dogs/retrievers"

,

0