Question

Is it possible to add multiple values in the id attribute of glink tag?

I want to get a url like: /place/view/home/7? (/< controller >/< action >/< name >/< id >?params)

is it possible with glink tag?

Was it helpful?

Solution

If I understand correctly what you're looking for is the params option,

<g:link controller="cname" action="act" id="1" params="[sort: 'title']" />

OTHER TIPS

I use the following solution in Grails v2.4.3:

In UrlMappings.groovy, I added:

static mappings = {
    ...
    name dualId: "/$controller/$action/$id/$id2?(.$format)?" {}

    /* Usage:
     *  <g:link mapping="dualId" action="myMethod" params='[id2: "${id2Value}"]'>
     *    Accept id AND id2
     *  </g:link>

     *  Generates following link in the UI: 
     *    <a href="/myController/myMethod/142/387">Accept id AND id2</a>
     *  SPECIAL NOTE: params='[id2: "${id2Value}"]' works.
     *    using: params="[id2: '${id2Value}']" DOES NOT WORK!
     */
    ...
}

For your requirements, "/place/view/home/7? (/< controller >/< action >/< name >/< id >?params)", you could use:

static mappings = {
    ...
    name placeViewHome: "/$controller/$action/$name/$id?" {}

    /* Usage:
     *  <g:link mapping="placeViewHome" action="myAction" params='[name: "${nameValue}", q: "str"]'>
     *    Accept name AND id
     *  </g:link>

     *  Should generate the following link in the UI: 
     *    <a href="/myController/myMethod/myName/387?q=str">Accept name AND id</a>
     *  I didn't try it, but you should be able to add in other params and I would expect them to appear after the question mark as usual.
     */
    ...
}

See Grails documentation, 8.4.11 Named URL Mappings, for more detail...

Note: In the g:link, the order of the quotes mattered, double quotes inside single quotes worked, but not the other way around.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top