8.3.8 Mapping Wildcards

Grails' URL mappings mechanism also supports wildcard mappings. For example consider the following mapping:

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

This mapping will match all paths to images such as /image/logo.jpg. Of course you can achieve the same effect with a variable:

  1. static mappings = {
  2. "/images/$name.jpg"(controller: "image")
  3. }

However, you can also use double wildcards to match more than one level below:

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

In this cases the mapping will match /image/logo.jpg as well as /image/other/logo.jpg. Even better you can use a double wildcard variable:

  1. static mappings = {
  2. // will match /image/logo.jpg and /image/other/logo.jpg
  3. "/images/$name**.jpg"(controller: "image")
  4. }

In this case it will store the path matched by the wildcard inside a name parameter obtainable from the params object:

  1. def name = params.name
  2. println name // prints "logo" or "other/logo"

If you use wildcard URL mappings then you may want to exclude certain URIs from Grails' URL mapping process. To do this you can provide an excludes setting inside the UrlMappings.groovy class:

  1. class UrlMappings {
  2. static excludes = ["/images/*", "/css/*"]
  3. static mappings = {
  4. ...
  5. }
  6. }

In this case Grails won’t attempt to match any URIs that start with /images or /css.