【问题标题】:Displaying tree on JSP page在 JSP 页面上显示树
【发布时间】:2011-01-09 10:17:23
【问题描述】:

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

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

    // Getters & setters

}

【问题讨论】:

    标签: java jsp recursion tree


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:
      【解决方案3】:

      Jsp tree Project可以帮到你。

      【讨论】:

        【解决方案4】:

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

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

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

        【讨论】:

          【解决方案5】:

          只需检查这个 JSP 树。它很简单,并且具有最少的 Java 脚本。我使用了velocity模板和JSP Tag类。

          simple JSP tree

          【讨论】:

            【解决方案6】:

            其他答案的编译。 测试过。


            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>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2012-09-29
              • 2015-04-02
              • 1970-01-01
              • 1970-01-01
              • 2019-09-29
              • 2012-08-26
              • 1970-01-01
              相关资源
              最近更新 更多