【发布时间】: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
}
【问题讨论】:
我需要在 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 tree Project可以帮到你。
【讨论】:
我建议您使用可用的标签库之一。 例如:
http://beehive.apache.org/docs/1.0/netui/tagsTree.html
以下讨论也可以提供帮助。 http://www.jguru.com/faq/view.jsp?EID=46659
【讨论】:
只需检查这个 JSP 树。它很简单,并且具有最少的 Java 脚本。我使用了velocity模板和JSP Tag类。
【讨论】:
其他答案的编译。 测试过。
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>
【讨论】: