wood burning stoves 2.0*
The moose likes JSP and the fly likes Recursive JSP function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Recursive JSP function" Watch "Recursive JSP function" New topic
Author

Recursive JSP function

Anthony Jacksony
Greenhorn

Joined: Mar 27, 2012
Posts: 4
Hi,

I'm a bit new with jsp and now i'm facing a bit of a trouble trying to build a recursive function. What happens is I have an object which i sent by json to a variable in the jsp. That object has a few attributes and also an arrayList with his children objects of the same type as him. So I've realized I can get to his children but after that I can't without repeating the code, therefore the need of a recursive function to help me go through the children objects. Is there a good way to do this? I thought about using a java function on the jsp but how am i suppose to pass the variable into the function and turn it into a service?

Thanks in advance.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
I would suggest doing it with a custom tag file.
You can then recursively invoke your custom tag.

Here is a quick and stupid example I put together to demonstrate the idea.

/WEB-INF/tags/treePrinter.tag


And something that invokes it: treePrinter.jsp



Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
Another approach (after a bit more thought) would be to pass the JSON to the client, and run some javascript code to produce the appropriate HTML, using YUI or whatever javascript library you prefer to help you on the way.
I'm presuming its a component of some kind that you're populating with data, so encapsulating that component in a javascript library should be relatively straight forward.

The benefit of that is you pass the json directly to the page, and have the javascript component worry about rendering itself.
Similar sort of separation to the custom tag example I gave, just a different approach/implementation.
Anthony Jacksony
Greenhorn

Joined: Mar 27, 2012
Posts: 4
Thanks for the help. In my case im not using a LinkedHashMap but an ArrayList. Does it work using the same logic but replacing those components? I'd like also to build the html with it, i suppose I do that within the tag right?

For example:


<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="tag"%>
<%@ attribute name="List" type="java.util.ArrayList"%>

<c:forEach var="ListItem" items="${List}">
<li>${ListItem.name}</li>
<c:if test="${not empty ListItem.childrenList}">
<ul>
<tag:ListPrinter List="${ListItem.childrenList}"/>
</ul>
</c:if>
</c:forEach>
Anthony Jacksony
Greenhorn

Joined: Mar 27, 2012
Posts: 4
This is my .jsp which invokes the tag. I'm getting an error now Unable to find setter method for attribute: List. Probably it's a conversion error no?

...
<li><a href="javascript:void(0);" onclick="javascript:callView('<c:url value="${ListItem.controllerName}"/>')" ><fmt:message key="${ListItem.name}" /></a>
<c:if test="${fn:length(ListItem.children_list) gt 0}">
<ul>
<tag:ListPrinter List="${ListItem.children_list}"/>
</ul>
</c:if>
</li>
...

How can I set the attribute to be received on the tag?
Anthony Jacksony
Greenhorn

Joined: Mar 27, 2012
Posts: 4
Thanks for all the help, I just solved it, i used the example i posted before but i was forgetting about initializing the Collection variable on the java. :)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Recursive JSP function