声明:此博文是根据网上一位大神的文章写的,和这位大神的著作大同小意,非常感谢这位大神。
首先把JQuery zTree v3.3.zip解压放到web目录下(可以根据业务需求自定义),然后看一下代码应用的整体结构:
以自己的demo为例:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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"> <link rel="stylesheet" href="zTree/css/demo.css" type="text/css"> <link rel="stylesheet" href="zTree/css/zTreeStyle/zTreeStyle.css" type="text/css"> <script type="text/javascript" src="zTree/js/jquery-1.4.4.min.js"></script> <script type="text/javascript" src="zTree/js/jquery.ztree.core.js"></script> <script type="text/javascript" src="zTree/js/jquery.ztree.excheck.js"></script> <script type="text/javascript" src="zTree/js/jquery.ztree.exedit.js"></script>
JsonDataServlet.java
package com.xbmu.demo; import java.io.IOException; import java.net.URLDecoder; import java.sql.SQLException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import com.xbmu.bean.Car; import com.xbmu.utils.DBCPUtil; @WebServlet(urlPatterns="/jsondata") public class JsonDataServlet extends HttpServlet { QueryRunner qr = new QueryRunner(DBCPUtil.getDataSource()); @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method = req.getParameter("method"); if("deletePp".equals(method)){ deletePp(req,resp); }else if("addPp".equals(method)){ addPp(req,resp); }else if("updatePp".equals(method)){ updatePp(req,resp); } resp.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); String data = getData(); resp.getWriter().write(data); } //更新节点 private void updatePp(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ req.setCharacterEncoding("utf-8"); String id = req.getParameter("id"); id = URLDecoder.decode(id,"utf-8"); String name = req.getParameter("name"); name = URLDecoder.decode(name,"utf-8"); try { String sql = "update car set name=? where id=?"; int update = qr.update(sql, name,id); if(update==1){ System.out.println("修改成功"); }else{ System.out.println("修改失败"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //增加节点 private void addPp(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String pId = req.getParameter("pId"); pId = URLDecoder.decode(pId,"utf-8"); String name = req.getParameter("name"); name = URLDecoder.decode(name,"utf-8"); try { String sql = "insert into car(pId,name) values(?,?)"; int update = qr.update(sql, pId,name); if(update==1){ System.out.println("添加成功"); }else{ System.out.println("添加失败"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //删除节点 private void deletePp(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String id = req.getParameter("id"); id = URLDecoder.decode(id,"utf-8"); String sql = "delete from car where id=?"; try { int update = qr.update(sql, id); if(update==1){ System.out.println("删除成功"); }else{ System.out.println("删除失败"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } public String getData(){ String sql = "select * from car"; JSONArray array = new JSONArray(); try { List<Car> carList = qr.query(sql, new BeanListHandler<Car>(Car.class)); for (Car car : carList) { JSONObject jsonObject = new JSONObject(); jsonObject.put("id",car.getId()); jsonObject.put("name",car.getName()); jsonObject.put("pId",car.getpId()); array.put(jsonObject); } System.out.println(array.toString()); } catch (SQLException e) { e.printStackTrace(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return array.toString(); } }test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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"> <link rel="stylesheet" href="zTree/css/demo.css" type="text/css"> <link rel="stylesheet" href="zTree/css/zTreeStyle/zTreeStyle.css" type="text/css"> <script type="text/javascript" src="zTree/js/jquery-1.4.4.min.js"></script> <script type="text/javascript" src="zTree/js/jquery.ztree.core.js"></script> <script type="text/javascript" src="zTree/js/jquery.ztree.excheck.js"></script> <script type="text/javascript" src="zTree/js/jquery.ztree.exedit.js"></script> <script type="text/javascript"> var setting = { async : { enable : true,//开启异步加载处理 url : encodeURI(encodeURI("/peTreeDemo2/jsondata")), autoParam : [ "id" ], dataFilter : filter, contentType : "application/json", type : "get" }, view : { expandSpeed : "", addHoverDom : addHoverDom, removeHoverDom : removeHoverDom, selectedMulti : false }, edit : { enable : true }, data : { simpleData : { enable : true } }, callback : { beforeRemove : beforeRemove, beforeRename : beforeRename, } }; function filter(treeId, parentNode, childNodes) { if (!childNodes) return null; for (var i = 0, l = childNodes.length; i < l; i++) { childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.'); } return childNodes; } function beforeRemove(treeId, treeNode) { if (confirm("确认删除节点--" + treeNode.name + "--吗?")) { var param = "id=" + treeNode.id; $.post(encodeURI(encodeURI("/peTreeDemo2/jsondata?method=deletePp&" + param))); } else { return false; } } function beforeRename(treeId, treeNode, newName) { if (newName.length == 0) { alert("节点名称不能为空."); return false; } var param = "id=" + treeNode.id + "&name=" + newName; $.post(encodeURI(encodeURI("/peTreeDemo2/jsondata?method=updatePp&" + param))); return true; } function addHoverDom(treeId, treeNode) { var sObj = $("#" + treeNode.tId + "_span"); if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0) return; var addStr = "<span class='button add' id='addBtn_" + treeNode.tId + "' title='add node' onfocus='this.blur();'></span>"; sObj.after(addStr); var btn = $("#addBtn_" + treeNode.tId); if (btn) btn.bind("click", function() { var Ppname = prompt("请输入新节点名称"); if (Ppname == null) { return; } else if (Ppname == "") { alert("节点名称不能为空"); } else { var param ="&pId="+ treeNode.id + "&name=" + Ppname; var zTree = $.fn.zTree.getZTreeObj("treeDemo"); $.post( encodeURI(encodeURI("/peTreeDemo2/jsondata?method=addPp&" + param)), function(data) { if ($.trim(data) != null) { var treenode = $.trim(data); zTree.addNodes(treeNode, { pId : treeNode.id, name : Ppname }, true); } }) } }); }; function removeHoverDom(treeId, treeNode) { $("#addBtn_" + treeNode.tId).unbind().remove(); }; $(document).ready(function() { $.fn.zTree.init($("#treeDemo"), setting); }); </script> <style type="text/css"> .ztree li span.button.add { margin-left: 2px; margin-right: -1px; background-position: -144px 0; vertical-align: top; *vertical-align: middle } </style> </head> <body> <div class="content_wrap"> <div class="zTreeDemoBackground left"> <ul id="treeDemo" class="ztree"></ul> </div> </div> </body> </html>运行效果:
自己的写的一个小demo和网上一位大神做的笔记的下载地址:http://download.csdn.net/detail/btt2013/9479377