Wednesday, September 2, 2009

Grails : reverse url mappings using g:link tag

Hi friends,

Reverse url mappings is needed when url is to be generated based on controller and the action. It can be implemented using g:link tag or createLink.


UrlMappings.groovy
"/book/$title"(controller:'book', action:'show')


When the controller and action name are variables, could be any and we need to get the right url. Then we can use g:link


controlller="book"
actionName="show"
myTitle="Grails-in-action"

<g:link controller="$controllerName" action="$actionName" params="[title:myTitle]"/>


The generated url would be "/book/Grails-in-action"

One more thing, since g:link tag generates anchor tag at the end, we can g:link tag around button, image etc. just like the anchor tag.
Hope the above code helps.

No comments:

Post a Comment