Tag - link

Tag - link

Description

Creates an html anchor tag with the href set based on the parameters specified.

Parameters

  • action (optional) - the name of the action to use in the link, if not specified the default action will be linked
  • controller (optional) - the name of the controller to use in the link, if not specified the current controller will be linked
  • id (optional) - the id to use in the link
  • params (optional) - a map containing request parameters
  • url (optional) - a map containing the action, controller, id etc.
  • fragment (optional) - links to this anchor on the page

Examples

Example controller for an application called "shop":

class BookController {
     def defaultAction="list"
     def list = { [ books: Book.list( params ) ] }
     def show = { [ book : Book.get( params['id'] ) ] }
}

Example usages for above controller:
<g:link action="show" id="1">Book 1</g:link>
<g:link action="show" id="1" fragment="description">Book 1 Description</g:link>
<g:link action="show" id="${currentBook.id}">${currentBook.name}</g:link>
<g:link controller="book">Book Home</g:link>
<g:link controller="book" action="list">Book List</g:link>
<g:link url="[action:'list',controller:'book']">Book List</g:link>
<g:link action="list" params="[sort:'title',order:'asc',author:currentBook.author]">
     Book List
</g:link>

${foo} expansions will NOT work in the params parameter (although they will work in the id parameter).

One way to insert a variable inside the params parameter is to use the def tag and refer to the var inside the params attribute. For example:
<g:def var="fooVar" value="${foo}"/>
<g:link controller="book" action="list" params="[fooParam: fooVar]">Book List</g:link>


Example as a method call in GSP only:
<%= link(action:'list',controller:'book') { 'Book List' }%>

Results in:
<a href="/shop/book/list">Book List</a>

An "it" variable in the body of a nested tag (i.e. if you have a g:link nested in a g:each) generally runs into compilation problems. This can be easily resolved by naming your parameter in the g:each by passing a var parameter.

1 Comment

  • Gravatar
    "${foo} expansions will NOT work in the params parameter (although they will work in the id parameter). "

    I think this is wrong. I could use the expansion in my template. Like this

    <g:link action="download" params='filePath:"${fileResourceInstance}" (+)'> Download </g:link>

    I think one just needs to mind the quote: use single quote "'" to wrap around the params.

    Mar 30, 2009 16:03 PM dmly_us

Post a Comment