思维导图
?一.Json串得转换
1、转换为对象
package com.lj.demo;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonDemo1 {
public static void main(String[] args) throws Exception {
// 一:实体类转换成Json串-》json对象
JsonObject1 obj1 = new JsonObject1("l1", "学生管理", "closed");
ObjectMapper om = new ObjectMapper();
System.out.println(om.writeValueAsString(obj1));
// 二:map集合转成json串-》json对象
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", "11");
map.put("text", "xues");
map.put("state", "closed");
System.out.println(om.writeValueAsString(map));
}
}
运行结果
2、转换成数组?
package com.lj.demo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonDemo2 {
//方式一:获取json数组 实体类对象转换成json ——>?json数组
public static void main(String[] args) throws Exception {
JsonObject1 obj1=new JsonObject1("14", "about.html", "null");
JsonObject1 obj2=new JsonObject1("15", "welcome.html", "null");
List<JsonObject1> list=new ArrayList<>();
list.add(obj1);
list.add(obj2);
ObjectMapper om=new ObjectMapper();
System.out.println(om.writeValueAsString(list));
//方式二:map集合对象转换成json数组
Map<String, Object> map=new HashMap<String, Object>();
map.put("id","14");
map.put("text","about.html");
map.put("state", null);
Map<String, Object> map2=new HashMap<String, Object>();
map2.put("id","15");
map2.put("text","welcome.html");
map2.put("state",null);
List<Map<String, Object>> listMap=new ArrayList<>();
listMap.add(map);
listMap.add(map2);
System.out.println(om.writeValueAsString(listMap));
}
}
运行结果
?二、从数据库拿值然后显示到菜单并且具有层级关系
1.导jar包
2.编写对应得实体类(实体类就没放图了)
3.写dao包(目的:获取所有表数据,并进行分级)
package com.lj.dao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lj.entity.Menu;
import com.zking.util.BaseDao;
import com.zking.util.BuildTree;
import com.zking.util.PageBean;
import com.zking.util.TreeVo;
public class MenuDao extends BaseDao<Menu> {
public List<Menu> list(Menu menu, PageBean pageBean) throws Exception {
return super.executeQuery("select * from t_easyui_menu", Menu.class, pageBean);
}
public List<TreeVo<Menu>> tree(Menu menu, PageBean pageBean) throws Exception{
//拿到平级数据,没有父子层关系的数据
List<Menu> list = this.list(menu, pageBean);
//将List<Menu>转成list<TreeVo<Menu>>对象
List<TreeVo<Menu>> listVos=new ArrayList<TreeVo<Menu>>();
for (Menu m : list) {
TreeVo<Menu> vo=new TreeVo<>();
vo.setId(m.getMenuid());
vo.setText(m.getMenuname());
vo.setParentId(m.getParentid());
Map<String, Object> attributes=new HashMap<String, Object>();
//self是个键名 随便取名字
attributes.put("self", m);
vo.setAttributes(attributes);
listVos.add(vo);
}
//构建父子层关系,希望拿到菜单管理下的所有子节点数据
return BuildTree.buildList(listVos, "000");
}
public static void main(String[] args) throws Exception {
MenuDao menuDao=new MenuDao();
//List<Menu> list=menuDao.list(null, null);
List<TreeVo<Menu>> tree = menuDao.tree(null, null);
ObjectMapper om=new ObjectMapper();
//System.out.println(om.writeValueAsString(list));
System.out.println(om.writeValueAsString(tree));
}
}
?4.改ResponseUtil
package com.zking.util;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
public class ResponseUtil {
public static void write(HttpServletResponse response,Object o)throws Exception{
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
out.println(o.toString());
out.flush();
out.close();
}
public static void writeJson(HttpServletResponse response,Object o)throws Exception{
ObjectMapper om=new ObjectMapper();
// om.writeValueAsString(o)代表json串
write(response, om.writeValueAsString(o));
}
}
5.写MenuAction
package com.lj.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriver;
import com.zking.util.ResponseUtil;
import com.zking.util.TreeVo;
import com.lj.dao.MenuDao;
import com.lj.entity.Menu;
public class MenuAction extends ActionSupport implements ModelDriver<Menu> {
Menu menu=new Menu();
MenuDao menuDao=new MenuDao();
@Override
public Menu getModel() {
// TODO Auto-generated method stub
return menu;
}
public String tree(HttpServletRequest req, HttpServletResponse resp) throws Exception {
List<TreeVo<Menu>> tree = menuDao.tree(null, null);
ResponseUtil.writeJson(resp, tree);
return null;
}
}
6.运行MenuDao?(运行结果如下)
[{"id":"004","text":"统一配置","state":null,"checked":false,"attributes":{"self":{"serialNo":"1C63DA57E65243F184D6789A88E137B9","menuid":"004","menuname":"统一配置","menuURL":null,"parentid":"000"}},"children":[{"id":"004004","text":"人员管理","state":null,"checked":false,"attributes":{"self":{"serialNo":"269D9A1546E44A438AEA0513DFFA8D86","menuid":"004004","menuname":"人员管理","menuURL":null,"parentid":"004"}},"children":[{"id":"004004001","text":"角色维护","state":null,"checked":false,"attributes":{"self":{"serialNo":"365C214ED29841E0900BA7D0EECEE4FE","menuid":"004004001","menuname":"角色维护","menuURL":"jsp/config/userManage/roleInfo","parentid":"004004"}},"children":[],"parentId":"004004","hasParent":true,"hasChildren":false},{"id":"004004004","text":"权限信息维护","state":null,"checked":false,"attributes":{"self":{"serialNo":"79D5736B5D2B4427AE5D1DC2D9F3F414","menuid":"004004004","menuname":"权限信息维护","menuURL":"jsp/config/userManage/authInfo","parentid":"004004"}},"children":[],"parentId":"004004","hasParent":true,"hasChildren":false},{"id":"004004003","text":"人员信息维护","state":null,"checked":false,"attributes":{"self":{"serialNo":"90A28BFD207744428C623182701113BC","menuid":"004004003","menuname":"人员信息维护","menuURL":"jsp/userManage.jsp","parentid":"004004"}},"children":[],"parentId":"004004","hasParent":true,"hasChildren":false},{"id":"004004002","text":"工作组维护","state":null,"checked":false,"attributes":{"self":{"serialNo":"E51F9D7EDF7648E9A641100A10DD5D67","menuid":"004004002","menuname":"工作组维护","menuURL":"jsp/config/userManage/groupInfo","parentid":"004004"}},"children":[],"parentId":"004004","hasParent":true,"hasChildren":false}],"parentId":"004","hasParent":true,"hasChildren":true},{"id":"004001","text":"数据字典","state":null,"checked":false,"attributes":{"self":{"serialNo":"49EB2A822E03457498A0A19F9956AE2B","menuid":"004001","menuname":"数据字典","menuURL":"jsp/config/dataDict","parentid":"004"}},"children":[],"parentId":"004","hasParent":true,"hasChildren":false},{"id":"004003","text":"权限管理","state":null,"checked":false,"attributes":{"self":{"serialNo":"7B63D97C1B2C4D8896AB3648B065CCD1","menuid":"004003","menuname":"权限管理","menuURL":null,"parentid":"004"}},"children":[{"id":"004003003","text":"用户权限管理","state":null,"checked":false,"attributes":{"self":{"serialNo":"059CB9B033C247A1AD4B3F0E78C0ED55","menuid":"004003003","menuname":"用户权限管理","menuURL":"jsp/userManage.jsp","parentid":"004003"}},"children":[],"parentId":"004003","hasParent":true,"hasChildren":false},{"id":"004003002","text":"组权限管理","state":null,"checked":false,"attributes":{"self":{"serialNo":"57BC4AB9F93741ACBBD2BDEC71538653","menuid":"004003002","menuname":"组权限管理","menuURL":"jsp/config/groupAuthorityManage","parentid":"004003"}},"children":[],"parentId":"004003","hasParent":true,"hasChildren":false},{"id":"004003001","text":"角色权限管理","state":null,"checked":false,"attributes":{"self":{"serialNo":"DA05BB54E33845C4922A586B88C48205","menuid":"004003001","menuname":"角色权限管理","menuURL":"jsp/config/roleAuthorityManage","parentid":"004003"}},"children":[],"parentId":"004003","hasParent":true,"hasChildren":false}],"parentId":"004","hasParent":true,"hasChildren":true},{"id":"004002","text":"系统参数配置","state":null,"checked":false,"attributes":{"self":{"serialNo":"D0D2C27E371B491DB1099E50B2D0A8DA","menuid":"004002","menuname":"系统参数配置","menuURL":"jsp/config/systemParamCfg","parentid":"004"}},"children":[],"parentId":"004","hasParent":true,"hasChildren":false}],"parentId":"000","hasParent":false,"hasChildren":true},{"id":"002","text":"后勤管理","state":null,"checked":false,"attributes":{"self":{"serialNo":"20137C05907C42C7B488F9208E6FDDFE","menuid":"002","menuname":"后勤管理","menuURL":null,"parentid":"000"}},"children":[{"id":"002004","text":"宿舍日常报销","state":null,"checked":false,"attributes":{"self":{"serialNo":"1F560F60BAFA488B9D95E793226A1CE7","menuid":"002004","menuname":"宿舍日常报销","menuURL":null,"parentid":"002"}},"children":[{"id":"002004002","text":"大件购置","state":null,"checked":false,"attributes":{"self":{"serialNo":"391B22000A874FF7B27298E3B4AC1487","menuid":"002004002","menuname":"大件购置","menuURL":"jsp/support/largeThingInfo","parentid":"002004"}},"children":[],"parentId":"002004","hasParent":true,"hasChildren":false},{"id":"002004001","text":"维修","state":null,"checked":false,"attributes":{"self":{"serialNo":"BC03322D761841F08859350D1C4B0ECA","menuid":"002004001","menuname":"维修","menuURL":"jsp/support/repairInfo","parentid":"002004"}},"children":[],"parentId":"002004","hasParent":true,"hasChildren":false}],"parentId":"002","hasParent":true,"hasChildren":true},{"id":"002001","text":"宿舍管理","state":null,"checked":false,"attributes":{"self":{"serialNo":"85EBB07BE0B349D3B3AD1B50ED546E00","menuid":"002001","menuname":"宿舍管理","menuURL":"jsp/support/roominfo","parentid":"002"}},"children":[],"parentId":"002","hasParent":true,"hasChildren":false},{"id":"002005","text":"后勤统计","state":null,"checked":false,"attributes":{"self":{"serialNo":"9C660197D7B44BE584101F286AC4DC8A","menuid":"002005","menuname":"后勤统计","menuURL":"jsp/support/totalInfo","parentid":"002"}},"children":[],"parentId":"002","hasParent":true,"hasChildren":false},{"id":"002002","text":"水电费","state":null,"checked":false,"attributes":{"self":{"serialNo":"C9A01427553848449C76864118A014E6","menuid":"002002","menuname":"水电费","menuURL":"jsp/support/waterElectricity","parentid":"002"}},"children":[],"parentId":"002","hasParent":true,"hasChildren":false},{"id":"002003","text":"房屋租金","state":null,"checked":false,"attributes":{"self":{"serialNo":"D4E33EF7C17747E9A52F765A71CBDABE","menuid":"002003","menuname":"房屋租金","menuURL":null,"parentid":"002"}},"children":[{"id":"002003002","text":"租金信息","state":null,"checked":false,"attributes":{"self":{"serialNo":"5D2D5331AFEC47FB8BA8D2AD15EFF2FF","menuid":"002003002","menuname":"租金信息","menuURL":"jsp/support/moneyPayInfo","parentid":"002003"}},"children":[],"parentId":"002003","hasParent":true,"hasChildren":false},{"id":"002003001","text":"租房合同","state":null,"checked":false,"attributes":{"self":{"serialNo":"C7E633BAA3DE4EB0803F2D8C2EDC3196","menuid":"002003001","menuname":"租房合同","menuURL":"jsp/support/rentalAgreement","parentid":"002003"}},"children":[],"parentId":"002003","hasParent":true,"hasChildren":false}],"parentId":"002","hasParent":true,"hasChildren":true}],"parentId":"000","hasParent":false,"hasChildren":true},{"id":"003","text":"财务","state":null,"checked":false,"attributes":{"self":{"serialNo":"53E6874290344BFB8AE0713706F31840","menuid":"003","menuname":"财务","menuURL":null,"parentid":"000"}},"children":[{"id":"003003","text":"住宿费","state":null,"checked":false,"attributes":{"self":{"serialNo":"131DDC09EDF549FE839DE571F6C2957D","menuid":"003003","menuname":"住宿费","menuURL":null,"parentid":"003"}},"children":[{"id":"003003002","text":"水电费","state":null,"checked":false,"attributes":{"self":{"serialNo":"7E7652596263419690E56D36A6B11083","menuid":"003003002","menuname":"水电费","menuURL":"jsp/finance/waterElectricity","parentid":"003003"}},"children":[],"parentId":"003003","hasParent":true,"hasChildren":false},{"id":"003003001","text":"租金","state":null,"checked":false,"attributes":{"self":{"serialNo":"A1519F777F5941E08E08634CC3770976","menuid":"003003001","menuname":"租金","menuURL":"jsp/finance/roomMoney","parentid":"003003"}},"children":[],"parentId":"003003","hasParent":true,"hasChildren":false}],"parentId":"003","hasParent":true,"hasChildren":true},{"id":"003002","text":"成考费","state":null,"checked":false,"attributes":{"self":{"serialNo":"8E0639EC6B7C4F399EFA5922BE329618","menuid":"003002","menuname":"成考费","menuURL":"jsp/finance/adultExam","parentid":"003"}},"children":[],"parentId":"003","hasParent":true,"hasChildren":false},{"id":"003001","text":"学费","state":null,"checked":false,"attributes":{"self":{"serialNo":"F734A1B966DC4D47A7C473165C7B262B","menuid":"003001","menuname":"学费","menuURL":null,"parentid":"003"}},"children":[{"id":"003001002","text":"升学学费","state":null,"checked":false,"attributes":{"self":{"serialNo":"0AAADCF1F3884271A5C42EE845F47317","menuid":"003001002","menuname":"升学学费","menuURL":"jsp/finance/higherGrade","parentid":"003001"}},"children":[],"parentId":"003001","hasParent":true,"hasChildren":false},{"id":"003001001","text":"开学学费","state":null,"checked":false,"attributes":{"self":{"serialNo":"7F84AB7E29B44987BF117A69A2750B6B","menuid":"003001001","menuname":"开学学费","menuURL":"jsp/finance/enteredSchool","parentid":"003001"}},"children":[],"parentId":"003001","hasParent":true,"hasChildren":false}],"parentId":"003","hasParent":true,"hasChildren":true}],"parentId":"000","hasParent":false,"hasChildren":true},{"id":"001","text":"学生管理","state":null,"checked":false,"attributes":{"self":{"serialNo":"7F0BCDA507EA426E9A49C1A6EC184569","menuid":"001","menuname":"学生管理","menuURL":null,"parentid":"000"}},"children":[{"id":"001005","text":"就业信息","state":null,"checked":false,"attributes":{"self":{"serialNo":"425CA8235A894573B65BA269D5550AF6","menuid":"001005","menuname":"就业信息","menuURL":"jsp/student/employmentInfo","parentid":"001"}},"children":[],"parentId":"001","hasParent":true,"hasChildren":false},{"id":"001003","text":"缴费信息","state":null,"checked":false,"attributes":{"self":{"serialNo":"7FB270DC0BA745E7A58EA65D41B70D8F","menuid":"001003","menuname":"缴费信息","menuURL":"jsp/student/paymentInfo","parentid":"001"}},"children":[],"parentId":"001","hasParent":true,"hasChildren":false},{"id":"001001","text":"学生相关信息","state":null,"checked":false,"attributes":{"self":{"serialNo":"CF0BBFA5E82543F5A29B62E73614266A","menuid":"001001","menuname":"学生相关信息","menuURL":"jsp/student/studentReferInfo","parentid":"001"}},"children":[],"parentId":"001","hasParent":true,"hasChildren":false},{"id":"001004","text":"表现相关信息","state":null,"checked":false,"attributes":{"self":{"serialNo":"DE820480D19248F59443ECEAF1388506","menuid":"001004","menuname":"表现相关信息","menuURL":"jsp/student/performanceInfo","parentid":"001"}},"children":[],"parentId":"001","hasParent":true,"hasChildren":false},{"id":"001002","text":"班级相关信息","state":null,"checked":false,"attributes":{"self":{"serialNo":"E8BF8DBC615A4066BC0A069DF4DC0D01","menuid":"001002","menuname":"班级相关信息","menuURL":"jsp/student/classReferInfo","parentid":"001"}},"children":[],"parentId":"001","hasParent":true,"hasChildren":false}],"parentId":"000","hasParent":false,"hasChildren":true}]
7.然后复制运行结果到http://tool.oschina.net/codeformat/json/该网址进行json代码格式化
? 然后再把已经格式化的代码复制到tree_data1.json里面
8.第七步:更改index.js的跳转路径
$(function(){
$('#stuMenu').tree({
url:$("#ctx").val()+'/menu.action?methodName=tree',
onClick: function(node){
//alert(node.text); // 在用户点击的时候提示
var exists=$('#stuTabs').tabs('exists',node.text);
if(exists){
$('#stuTabs').tabs('select',node.text);
}else{
$('#stuTabs').tabs('add',{
title:node.text,
content:'<iframe height="100%" width="100%" src="'+node.attributes.url+'"></iframe>',
closable:true,
tools:[{
iconCls:'icon-mini-refresh',
handler:function(){
alert('refresh');
}
}]
});
}
}
});
})
url:$("#ctx").val()+'/menu.action?methodName=tree'
这里的$("#ctx").val()是在页面那边调用了一个隐藏字段
<input type="hidden" id="ctx" value="${pageContext.request.contextPath }">
以上就是Tree后端实现全部步骤? ?看最后结果
?
|