前言:给大家讲解tree后端实现
码字不易,点个关注
转载请说明!
开发工具:eclipse
目录
二、根据属性创建实体类(与数据库一致)以及dao类,action类...
接着我上篇博客前端实现讲:
需要导的架包:
一、目标:将tree的死数据变成数据库中的读取方式
1.实体类对象转换为json串---》json对象
JsonObject1 obj1 = new JsonObject1("11", "学生管理", "closed");
ObjectMapper om = new ObjectMapper();
System.out.println(om.writeValueAsString(obj1));
2.map集合转换成json串 json对象
Map<String,Object> map = new HashMap<String,Object>();
map.put("id", "11");
map.put("text", "学生管理");
map.put("state", "closed");
运行结果如下:
实体类集合对象转换成Json串 -> json数组 -->方式一
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串 -> 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));
运行结果如下:
结论:就json串的转换结果而言,Map等价于对象 List<Map>等价于List<创建的类>
二、根据属性创建实体类(与数据库一致)以及dao类,action类...
package com.hpw.entity;
public class Menu {
private String serialNo;
private String menuid;
private String menuname;
private String menuURL;
private String parentid;
public String getSerialNo() {
return serialNo;
}
public void setSerialNo(String serialNo) {
this.serialNo = serialNo;
}
public String getMenuid() {
return menuid;
}
public void setMenuid(String menuid) {
this.menuid = menuid;
}
public String getMenuname() {
return menuname;
}
public void setMenuname(String menuname) {
this.menuname = menuname;
}
public String getMenuURL() {
return menuURL;
}
public void setMenuURL(String menuURL) {
this.menuURL = menuURL;
}
public String getParentid() {
return parentid;
}
public void setParentid(String parentid) {
this.parentid = parentid;
}
@Override
public String toString() {
return "Menu [serialNo=" + serialNo + ", menuid=" + menuid + ", menuname=" + menuname + ", menuURL=" + menuURL
+ ", parentid=" + parentid + "]";
}
public Menu() {
}
}
dao类:
package com.hpw.dao;
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<TreeVo<Menu>> listVos=new ArrayList<TreeVo<Menu>>();
for (Menu m : list) {
TreeVo<Menu> vo=new TreeVo<Menu>();
vo.setId(m.getMenuid());
vo.setText(m.getMenuname());
vo.setParentId(m.getParentid());
Map<String, Object> map = new HashMap<String,Object>();
map.put("self",m.getMenuURL());
vo.setAttributes(map);
listVos.add(vo);
}
return BuildTree.buildList(listVos, "000");
}
}
调用的buildList()方法为帮助类里面的方法
buildTree:
package com.hpw.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BuildTree {
/**
* 默认-1为顶级节点
* @param nodes
* @param <T>
* @return
*/
public static <T> TreeVo<T> build(List<TreeVo<T>> nodes) {
if (nodes == null) {
return null;
}
List<TreeVo<T>> topNodes = new ArrayList<TreeVo<T>>();
for (TreeVo<T> children : nodes) {
String pid = children.getParentId();
if (pid == null || "-1".equals(pid)) {
topNodes.add(children);
continue;
}
for (TreeVo<T> parent : nodes) {
String id = parent.getId();
if (id != null && id.equals(pid)) {
parent.getChildren().add(children);
children.setHasParent(true);
parent.setChildren(true);
continue;
}
}
}
TreeVo<T> root = new TreeVo<T>();
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<String, Object> state = new HashMap<>(16);
state.put("opened", true);
root.setState(state);
}
return root;
}
/**
* 指定idparam为顶级节点
* @param nodes
* @param idParam
* @param <T>
* @return
*/
public static <T> List<TreeVo<T>> buildList(List<TreeVo<T>> nodes, String idParam) {
if (nodes == null) {
return null;
}
List<TreeVo<T>> topNodes = new ArrayList<TreeVo<T>>();
for (TreeVo<T> children : nodes) {
String pid = children.getParentId();
if (pid == null || idParam.equals(pid)) {
topNodes.add(children);
continue;
}
for (TreeVo<T> 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;
}
}
TreeVo:
package com.hpw.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class TreeVo<T> {
/**
* 节点ID
*/
private String id;
/**
* 显示节点文本
*/
private String text;
/**
* 节点状态,open closed
*/
private Map<String, Object> state;
/**
* 节点是否被选中 true false
*/
private boolean checked = false;
/**
* 节点属性
*/
private Map<String, Object> attributes;
/**
* 节点的子节点
*/
private List<TreeVo<T>> children = new ArrayList<TreeVo<T>>();
/**
* 父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<String, Object> getState() {
return state;
}
public void setState(Map<String, Object> state) {
this.state = state;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public Map<String, Object> getAttributes() {
return attributes;
}
public void setAttributes(Map<String, Object> attributes) {
this.attributes = attributes;
}
public List<TreeVo<T>> getChildren() {
return children;
}
public void setChildren(List<TreeVo<T>> 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<String, Object> state, boolean checked, Map<String, Object> attributes,
List<TreeVo<T>> children, boolean isParent, boolean isChildren, String parentID) {
super();
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() {
super();
}
}
子控制器MenuAction:
public Menu menu = new Menu();
public MenuDao dao = new MenuDao();
public Menu getModel() {
return menu;
}
public String tree(HttpServletRequest req, HttpServletResponse resp) throws Exception {
List<TreeVo<Menu>> tree = dao.tree(menu, null);
ResponseUtil.writeJson(resp, tree);
return null;
}
三、配置conf文件夹下的xml文件
<?xml version="1.0" encoding="UTF-8"?>
<config>
<action type="com.zking.web.MenuAction" path="/menu"></action>
</config>
四、修改(static)的js文件的url
$(function() {
/**
* $.ajax无刷新 $.hello
* $.extends("hello",function(){
* alert("hello");
* })
* 1.tree方法是通过$.extends()拓展出来的
* 2.tree方法做的事
* $('#tt').append("<li<span>Friend</spa></li>");
* .append("<li<span>Friend</spa></li>");
*
* 需求:
* 1.点击左侧显示右侧Tab
* 1.1给菜单添加点击事件
* 1.2调用tabs选项卡打开对应的页面
* 选项卡打开
* 选项卡对应页面展示
* 1.3新建对应的打开页面
* 2.不能打开重复的窗口
* 拿到目前所有打开的tabs选项卡,与将要打开的选项卡做对比(exists)
* 存在true:不打开
* 存在false :打开
* 3.对于已经存在的Tab选项,被点击的时候应该默认被选中
* 4.点击菜单,能够访问对应的页面,而非文字内容
*/
$('#stuMenu').tree({
//将tree的死数据变成数据库中的读取方式
url:$("#ctx").val()+'/menu.action?methodName=tree',
onClick: function(node) {
//alert(node.text);//用户在点击的时候提示
// add a new tab panel
//判断当前将要打开的选项卡是否存在
var exists = $('#stuTabs').tabs('exists',node.text);
//alert(node.attributes.url);
if(exists){
$('#stuTabs').tabs('select',node.text);
}else{
//iframe
$('#stuTabs').tabs('add',{
title:node.text,
content:'<iframe width="100%" height="100%"
src="'+node.attributes.url+'"></ifrane>',
closable:true,
tools:[{
iconCls:'icon-mini-refresh',
handler:function(){
alert('refresh');
}
}]
});
}
}
});
})
注意:js文件中,不支持el表达式
所以绝对路径不能用${ pageContext.request.contextPath },要在jsp文件中加入一个隐藏的input
运行结果如下(数据库的活数据都进去了):
到这里就结束了,我依旧是那个学IT的小学生
欢迎大佬指点