2、将map集合转化成josn数据JsonObject obj1=new JsonObject("1", "张三", null);
JsonObject obj2=new JsonObject("2", "李四", null);
Listlist=new ArrayList ();
list.add(obj1);
list.add(obj2);
ObjectMapper om=new ObjectMapper();
System.out.println("方式一:"+om.writevalueAsString(list));
二: 让josn书局有父子层级关系Map< String, Object> map=new HashMap
();
map.put("id", "1");
map.put("name", "张三");
map.put("price", null);
Map< String, Object> map2=new HashMap();
map2.put("id", "2");
map2.put("name", "李四");
map2.put("price", null);
List
主界面代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>后台界面
js文件代码
$(function() {
$('#tt').tree({
url:$("#ctx").val()+"/menu.action?methodName=tree",
onClick : function(node) {
var exists = $('#ttt').tabs('exists', node.text);
if (exists) {
$('#ttt').tabs('select', node.text);
} else {
$('#ttt')
.tabs(
'add',
{
title : node.text,
content : '
MVC配置
子控制器代码
package com.zking.util; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.wmy.dao.Menudao; import com.wmy.entity.Menu; import com.wmy.entity.TreeVo; import com.zking.framework.ActionSupport; import com.zking.framework.ModelDriver; public class Menuaction extends ActionSupport implements ModelDriver
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 writem(HttpServletResponse response,Object o)throws Exception{
ObjectMapper om=new ObjectMapper();
write(response, om.writevalueAsString(o));
}
}
将数据变成具有父子层级
package com.zking.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.wmy.entity.TreeVo;
public class BuildTree {
T> 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;
}
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;
}
}
运行结果
后端数据库数据就显示到前端页面上了



