在JSP页面上显示树

时间:2011-01-09 10:17:24

标签: java jsp recursion tree

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

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

    // Getters & setters

}

6 个答案:

答案 0 :(得分:14)

使用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/

答案 1 :(得分:1)

答案 2 :(得分:0)

Jsp tree Project可以帮到你。

答案 3 :(得分:0)

我建议您使用其中一个可用的标记库。 例如:

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

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

答案 4 :(得分:0)

只需检查此JSP树。它很简单,并且具有最小的Java脚本。我使用了速度模板和JSP Tag类。

simple JSP tree

答案 5 :(得分:0)

  

JSP标记中的递归

// Compilation from the other answers. Tested myself.

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>