在JSP页面上显示树

发布于 2024-10-11 12:47:02 字数 230 浏览 2 评论 0原文

我需要在JSP页面上显示树。我怎样才能做到这一点?我有以下对象:

public class Node {
    private Long id;
    private Long parentId;
    private String name;
    private List<Node> children;

    // Getters & setters

}

I need to display tree on JSP page. How can I do that? I have following object:

public class Node {
    private Long id;
    private Long parentId;
    private String name;
    private List<Node> children;

    // Getters & setters

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

太阳男子 2024-10-18 12:47:02

使用 jsp 递归推出您自己的

Controller.java

Node root = getTreeRootNode();
request.setAttribute("node", root);

main.jsp 页面中

<jsp:include page="node.jsp"/>

node.jsp

<c:forEach var="node" items="${node.children}">
    <!-- TODO: print the node here -->
    <c:set var="node" value="${node}" scope="request"/>
    <jsp:include page="node.jsp"/>
</c:forEach>

基于 http://web.archive.org/web/ 20130509135219/http://blog.boyandi.net/2007/11/21/jsp-recursion/

Roll your own with jsp recursion

In Controller.java

Node root = getTreeRootNode();
request.setAttribute("node", root);

In main.jsp page

<jsp:include page="node.jsp"/>

In node.jsp

<c:forEach var="node" items="${node.children}">
    <!-- TODO: print the node here -->
    <c:set var="node" value="${node}" scope="request"/>
    <jsp:include page="node.jsp"/>
</c:forEach>

Based on http://web.archive.org/web/20130509135219/http://blog.boyandi.net/2007/11/21/jsp-recursion/

若能看破又如何 2024-10-18 12:47:02

Jsp 树项目 可以帮助您。

Jsp tree Project can help you.

半透明的墙 2024-10-18 12:47:02

我建议您使用可用的标签库之一。
例如:

http://beehive.apache.org/docs/1.0/netui/ TagsTree.html

以下讨论也有帮助。
http://www.jguru.com/faq/view.jsp?EID=46659

I'd recommend you to use one of the available tag libraries.
For example:

http://beehive.apache.org/docs/1.0/netui/tagsTree.html

The following discussion can help too.
http://www.jguru.com/faq/view.jsp?EID=46659

被翻牌 2024-10-18 12:47:02

只需检查这个 JSP 树即可。它很简单并且具有最少的 Java 脚本。我使用了速度模板和 JSP 标签类。

简单的 JSP 树

Just check this JSP tree. It is simple and has minimum Java Scripts. I used velocity templates and JSP Tag class.

simple JSP tree

残龙傲雪 2024-10-18 12:47:02

根据其他答案编译。


的递归

Unit.javaEmployees.javaApplication.java

public class Unit {
    private String name;
    private HashSet<Unit> units;

    // getters && setters
}

public class Employees {
    private HashSet<Unit> units;

    // getters && setters
}

EmployeeList.jspUnit.jsp

...
request.setAttribute("employees", employees);
request.getRequestDispatcher("EmployeeList.jsp").forward(request, response);
...

已测试。

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
    <head>
        ...
    </head>
    <body>
        ...
        <ul>
            <c:forEach var="unit" items="${employees.getUnits()}">
                <li>
                    <c:set var="unit" value="${unit}" scope="request"/>
                    <jsp:include page="Unit.jsp"/>
                </li>
            </c:forEach>
        </ul>
    </body>
<html>

JSP 标记上

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<span>${unit.getName()}</span>
...
<ul>
    <c:forEach var="innerUnit" items="${unit.getUnits()}">
        <li>
            <c:set var="unit" value="${innerUnit}" scope="request"/>
            <jsp:include page="Unit.jsp"/>
        </li>
    </c:forEach>
</ul>

Compilation from the other answers. Tested.


Recursion on JSP tags

Unit.java

public class Unit {
    private String name;
    private HashSet<Unit> units;

    // getters && setters
}

Employees.java

public class Employees {
    private HashSet<Unit> units;

    // getters && setters
}

Application.java

...
request.setAttribute("employees", employees);
request.getRequestDispatcher("EmployeeList.jsp").forward(request, response);
...

EmployeeList.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
    <head>
        ...
    </head>
    <body>
        ...
        <ul>
            <c:forEach var="unit" items="${employees.getUnits()}">
                <li>
                    <c:set var="unit" value="${unit}" scope="request"/>
                    <jsp:include page="Unit.jsp"/>
                </li>
            </c:forEach>
        </ul>
    </body>
<html>

Unit.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<span>${unit.getName()}</span>
...
<ul>
    <c:forEach var="innerUnit" items="${unit.getUnits()}">
        <li>
            <c:set var="unit" value="${innerUnit}" scope="request"/>
            <jsp:include page="Unit.jsp"/>
        </li>
    </c:forEach>
</ul>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文