JSP生成树形菜单
一、JSP生成树形菜单
1 <!-- 生成树形菜单对象,并存入session -->
2 <c:set var="treeName" value="tree.model"/>
3 <c:if test="${empty sessionScope[treeName]}">
4 <!-- 生成树对象 -->
5 <jsp:useBean id="tree" class="com.jenkov.prizetags.tree.impl.Tree" scope="page"/>
6 <!-- 生成根结点对象 -->
7 <jsp:useBean id="root" class="com.jenkov.prizetags.tree.impl.TreeNode"/>
8 <jsp:setProperty name="root" property="id" value="rootId"/>
9 <jsp:setProperty name="root" property="name" value="后台管理操作菜单"/>
10 <jsp:setProperty name="root" property="type" value="root"/>
11 <!-- 生成文章管理功能集结点对象 -->
12 <jsp:useBean id="articleAdmins" class="com.jenkov.prizetags.tree.impl.TreeNode"/>
13 <jsp:setProperty name="articleAdmins" property="id" value="articleAdminsId"/>
14 <jsp:setProperty name="articleAdmins" property="name" value="文章发布"/>
15 <jsp:setProperty name="articleAdmins" property="type" value="functions"/>
16 <!-- 生成用户管理功能集结点对象 -->
17 <jsp:useBean id="userAdmins" class="com.jenkov.prizetags.tree.impl.TreeNode"/>
18 <jsp:setProperty name="userAdmins" property="id" value="userAdminsId"/>
19 <jsp:setProperty name="userAdmins" property="name" value="用户管理"/>
20 <jsp:setProperty name="userAdmins" property="type" value="functions"/>
21 <!-- 生成增加文章功能结点对象 -->
22 <jsp:useBean id="addArticle" class="com.jenkov.prizetags.tree.impl.TreeNode"/>
23 <jsp:setProperty name="addArticle" property="id" value="addArticleId"/>
24 <jsp:setProperty name="addArticle" property="name" value="增加文章"/>
25 <jsp:setProperty name="addArticle" property="type" value="function"/>
26 <!-- 生成修改与删除文章功能结点对象 -->
27 <jsp:useBean id="updateArticle" class="com.jenkov.prizetags.tree.impl.TreeNode"/>
28 <jsp:setProperty name="updateArticle" property="id" value="updateArticleId"/>
29 <jsp:setProperty name="updateArticle" property="name" value="修改与删除文章"/>
30 <jsp:setProperty name="updateArticle" property="type" value="function"/>
31 <!-- 生成栏目管理功能结点对象 -->
32 <jsp:useBean id="channelAdmin" class="com.jenkov.prizetags.tree.impl.TreeNode"/>
33 <jsp:setProperty name="channelAdmin" property="id" value="channelAdminId"/>
34 <jsp:setProperty name="channelAdmin" property="name" value="栏目管理"/>
35 <jsp:setProperty name="channelAdmin" property="type" value="function"/>
36 <!-- 生成用户管理功能结点对象 -->
37 <c:if test="${sessionScope.adminUserRole==2}">
38 <jsp:useBean id="userAdmin" class="com.jenkov.prizetags.tree.impl.TreeNode"/>
39 <jsp:setProperty name="userAdmin" property="id" value="userAdminId"/>
40 <jsp:setProperty name="userAdmin" property="name" value="用户管理"/>
41 <jsp:setProperty name="userAdmin" property="type" value="function"/>
42 <%userAdmin.setParent(userAdmins);%>
43 </c:if>
44 <!-- 修改当前用户密码功能结点对象 -->
45 <jsp:useBean id="userPowerAdmin" class="com.jenkov.prizetags.tree.impl.TreeNode"/>
46 <jsp:setProperty name="userPowerAdmin" property="id" value="userPowerAdminId"/>
47 <jsp:setProperty name="userPowerAdmin" property="name" value="修改当前用户密码"/>
48 <jsp:setProperty name="userPowerAdmin" property="type" value="function"/>
49 <!-- 生成退出系统功能结点对象 -->
50 <jsp:useBean id="exitSys" class="com.jenkov.prizetags.tree.impl.TreeNode"/>
51 <jsp:setProperty name="exitSys" property="id" value="exitSysId"/>
52 <jsp:setProperty name="exitSys" property="name" value="退出系统"/>
53 <jsp:setProperty name="exitSys" property="type" value="function"/>
54 <!-- 设置结点之前的父子关系 -->
55 <%addArticle.setParent(articleAdmins);
56 updateArticle.setParent(articleAdmins);
57 userPowerAdmin.setParent(userAdmins);
58 articleAdmins.setParent(root);
59 channelAdmin.setParent(root);
60 userAdmins.setParent(root);
61 exitSys.setParent(root);
62 tree.setRoot(root); %>
63 <!-- 将树对象存入session -->
64 <c:set var="tree.model" value="${tree}" scope="session"/>
65 </c:if>
二、在HTML页面中显示
1 <!------显示树形菜单------>
2 <table cellspacing="0" cellpadding="0" border="0">
3 <tree:tree tree="tree.model" node="tree.node" includeRootNode="true">
4 <tr><td>
5 <table cellspacing="0" cellpadding="0" border="0">
6 <tr><td>
7 <!-- 显示结点前面的竖线或空格图片 -->
8 <tree:nodeIndent
9 node="tree.node" indentationType="type"><tree:nodeIndentVerticalLine
10 indentationType="type"><img
11 src="gif/verticalLine.gif"></tree:nodeIndentVerticalLine><tree:nodeIndentBlankSpace
12 indentationType="type"><img
13 src="gif/blankSpace.gif"></tree:nodeIndentBlankSpace></tree:nodeIndent>
14 </td><td>
15 <!-- 显示结点前面的+-图片,文夹件打开、关闭图片,文件图片 -->
16 <tree:nodeMatch node="tree.node" hasChildren="true" expanded="false" isLastChild="false">
17 <a href="treeJava.jsp?expand=<tree:nodeId node="tree.node"/>"><img
18 src="gif/collapsedMidNode.gif" border="0"><img
19 src="gif/closedFolder.gif" border="0"></a>
20 </tree:nodeMatch>
21 <tree:nodeMatch node="tree.node" hasChildren="true" expanded="true" isLastChild="false">
22 <a href="treeJava.jsp?collapse=<tree:nodeId node="tree.node"/>"><img
23 src="gif/expandedMidNode.gif" border="0"><img
24 src="gif/openFolder.gif" border="0"></a>
25 </tree:nodeMatch>
26 <tree:nodeMatch node="tree.node" hasChildren="true" expanded="false" isLastChild="true">
27 <a href="treeJava.jsp?expand=<tree:nodeId node="tree.node"/>"><img
28 src="gif/collapsedLastNode.gif" border="0"><img
29 src="gif/closedFolder.gif" border="0"></a>
30 </tree:nodeMatch>
31 <tree:nodeMatch node="tree.node" hasChildren="true" expanded="true" isLastChild="true">
32 <a href="treeJava.jsp?collapse=<tree:nodeId node="tree.node"/>"><img
33 src="gif/expandedLastNode.gif" border="0"><img
34 src="gif/openFolder.gif" border="0"></a>
35 </tree:nodeMatch>
36 <tree:nodeMatch node="tree.node" hasChildren="false" isLastChild="false"><img
37 src="gif/noChildrenMidNode.gif" border="0"><img
38 src="gif/nonFolder.gif" border="0">
39 </tree:nodeMatch>
40 <tree:nodeMatch node="tree.node" hasChildren="false" isLastChild="true"><img
41 src="gif/noChildrenLastNode.gif" border="0"><img
42 src="gif/nonFolder.gif" border="0">
43 </tree:nodeMatch>
44 </td><td>
45 <!-- 显示结点名称,并作超链接设置 -->
46 <tree:nodeMatch node="tree.node" hasChildren="false">
47 <c:set var="currentNodeName" value="tree.node"/>
48 <c:set var="currentNode" value="${requestScope[currentNodeName]}"/>
49 <c:set var="currentNodeId" value="${currentNode.node.id}"/>
50 <!-- 增加文章节点超链接设置 -->
51 <c:set var="addArticleIdStr" value="addArticleId"/>
52 <c:if test="${currentNodeId == addArticleIdStr}">
53 <a href="article/addArticle1.jsp" target="_right">
54 <font size="2"><tree:nodeName node="tree.node"/></font>
55 </a>
56 </c:if>
57 <!-- 修改和删除文章节点超链接设置 -->
58 <c:set var="updateArticleIdStr" value="updateArticleId"/>
59 <c:if test="${currentNodeId == updateArticleIdStr}">
60 <a href="article/updateArticle1.jsp" target="_right">
61 <font size="2"><tree:nodeName node="tree.node"/></font>
62 </a>
63 </c:if>
64 <!-- 栏目管理节点超链接设置 -->
65 <c:set var="channelAdminIdStr" value="channelAdminId"/>
66 <c:if test="${currentNodeId == channelAdminIdStr}">
67 <a href="channel/channelAdmin1.jsp" target="_right">
68 <font size="2"><tree:nodeName node="tree.node"/></font>
69 </a>
70 </c:if>
71 <!-- 用户管理节点超链接设置 -->
72 <c:set var="userAdminIdStr" value="userAdminId"/>
73 <c:if test="${currentNodeId == userAdminIdStr}">
74 <a href="user/userAdmin1.jsp" target="_right">
75 <font size="2"><tree:nodeName node="tree.node"/></font>
76 </a>
77 </c:if>
78 <!-- 修改当前用户密码节点超链接设置 -->
79 <c:set var="userPowerAdminIdStr" value="userPowerAdminId"/>
80 <c:if test="${currentNodeId == userPowerAdminIdStr}">
81 <a href="user/userAdmin2.jsp" target="_right">
82 <font size="2"><tree:nodeName node="tree.node"/></font>
83 </a>
84 </c:if>
85 <!-- 生成退出系统功能结点对象 -->
86 <c:set var="exitSysIdStr" value="exitSysId"/>
87 <c:if test="${currentNodeId == exitSysIdStr}">
88 <a href="login.jsp" target="_parent">
89 <font size="2"><tree:nodeName node="tree.node"/></font>
90 </a>
91 </c:if>
92 </tree:nodeMatch>
93 <tree:nodeNoMatch node="tree.node" hasChildren="false">
94 <font size="2"><tree:nodeName node="tree.node"/></font>
95 </tree:nodeNoMatch>
96 </td></tr>
97 </table>
98 </td></tr>
99 </tree:tree>
100 </table>