1.combotree.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
%>
<!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>Insert title here</title>
<link rel="stylesheet" type="text/css"
href="<%=path%>/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
href="<%=path%>/easyui/themes/icon.css">
<link rel="stylesheet" type="text/css" href="<%=path%>/easyui/demo.css">
<script type="text/javascript"
src="<%=path%>/easyui/jquery-1.7.2.min.js"></script>
<script type="text/javascript"
src="<%=path%>/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript">
$(function(){
$("#t1").combotree({
url:'/egoTest/tree/loadTree.do',
300,
checkbox:true,
multiple:true
});
$("#btn1").click(function(){
console.info($("#t1").combotree('getValue'));
});
$("#btn2").click(function(){
var tree = $("#t1").combotree('tree');
var root = tree.tree('getRoot');
console.info(root);
});
});
</script>
<title>Insert title here</title>
</head>
<body>
<input id="t1"/>
<a id="btn1" class="easyui-linkbutton">获取tree选中的值</a>
<a id="btn2" class="easyui-linkbutton">获取tree对象</a>
</body>
</html>
2.TreeController.java
@RequestMapping("/loadTree")
public void getAjaxUser(HttpServletRequest request,
HttpServletResponse response, ModelMap modelMap, Page page) {
try {
String id = request.getParameter("id");
response.setContentType("text/html;charset=utf-8");
List<TreeVO> tList = treeService.queryTree(id);
response.getWriter().write(JSONArray.fromObject(tList).toString());
} catch (Exception e) {
e.printStackTrace();
}
}
3.treeService.java
public List<TreeVO> queryTree(String id) {
List<Resource> rList = treeDao.queryTree(id);
List<TreeVO> tList = new ArrayList<TreeVO>();
for (Iterator iterator = rList.iterator(); iterator.hasNext();) {
Resource resource = (Resource) iterator.next();
TreeVO tree = new TreeVO();
tree.setId(resource.getId());
tree.setText(resource.getName());
tree.setChecked(resource.getChecked());
tree.setIconCls(resource.getIcons());
tree.setParent_id(resource.getParent_id());
if(treeDao.queryTree(String.valueOf(resource.getId())).size() > 0){
tree.setState("closed");
}else{
tree.setState("open");
}
Map<String,Object> map = new HashMap<String,Object>();
map.put("url", resource.getUrl());
tree.setAttributes(map);
tList.add(tree);
}
return tList;
}
4.Resource.java
private int id;
private String name;
private String url;
private int checked;
private String icons;
private int parent_id;
5.treeVO.java 不用与表中的实体类对应
private int id;
private String text;
private String iconCls;
private int checked;
private int parent_id;
private String state;
private Map<String, Object> attributes = new HashMap<String, Object>();
public TreeVO(){}
public TreeVO(int id, String text, String iconCls, int checked,
int parent_id, String state, Map<String, Object> attributes) {
super();
this.id = id;
this.text = text;
this.iconCls = iconCls;
this.checked = checked;
this.parent_id = parent_id;
this.state = state;
this.attributes = attributes;
}
6.resource表
/*
Navicat MySQL Data Transfer
Source Server : mysql_client
Source Server Version : 50045
Source Host : 127.0.0.1:3306
Source Database : salesproject
Target Server Type : MYSQL
Target Server Version : 50045
File Encoding : 65001
Date: 2015-10-18 07:38:25
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for resource
-- ----------------------------
DROP TABLE IF EXISTS `resource`;
CREATE TABLE `resource` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
`url` varchar(255) default NULL,
`checked` int(11) default '0',
`icons` varchar(255) default NULL,
`parent_id` int(11) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=76 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of resource
-- ----------------------------
INSERT INTO `resource` VALUES ('1', '权限菜单', null, '0', null, '999999');
INSERT INTO `resource` VALUES ('2', '用户管理', null, '0', null, '1');
INSERT INTO `resource` VALUES ('3', '岗位管理', null, '0', null, '1');
INSERT INTO `resource` VALUES ('4', '资源管理', null, '0', null, '1');
INSERT INTO `resource` VALUES ('5', '用户功能1', null, '0', null, '2');
INSERT INTO `resource` VALUES ('7', '岗位功能1', '333', '0', null, '3');
INSERT INTO `resource` VALUES ('60', '资源功能1', '31', '0', null, '4');
INSERT INTO `resource` VALUES ('61', '资源功能2', '34', '0', null, '4');
INSERT INTO `resource` VALUES ('62', '资源功能3', '2', '0', null, '4');
INSERT INTO `resource` VALUES ('63', '资源功能4', 'aa', '0', null, '4');
INSERT INTO `resource` VALUES ('70', '权限管理', 'aa', '0', null, '1');
INSERT INTO `resource` VALUES ('75', '权限管理11', '66', '0', null, '70');