Another great feature of URL mappings is that they automatically customize the behaviour of the link tag so that changing the mappings don’t require you to go and change all of your links.

This is done through a URL re-writing technique that reverse engineers the links from the URL mappings. So given a mapping such as the blog one from an earlier section:

  1. static mappings = {
  2. "/$blog/$year?/$month?/$day?/$id?"(controller:"blog", action:"show")
  3. }

If you use the link tag as follows:

  1. <g:link controller="blog" action="show"
  2. params="[blog:'fred', year:2007]">
  3. My Blog
  4. </g:link>
  5. <g:link controller="blog" action="show"
  6. params="[blog:'fred', year:2007, month:10]">
  7. My Blog - October 2007 Posts
  8. </g:link>

Grails will automatically re-write the URL in the correct format:

  1. <a href="/fred/2007">My Blog</a>
  2. <a href="/fred/2007/10">My Blog - October 2007 Posts</a>