1.先把工具类考进去
2.创建实体类
package com.likang.entity;
/**
* 作用是通过TreeNode类转换成tree_data1.json的字符串
* @author Hasee
*
*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TreeNode {
private String id;
private String text;
private List<TreeNode> children =new ArrayList<>();
private Map<String, Object> attributes=new HashMap<>();
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 List<TreeNode> getChildren() {
return children;
}
public void setChildren(List<TreeNode> children) {
this.children = children;
}
public Map<String, Object> getAttributes() {
return attributes;
}
public void setAttributes(Map<String, Object> attributes) {
this.attributes = attributes;
}
@Override
public String toString() {
return "TreeNode [id=" + id + ", text=" + text + ", children=" + children + ", attributes=" + attributes + "]";
}
}
3.dao包
package com.likang.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.likang.entity.TreeNode;
import com.likang.util.JsonBaseDao;
import com.likang.util.JsonUtils;
import com.likang.util.PageBean;
import com.likang.util.StringUtils;
public class MenuDao extends JsonBaseDao{
private List<TreeNode> listTreeNode;
/**
* 给前台返回tree_data1.json的字符串
* @param paMap 从前台jsp传递过来的参数集合
* @param pageBean
* @return
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public List<TreeNode> listTreeNode(Map<String,String[]> paMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
List<Map<String, Object>> listMap = this.listMap(paMap, pageBean);
List<TreeNode> listTreeNode=new ArrayList<>();
this.listMapTOListTreeNode(listMap, listTreeNode);
return listTreeNode;
}
public List<Map<String, Object>> listMap(Map<String,String[]> paMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql="select * from t_easyui_menu where true ";
String menuId= JsonUtils.getParamVal(paMap, "Menuid");
if(StringUtils.isNotBlank(menuId)) {
sql +=" and parentid="+menuId;
}else {
sql+=" and parentid=-1";
}
//这里面放的是数据库中的菜单信息
List<Map<String, Object>> listMap=super.executeQuery(sql, pageBean);
return listMap;
}
/**
* [{'Menuid' : 001,'Menuname':'学生管理'},{{‘Menuid’:001,'Menuname':'后勤管理'}}]
* -->
* {id:...,text:...}
* @param map
* @param treenode
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
private void mapTOTreeNode(Map<String, Object> map,TreeNode treenode) throws InstantiationException, IllegalAccessException, SQLException {
treenode.setId(map.get("Menuid")+"");
treenode.setText(map.get("Menuname")+"");
treenode.setAttributes(map);
// 将子节点添加到父节点当中,建立数据之间的父子关系
// treenode.setChildren(children);
Map<String, String[]> childrenMap=new HashMap<>();
childrenMap.put("Menuid", new String[] {treenode.getId()});
List<Map<String, Object>> listMap = this.listMap(childrenMap, null);
List<TreeNode> listTreeNode=new ArrayList<>();
this.listMapTOListTreeNode(listMap, listTreeNode);
treenode.setChildren(listTreeNode);
}
/**
* [{'Menuid' : 001,'Menuname':'学生管理'},{{‘Menuid’:001,'Menuname':'后勤管理'}}]
* -->
* tree_data1.json
* @param listMap
* @param listTreeNode
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
private void listMapTOListTreeNode(List<Map<String, Object>> listMap,List<TreeNode> listTreeNode) throws InstantiationException, IllegalAccessException, SQLException {
TreeNode treeNode=null;
for (Map<String, Object> map : listMap) {
treeNode=new TreeNode();
mapTOTreeNode(map, treeNode);
listTreeNode.add(treeNode);
}
}
}
web层
package com.likang.web;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.likang.dao.MenuDao;
import com.likang.entity.TreeNode;
import com.likang.util.ResponseUtil;
import com.zking.framework.ActionSupport;
public class MenuAction extends ActionSupport {
private MenuDao dao=new MenuDao();
public String menuTree(HttpServletRequest req, HttpServletResponse resp) throws JsonProcessingException, Exception {
ObjectMapper om= new ObjectMapper();
//获取到easyui框架所识别的json格式
List<TreeNode> listTreeNode = this.dao.listTreeNode(req.getParameterMap(), null);
ResponseUtil.write(resp, om.writeValueAsString(listTreeNode));
return null;
}
}
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<config>
<!-- <action path="/regAction" type="test.RegAction">
<forward name="failed" path="/reg.jsp" redirect="false" />
<forward name="success" path="/login.jsp" redirect="true" />
</action> -->
<action path="/menuAction" type="com.likang.web.MenuAction">
</action>
<!-- <action path="/userAction" type="com.zking.web.UserAction">
<forward name="index" path="/index.jsp" redirect="false" />
</action> -->
</config>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>T216_easyui</display-name>
<filter>
<filter-name>encodingFiter</filter-name>
<filter-class>com.likang.util.EncodingFiter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingFiter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>actionServlet</servlet-name>
<servlet-class>com.zking.framework.ActionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>actionServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
jsp代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>后台主界面</title>
<!-- ctrl+shift+r 寻找 -->
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/static/js/public/easyui5/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/static/js/public/easyui5/themes/icon.css">
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/public/easyui5/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/public/easyui5/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/index.js"></script>
</head>
<body class="easyui-layout">
<div data-options="region:'north',border:false" style="height:60px;background:#B3DFDA;padding:10px">north region</div>
<div data-options="region:'west',split:true,title:'West'" style="width:150px;padding:10px;">
<ul id="tt"></ul>
</div>
<div data-options="region:'east',split:true,collapsed:true,title:'East'" style="width:100px;padding:10px;">east region</div>
<div data-options="region:'south',border:false" style="height:50px;background:#A9FACD;padding:10px;">south region</div>
<div data-options="region:'center',title:'Center'">
<div id="menuTab" class="easyui-tabs" style="">
<div title="首页" style="padding:20px;display:none;">
欢迎界面
</div>
</div>
</div>
</body>
</html>
js代码
$(function(){
$('#tt').tree({
url:'menuAction.action?methodName=menuTree',
onClick: function(node){
// alert(node.text);
// add a new tab panel
var content = '<iframe scrolling="no" frameborder="0" src="'+node.attributes.menuURL+'" width="99%" height="99%"></iframe>';
if($('#menuTab').tabs('exists',node.text)){
//存在执行选项卡选中已有选项卡的操作
$('#menuTab').tabs('select',node.text);
}else{
//不存在执行新增的操作
$('#menuTab').tabs('add',{
title:node.text,
content:content,
closable:true,
});
}
}
});
})
结果如下: