You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-user@tomcat.apache.org by Eugeny N Dzhurinsky <eu...@jdevelop.com> on 2005/08/17 12:29:10 UTC

JSTL and recursion

Is there any way to create recursive lists using the JSTL?
For example I have the Project object, which have the list of top-level
messages. I am doing something like this

<c:forEach var="message" items="${project.message}">
<li>${message.subject}</li>
</c:forEach>

the message object contains the list of the messages, related to this message,
each of the messages in this list contains the list of the messages for
this emssage etc. I'd like to build the message thread. How can I do this
using the JSTL?

-- 
Eugene N Dzhurinsky

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Re: JSTL and recursion

Posted by Eugeny N Dzhurinsky <eu...@jdevelop.com>.
On Wed, Aug 17, 2005 at 11:44:51AM +0100, Murray Steele wrote:
> You could do something like:
> top-level.jsp:
> <c:set var="recurse_message" value="${project}" scope="request"/>
> <c:import url="recurse.jsp"/>
> <c:remove var="recurse_message" scope="request"/>
>
> recurse.jsp:
> 
> <c:set var="my_message" value="${requestScope.recurse_message}"/>
> <ul>
> <c:forEach var="message" items="${my_message.messages}">
>   <li>${message.subject}
>     <c:set var="recurse_message" value="${message}" scope="request"/>
>     <c:import url="recurse.jsp"/>
>   </li>
> </c:forEach>
> </ul>
> <c:set var="recurse_message" value="${my_message}" scope="request" />
> 
> I did something similar a while back.  It seemed to work.  The final 
> c:set in recurse.jsp might be redundant, but you have no way of knowing 
> if the "calling" jsp will want to do something else with the 
> recurse_message in request scope that it sent you, so it's probably 
> safest to reset it to your value.
> 
> There may be a better solution tho' (doesn't Programming 101 suggest 
> you can rewrite most recursion with cunning loops instead?).

Well, this may be true... But i can't beleive there is no trick to avoid the
JSP reinclusion...

-- 
Eugene N Dzhurinsky

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Re: JSTL and recursion

Posted by Murray Steele <mu...@peoplesarchive.com>.
You could do something like:

top-level.jsp:

<c:set var="recurse_message" value="${project}" scope="request"/>
<c:import url="recurse.jsp"/>
<c:remove var="recurse_message" scope="request"/>

recurse.jsp:

<c:set var="my_message" value="${requestScope.recurse_message}"/>
<ul>
<c:forEach var="message" items="${my_message.messages}">
   <li>${message.subject}
     <c:set var="recurse_message" value="${message}" scope="request"/>
     <c:import url="recurse.jsp"/>
   </li>
</c:forEach>
</ul>
<c:set var="recurse_message" value="${my_message}" scope="request" />

I did something similar a while back.  It seemed to work.  The final 
c:set in recurse.jsp might be redundant, but you have no way of knowing 
if the "calling" jsp will want to do something else with the 
recurse_message in request scope that it sent you, so it's probably 
safest to reset it to your value.

There may be a better solution tho' (doesn't Programming 101 suggest 
you can rewrite most recursion with cunning loops instead?).

Muz

On 17 Aug 2005, at 11:29, Eugeny N Dzhurinsky wrote:

> Is there any way to create recursive lists using the JSTL?
> For example I have the Project object, which have the list of top-level
> messages. I am doing something like this
>
> <c:forEach var="message" items="${project.message}">
> <li>${message.subject}</li>
> </c:forEach>
>
> the message object contains the list of the messages, related to this 
> message,
> each of the messages in this list contains the list of the messages for
> this emssage etc. I'd like to build the message thread. How can I do 
> this
> using the JSTL?
>
> -- 
> Eugene N Dzhurinsky
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: taglibs-user-help@jakarta.apache.org
>
>
--
Murray Steele
Senior Developer

Peoples Archive
w: http://www.peoplesarchive.com
t: 0207 323 0323
d: 0207 631 9147

This email has been scanned by Postini.
For more information please visit http://www.postini.com


---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org