Using TagLib to avoid changes due to change in URLMappings

14 / Feb / 2011 by Mohd Farid 0 comments

Recently, I used Taglib to centralize the effect of URL mapping related changes of my grails application.

[java]
def userPageLink = {attrs, body ->

def user = User.read(attrs.id)

out << g.link(controller: ‘user’, action: ‘show’, params: [name: user.name]) {body()}
}
[/java]

So wherever I need a link to user page, I can use this Taglib instead of g:link.

[java]
<hys:userPageLink id="${user.id}">${user.name}</hys:userPageLink>
[/java]

Advantage:


Whenever I change the URL Mappings, there is no need to update all the g:link in all my gsp files. I would just update my taglib to cater to new URL Mapping.

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *