我需要在JSP页面上显示树。我怎样才能做到这一点?我有以下对象:
public class Node {
private Long id;
private Long parentId;
private String name;
private List<Node> children;
// Getters & setters
}
答案 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)
您可以尝试http://www.soft82.com/download/windows/tree4jsp/
也可以从http://www.einnovates.com/jsptools/tree4jsp/tree4jsp_v1.2.zip
下载答案 2 :(得分:0)
Jsp tree Project可以帮到你。
答案 3 :(得分:0)
我建议您使用其中一个可用的标记库。 例如:
答案 4 :(得分:0)
只需检查此JSP树。它很简单,并且具有最小的Java脚本。我使用了速度模板和JSP Tag类。
答案 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>