如何在JSP中呈现递归集合 [英] How to present a recursive collection in JSP

查看:520
本文介绍了如何在JSP中呈现递归集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个后端服务,它返回一个 Info 对象。这个 Info 对象有一个 FolderGroup 对象列表, FolderGroup 对象等。

I have a backend service which is returning me an Info object. This Info object has a list of FolderGroup objects which in turn has list of FolderGroup objects and so on.

基本上它是表示文件夹和子文件夹。但是在我的JSP页面中,我不知道到底有多深度才能迭代。

Basically it is to represent folders and subfolders. But in my JSP page, I would not know till what depth it is present for me to iterate. How can this be handled with JSTL?

推荐答案

创建一个JSP标签文件( WEB-INF / tags /folderGroups.tag )包含以下代码:

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 calls itself recursively to generate a folder tree.

在您的JSP中,执行

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

这篇关于如何在JSP中呈现递归集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆