在JSP页面上显示树形结构

12

我需要在JSP页面上展示一棵树,我有以下对象:

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

    // Getters & setters

}
6个回答

17

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



0

从其他答案编译而来。已测试。


在JSP标记上的递归

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>

0

0

0

请查看这个JSP树。它很简单,只有最少的Java脚本。我使用了速度模板和JSP标签类。

简单的JSP树


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接