后端
先找我们需要的,然后根据表来建立实体类
思路:树形菜单中分为了父级菜单和子级菜单,在数据库表中pid为-1的为一级菜单,pid为表中某个菜单的id说明他们是父子关系。为了使我们的树形菜单变为动态而不是在jsp页面中定死,在后端我们需要将父级菜单和子级菜单分类,在遍历父级菜单的同时 遍历子级菜单,根据id和pid找到他们相对应的关系。在前端我们使用ajax进行拼接,拿到后端传过来的方法进行遍历,用HasChildren判断是否存在子节点,存在即继续拼接。
数据库中t_oa_Permission表对应的实体类
下面展示Permission实体类
。
package com.yzp.entity;
/**
* 数据库中t_oa_Permission表对应的实体类
* @author 易泽鹏
*
*
*/
public class Permission {
private long id;//id
private String name;//菜单名
private String description;//菜单描述
private String url;//点击菜单跳转页面
private long pid;//父级菜单的id
private int ismenu;//显示顺序
private long displayno;//显示的顺序
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public long getPid() {
return pid;
}
public void setPid(long pid) {
this.pid = pid;
}
public int getIsmenu() {
return ismenu;
}
public void setIsmenu(int ismenu) {
this.ismenu = ismenu;
}
public long getDisplayno() {
return displayno;
}
public void setDisplayno(long displayno) {
this.displayno = displayno;
}
public Permission() {
// TODO Auto-generated constructor stub
}
public Permission(long id, String name, String description, String url, long pid, int ismenu, long displayno) {
super();
this.id = id;
this.name = name;
this.description = description;
this.url = url;
this.pid = pid;
this.ismenu = ismenu;
this.displayno = displayno;
}
@Override
public String toString() {
return "Permission [id=" + id + ", name=" + name + ", description=" + description + ", url=" + url + ", pid="
+ pid + ", ismenu=" + ismenu + ", displayno=" + displayno + "]";
}
}
下面展示 Permissiondao
。
package com.yzp.dao;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.yzp.entity.Permission;
import com.zking.util.BaseDao;
import com.zking.util.BuildTree;
import com.zking.util.PageBean;
import com.zking.util.TreeVo;
public class permissionDao extends BaseDao<Permission>{
//查询t_oa_permission表中的数据
public List<Permission> list(Permission permission, PageBean pageBean) throws Exception {
String sql = "select * from t_easyui_permission";
return super.executeQuery(sql, Permission.class, pageBean);
}
public List<TreeVo<Permission>> menus(Permission permission, PageBean pageBean) throws Exception {
List<TreeVo<Permission>> trees = new ArrayList<TreeVo<Permission>>();
//从数据库拿到的菜单数据,此时数据是平级的,不具备父子关系
List<Permission> list = this.list(permission, pageBean);
for (Permission p : list) {
TreeVo<Permission> vo = new TreeVo<>();
vo.setId(p.getId()+"");
vo.setText(p.getName());
vo.setParentId(p.getPid()+"");//节点的父id
trees.add(vo);
}
return BuildTree.buildList(trees, "0");
}
}
下面展示 web代码
。
package com.yzp.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.yzp.dao.permissionDao;
import com.yzp.entity.Permission;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriver;
import com.zking.util.ResponseUtil;
import com.zking.util.TreeVo;
public class PermissionAction extends ActionSupport implements ModelDriver<Permission>{
private Permission permission = new Permission();
permissionDao permissiondao = new permissionDao();
@Override
public Permission getModel() {
return permission;
}
public String menus(HttpServletRequest req, HttpServletResponse resp) {
try {
List<TreeVo<Permission>> menus = permissiondao.menus(null, null);
//向前端响应数据
ResponseUtil.writeJson(resp, menus);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
下面展示 xml配置
。
前端
下面展示 jsp代码
。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="common/header.jsp" %>
<!DOCTYPE html >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div class="layui-layout layui-layout-admin">
<div class="layui-header">
<div class="layui-logo layui-hide-xs layui-bg-black">layout demo</div>
<!-- 头部区域(可配合layui 已有的水平导航) -->
<ul class="layui-nav layui-layout-left">
<!-- 移动端显示 -->
<li class="layui-nav-item layui-show-xs-inline-block layui-hide-sm" lay-header-event="menuLeft">
<i class="layui-icon layui-icon-spread-left"></i>
</li>
<!-- Top导航栏 -->
<li class="layui-nav-item layui-hide-xs"><a href="">nav 1</a></li>
<li class="layui-nav-item layui-hide-xs"><a href="">nav 2</a></li>
<li class="layui-nav-item layui-hide-xs"><a href="">nav 3</a></li>
<li class="layui-nav-item">
<a href="javascript:;">nav groups</a>
<dl class="layui-nav-child">
<dd><a href="">menu 11</a></dd>
<dd><a href="">menu 22</a></dd>
<dd><a href="">menu 33</a></dd>
</dl>
</li>
</ul>
<!-- 个人头像及账号操作 -->
<ul class="layui-nav layui-layout-right">
<li class="layui-nav-item layui-hide layui-show-md-inline-block">
<a href="javascript:;">
<img src="//tva1.sinaimg.cn/crop.0.0.118.118.180/5db11ff4gw1e77d3nqrv8j203b03cweg.jpg" class="layui-nav-img">
tester
</a>
<dl class="layui-nav-child">
<dd><a href="">Your Profile</a></dd>
<dd><a href="">Settings</a></dd>
<dd><a href="login.jsp">Sign out</a></dd>
</dl>
</li>
<li class="layui-nav-item" lay-header-event="menuRight" lay-unselect>
<a href="javascript:;">
<i class="layui-icon layui-icon-more-vertical"></i>
</a>
</li>
</ul>
</div>
<div class="layui-side layui-bg-black">
<div class="layui-side-scroll">
<!-- 左侧导航区域(可配合layui已有的垂直导航) -->
<ul id="menu" class="layui-nav layui-nav-tree" lay-filter="menu">
<li class="layui-nav-item layui-nav-itemed">
<a class="" href="javascript:;">menu group 1</a>
<dl class="layui-nav-child">
<dd><a href="javascript:;">menu 1</a></dd>
<dd><a href="javascript:;">menu 2</a></dd>
<dd><a href="javascript:;">menu 3</a></dd>
<dd><a href="">the links</a></dd>
</dl>
</li>
<li class="layui-nav-item">
<a href="javascript:;">menu group 2</a>
<dl class="layui-nav-child">
<dd><a href="javascript:;">list 1</a></dd>
<dd><a href="javascript:;">list 2</a></dd>
<dd><a href="">超链接</a></dd>
</dl>
</li>
<li class="layui-nav-item"><a href="javascript:;">click menu item</a></li>
<li class="layui-nav-item"><a href="">the links</a></li>
</ul>
</div>
</div>
<div class="layui-body">
<!-- 内容主体区域 -->
<div style="padding: 15px;">内容主体区域。记得修改 layui.css 和 js 的路径</div>
</div>
<div class="layui-footer">
<!-- 底部固定区域 -->
底部固定区域
</div>
</div>
<script type="text/javascript">
layui.use(['jquery'],function(){
let $ = layui.jquery;
$.ajax({
url:"${pageContext.request.contextPath}/permission.action?methodName=menus",
dataType:'json',
success:function(data){
let html = '';
$.each(data,function(i,n){
html +='<li class="layui-nav-item layui-nav-itemed">';
html +=' <a class="" href="javascript:;">'+data[i].text+'</a>';
//判断当前一级节点是否存在节点
if(data[i].hasChildren){
html +=' <dl class="layui-nav-child">';
let childern = data[i].children;
$.each(childern,function(index,node){
html+=' <dd><a href="javascript:;">'+childern[index].text+'</a></dd>';
})
html +=' </dl>';
}
html +='</li>';
});
$("#menu").html(html);
}
})
})
</script>
</body>
</html>
运行结果