can not reach iterator on gsp from inside link tag

View: New views
5 Messages — Rating Filter:   Alert me  

can not reach iterator on gsp from inside link tag

by alebo611 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi

From inside a gsp each loop, this will work:

<g:set var="title" value="${it.title}"/>
<td><g:link controller="document" id="${it.id}" action="show">${title}</g:link></td>

but accessing 'it' directly in link will fail:

<td><g:link controller="document" id="${it.id}" action="show">${it.title}</g:link></td>    <!--CAUSES NULLPOINTER -->

I just want to know if I should view this as a "bug" or as something "normal". If the latter is true, please explain why!

Re: can not reach iterator on gsp from inside link tag

by DaveKlein :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Could you post a bit more of your code, both working and failing versions?

Thanks,
Dave





--
Grails: A Quick-Start Guide
http://pragprog.com/titles/dkgrails
http://gquick.blogspot.com

Re: can not reach iterator on gsp from inside link tag

by alebo611 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

This works:

<g:each in="${instance.documents}">
                                                <tr>
                                                    <td>${it.docType}</td>
                                                    <g:set var="title" value="${it.title}"/>
                                                    <td><g:link controller="document" id="${it.id}" action="show">${title}</g:link></td>
                                                <tr>
                                        </g:each>

This fails:

<g:each in="${instance.documents}">
                                                <tr>
                                                    <td>${it.docType}</td>
                                                    <g:set var="title" value="${it.title}"/>
                                                    <td><g:link controller="document" id="${it.id}" action="show">${it.title}</g:link></td>
                                                <tr>
                                        </g:each>

As you can see, its the link name that is the only diff.


thanks


DaveKlein wrote:
Could you post a bit more of your code, both working and failing versions?

Thanks,
Dave





--
Grails: A Quick-Start Guide
http://pragprog.com/titles/dkgrails
http://gquick.blogspot.com

Re: can not reach iterator on gsp from inside link tag

by DaveKlein :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

This is mentioned here: http://grails.org/GSP+Tag+-+each

"Note that var must be specified when the iterator value is to be used from within the body of a GSP Dynamic Tag, such as in g:link See the example below. " 
As for the specific reason why this is the case, I guess we would have to look at the source code for the g:each tag, but that's written in Java and who wants to read Java code?  :-)

Is there a reason that you don't want to use a var for the iterator?  Something like:

<g: each in="${instance.documents}" var="doc">
    <g:link controller="document" id="${doc.id}" action="show">${doc.title}</g:link>
</g:each>


As a bonus the var can be set to something a bit more descriptive than "it" .

Dave




--
Grails: A Quick-Start Guide
http://pragprog.com/titles/dkgrails
http://gquick.blogspot.com

Re: can not reach iterator on gsp from inside link tag

by alebo611 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Allright then I will use "var". Thank you for the answer.



DaveKlein wrote:
This is mentioned here: http://grails.org/GSP+Tag+-+each

"Note that var must be specified when the iterator value is to be used from
within the body of a GSP Dynamic
Tag<http://www.grails.org/Dynamic+Tag+Libraries>,
such as in g:link See the example below. "

As for the specific reason why this is the case, I guess we would have to
look at the source code for the g:each tag, but that's written in Java and
who wants to read Java code?  :-)

Is there a reason that you don't want to use a var for the iterator?
Something like:

<g: each in="${instance.documents}" var="doc">
    <g:link controller="document" id="${doc.id}"
action="show">${doc.title}</g:link>
</g:each>


As a bonus the var can be set to something a bit more descriptive than "it"
.

Dave




--
Grails: A Quick-Start Guide
http://pragprog.com/titles/dkgrails
http://gquick.blogspot.com