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

查看:40
本文介绍了如何在 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 页面中,我不知道要迭代到什么深度.如何使用 JSTL 处理这种情况?

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 中,做

And inside your JSP, do

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

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

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