How to present a recursive collection in JSP

I have a server service that returns an Info object to me. This Info object has a list of FolderGroup objects, which in turn have a list of FolderGroup objects, etc.

This is mainly a representation of folders and subfolders. But on my JSP page, I would not know how deep the iteration would be for me. How can this be handled with JSTL?

+7
source share
1 answer

Create a JSP tag file ( WEB-INF/tags/folderGroups.tag ) containing the following code:

 <%@ attribute name="list" required="true" %> <%@ taglib tagdir="/WEB-INF/tags" prefix="myTags" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <c:if test="${!empty list}"> <ul> <c:forEach var="folderGroup" items="${list}"> <li><c:out value="${folderGroup.name}"/></li> <myTags:folderGroups list="${folderGroup.subGroups}"/> </c:forEach> </ul> </c:if> 

The tag is called recursively to generate the folder tree.

And inside your JSP do

 <%@ taglib tagdir="/WEB-INF/tags" prefix="myTags" %> ... <myTags:folderGroups list="${info.folderGroups}"/> 
+14
source

All Articles