簡體   English   中英

在 JSP 頁面上顯示樹

[英]Displaying tree on JSP page

我需要在 JSP 頁面上顯示樹。 我該怎么做? 我有以下對象:

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

    // Getters & setters

}

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

只需檢查此 JSP 樹即可。 它很簡單,並且具有最少的 Java 腳本。 我使用了速度模板和 JSP 標簽類。

簡單的 JSP 樹

Jsp 樹項目可以幫助您。

我建議您使用可用的標記庫之一。 例如:

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

以下討論也有幫助。 http://www.jguru.com/faq/view.jsp?EID=46659

從其他答案匯編。 測試。


JSP 標簽上的遞歸

單元.java

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

    // getters && setters
}

雇員.java

public class Employees {
    private HashSet<Unit> units;

    // getters && setters
}

應用程序.java

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

員工列表.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>

單元.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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM