Grails: reverse url mapping to generate urls ending with “.html”

28 / Aug / 2009 by Amit Jain 2 comments

Hi Friends,

Recently I came across a problem, where the url of the links were to be generated dynamically based on the controller and action to be executed and  should end with “.html”. Reverse url mappings came to my rescue, which can be implemented using <g:link> GSP tag.
Then I tried this,

In UrlMappings.groovy file :

class UrlMappings {
    static mappings = {
      ...
      "/contact(controller:'myController', action:'myAction')
    }

And in my gsp file :

<g:link controller="myController" action="myAction"> myLink</g:link>

The above code gave me this url “http://serverURL.com/contact”. I wanted not only “contact” at the end but also “.html” text to be appended with it. So for that I added one more entry to my UrlMappings.groovy file. Now it looks like this :

class UrlMappings {
    static mappings = {
      ...
      "/contact.html(controller:'myController', action:'myAction')
      "/contact(controller:'myController', action:'myAction')
    }
}

In the above code, both lines are required to make the url ending with “.html”. Only “/contact.html” mapping won’t work. Since for the url “http://serverURL.com/contact.html”, UrlMappings file is searched only for contact and “.html” gets truncated. Therefore we need to have “/contact.html” mapping also.
Don’t forget, the mappings specified above has to be in the same order.

Cheers!!
~~Amit Jain~~
amit@intelligrape.com

http://www.tothenew.com/blog/

FOUND THIS USEFUL? SHARE IT

comments (2)

Leave a comment -