用的是spring+hibernate
web层代码
<!DOCTYPE html>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<HTML>
<HEAD>
<TITLE> ZTREE DEMO </TITLE>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link href="<c:url value="/zTree_v3/css/demo.css" />" rel="stylesheet" type="text/css">
<link href="<c:url value="/zTree_v3/css/zTreeStyle/zTreeStyle.css" />" rel="stylesheet" type="text/css">
<script type="text/javascript" src="<c:url value="/zTree_v3/js/jquery-1.4.4.min.js" />"></script>
<script type="text/javascript" src="<c:url value="/zTree_v3/js/jquery.ztree.core-3.5.js" />"></script>
<SCRIPT type="text/javascript">
<!--
// zTree 的参数配置,深入使用请参考 API 文档(setting 配置详解)
var setting = {
data:
{
simpleData:
{
enable: true,
idKey: "id",
pIdKey: "pId",
rootPId: 0
}
}
//每个节点上是否显示 CheckBox
};
// zTree 的数据属性,深入使用请参考 API 文档(zTreeNode 节点数据详解)
var zTree;
var treeNodes;
$(function(){
$.ajax({
async : false,
cache:false,
type: 'POST',
dataType : "json",
url: "getTree",//请求的action路径
error: function () {//请求失败处理函数
alert('fail');
},
success:function(data){ //请求成功后处理函数。
alert(data);
treeNodes = data; //把后台封装好的简单Json格式赋给treeNodes
}
});
$.fn.zTree.init($("#tree"), setting, treeNodes);
});
//-->
</SCRIPT>
</HEAD>
<BODY>
<div>
<ul id="tree" class="ztree"></ul>
</div>
</BODY>
</HTML>
mvc层代码
@RequestMapping(value="/getTree", method=RequestMethod.POST)
public void getTree(HttpServletRequest request,HttpServletResponse response) throws IOException{
//1.查询数据库,把所有数据获取
List<Type> queryResult=typeService.getAll();
List<String> lstTree = new ArrayList<String>();
for(Type type:queryResult)
{
lstTree.add("{id:"+type.getTid()+",pId:"+type.getSupertype()+",name:"+"\""+type.getName()+"\""+"}");//2.排成String s1 = "{id:1, pId:0, name:\"test1\"}"; 格式
System.out.println("{id:"+type.getTid()+",pId:"+type.getSupertype()+",name:"+"\""+type.getName()+"\""+"}"); //3.组合add
}
System.out.println(lstTree);
//4.组成json发到WEB页面
response.getWriter().print(JSONArray.fromObject(lstTree).toString());
}