目录
一、左侧树形菜单数据分析
二、获取数据库表中的所有菜单树
三、在后台构建父子关系
四、前端代码实现
五、总结
pid是-1的 是父id
去layUI官网找到菜单,复制源码
实体类 Permission.java
package com.maomao.entity;
//数据库中t_oa_permission
public class Permission {
private long 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) {
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.java
package com.maomao.dao;
import java.util.ArrayList;
import java.util.List;
import com.maomao.entity.Permission;
import com.maomao.util.BaseDao;
import com.maomao.util.BuildTree;
import com.maomao.util.PageBean;
import com.maomao.util.TreeVo;
public class PermissionDao extends BaseDao{
//查询t_oa_permission表中的数据
public List list(Permission permission,PageBean pageBean) throws Exception{
String sql="select * from t_oa_permission";
return super.executeQuery(sql, Permission.class, pageBean);
}
public List> menus(Permission permission,PageBean pageBean) throws Exception{
List> trees=new ArrayList>();
// 从数据库中拿到的菜单数据,此时数据是平级的,不具备父子关系
List list=this.list(permission, pageBean);
for (Permission p : list) {
TreeVo vo=new TreeVo<>();
vo.setId(p.getId()+"");
vo.setText(p.getName());//节点名称
vo.setParentId(p.getPid()+"");
trees.add(vo);
}
return BuildTree.buildList(trees, "-1");
}
}
PermissionDaoTest.java
package com.maomao.dao;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.maomao.entity.Permission;
import com.maomao.util.TreeVo;
public class PermissionDaoTest {
private PermissionDao pd=new PermissionDao();
@Test
public void testList() {
fail("Not yet implemented");
}
@Test
public void testExecuteUpdate() {
fail("Not yet implemented");
}
@Test
public void testlist() {
try {
List list = pd.list(null, null);
// for (Permission p : list) {
// System.out.println(p);
// }
ObjectMapper om = new ObjectMapper();
System.out.println(om.writeValueAsString(list));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testMenus() {
try {
List> list = pd.menus(null, null);
for (TreeVo t : list) {
System.out.println(t);
}
ObjectMapper om = new ObjectMapper();
System.out.println(om.writeValueAsString(list));
} catch (Exception e) {
e.printStackTrace();
}
}
}
获得数据是JSON格式
TreeVo.java
package com.zking.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class TreeVo {
/**
* 节点ID
*/
private String id;
/**
* 显示节点文本
*/
private String text;
/**
* 节点状态,open closed
*/
private Map state;
/**
* 节点是否被选中 true false
*/
private boolean checked = false;
/**
* 节点属性
*/
private Map attributes;
/**
* 节点的子节点
*/
private List> children = new ArrayList>();
/**
* 父ID
*/
private String parentId;
/**
* 是否有父节点
*/
private boolean hasParent = false;
/**
* 是否有子节点
*/
private boolean hasChildren = false;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Map getState() {
return state;
}
public void setState(Map state) {
this.state = state;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public Map getAttributes() {
return attributes;
}
public void setAttributes(Map attributes) {
this.attributes = attributes;
}
public List> getChildren() {
return children;
}
public void setChildren(List> children) {
this.children = children;
}
public boolean isHasParent() {
return hasParent;
}
public void setHasParent(boolean isParent) {
this.hasParent = isParent;
}
public boolean isHasChildren() {
return hasChildren;
}
public void setChildren(boolean isChildren) {
this.hasChildren = isChildren;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public TreeVo(String id, String text, Map state, boolean checked, Map attributes,
List> children, boolean isParent, boolean isChildren, String parentID) {
this.id = id;
this.text = text;
this.state = state;
this.checked = checked;
this.attributes = attributes;
this.children = children;
this.hasParent = isParent;
this.hasChildren = isChildren;
this.parentId = parentID;
}
public TreeVo() {
}
}
BuildTree.java
package com.zking.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BuildTree {
/**
* 默认-1为顶级节点
* @param nodes
* @param
* @return
*/
public static TreeVo build(List> nodes) {
if (nodes == null) {
return null;
}
List> topNodes = new ArrayList>();
for (TreeVo children : nodes) {
String pid = children.getParentId();
if (pid == null || "-1".equals(pid)) {
topNodes.add(children);
continue;
}
for (TreeVo parent : nodes) {
String id = parent.getId();
if (id != null && id.equals(pid)) {
parent.getChildren().add(children);
children.setHasParent(true);
parent.setChildren(true);
continue;
}
}
}
TreeVo root = new TreeVo();
if (topNodes.size() == 1) {
root = topNodes.get(0);
} else {
root.setId("000");
root.setParentId("-1");
root.setHasParent(false);
root.setChildren(true);
root.setChecked(true);
root.setChildren(topNodes);
root.setText("顶级节点");
Map state = new HashMap<>(16);
state.put("opened", true);
root.setState(state);
}
return root;
}
/**
* 指定idparam为顶级节点
* @param nodes
* @param idParam
* @param
* @return
*/
public static List> buildList(List> nodes, String idParam) {
if (nodes == null) {
return null;
}
List> topNodes = new ArrayList>();
for (TreeVo children : nodes) {
String pid = children.getParentId();
if (pid == null || idParam.equals(pid)) {
topNodes.add(children);
continue;
}
for (TreeVo parent : nodes) {
String id = parent.getId();
if (id != null && id.equals(pid)) {
parent.getChildren().add(children);
children.setHasParent(true);
parent.setChildren(true);
continue;
}
}
}
return topNodes;
}
}
PermissionAction.java
package com.maomao.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.maomao.dao.PermissionDao;
import com.maomao.entity.Permission;
import com.maomao.util.ResponseUtil;
import com.maomao.util.TreeVo;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriver;
public class PermissionAction extends ActionSupport implements ModelDriver{
private Permission permission=new Permission();
private PermissionDao pd=new PermissionDao();
@Override
public Permission getModel() {
return permission;
}
public String menus(HttpServletRequest req, HttpServletResponse resp) {
try {
List> menus=pd.menus(null, null);
//向前端响应树形结构的数据
ResponseUtil.writeJson(resp, menus);
} catch (Exception e) {
e.printStackTrace();
}
return super.execute(req, resp);
}
}
配置文件mvc.xml
main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="common/header.jsp" %>
Insert title here
页面展示
1.layUI官网,如何显示菜单数据------>HTML
2.从HTML代码推断,需要什么样的数据格式------>数据必须存在父子关系
3.读取数据表,拿到菜单相关的数据---->数据是平级,不存在父子关系
4.解释了工具类的底层原理
5.调用工具类,将平级的菜单数据,转换了具备父子关系的菜单数据
6.后台代码补全:action、mvc.xml配置
7.前台调用action,并且利用$.ajax获取数据,然后通过$.each构建第一步中的HTML代码