ztree官网:http://www.ztree.me/v3/main.php#_zTreeInfo
页面:
<span style="font-size:14px;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <link rel="stylesheet" type="text/css" href="jquery/zTree/css/demo.css"> <link rel="stylesheet" type="text/css" href="jquery/zTree/css/zTreeStyle/zTreeStyle.css"> <script type="text/javascript" src="jquery/zTree/js/jquery-1.4.4.min.js"></script> <script type="text/javascript" src="jquery/zTree/js/jquery.ztree.core-3.5.min.js"></script><!-- 核心js --> <script type="text/javascript" src="jquery/zTree/js/jquery.ztree.excheck-3.5.min.js"></script><!-- 复选框js --> <script type="text/javascript" src="jquery/zTree/js/jquery.ztree.exedit-3.5.min.js"></script><!-- 可编辑js --> </head> <script type="text/javascript"> // zTree v3.x用法 function init(){ var setting = { async:{ enable: true,//启用异步加载 url:"TestzTreeServlet?type=load", autoParam:["id", "name"],//后台接受参数的名称 //dataFilter: filter//数据过滤 type:"post" }, view:{ showIcon:true,//显示图标 showLine:true,//显示连线 showTitle:true, fontCss:{color:"blue"}//字体样式 }, edit:{ enable: true, //允许编辑节点 showRenameBtn: true, renameTitle: setRenameTitle, showRemoveBtn:false }, callback:{ onAsyncError:function(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown){//加载异常 alert(errorThrown); }, onRename:function(event, treeId, treeNode, isCancel){ var ps={id:treeNode.id,name:treeNode.name}; $.post("TestzTreeServlet?type=edit",ps,function(data){ var treeObj=$.fn.zTree.getZTreeObj(treeId); if(data!="true"){ alert(data); treeObj.refresh(); } }); }, onClick:show //节点点击事件 } }; /* var treeNodes=[{id:1,pId:0,name:"文件夹1",isParent:true} {id:11,pId:1,name:"文件1",isParent:false} {id:12,pId:1,name:"文件2",isParent:false} {id:2,pId:0,name:"文件夹1",isParent:true} ]; $.fn.zTree.init($("#file_tree"), setting,treeNodes); */ $.fn.zTree.init($("#file_tree"), setting); } 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 show(event, treeId, treeNode){ alert("id="+treeNode.id+",pId="+treeNode.pId); } function setRenameTitle(treeId, treeNode) { return treeNode.isParent ? "编辑父节点名称":"编辑叶子节点名称"; } </script> <body onload="init()"> 呵呵。。。 <div class="zTreeDemoBackground left" style="margin-left:50px;"> <ul id="file_tree" class="ztree"></ul> </div> </body> </html> </span>
后台:
<span style="font-size:14px;">package servlet; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray; //import com.google.gson.JsonArray; import util.TreeJson; public class TestzTreeServlet extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html"); String id=request.getParameter("id"); String type=request.getParameter("type"); if(id==null || "".equals(id)){ id="E:/test"; } if(type!=null && type.equals("edit")){ String name=request.getParameter("name"); String msg="true"; try{ if(name==null || name.trim().equals("")){ msg="节点名称不能为空!"; } editFile(id,name); }catch(Exception e){ msg="编辑失败:"+e.getMessage(); } response.getWriter().write(msg); }else{ List list=readPath(id); JSONArray json=JSONArray.fromObject(list); response.getWriter().write(json.toString()); } } public static List readPath(String path){ List list=new ArrayList(); File file = new File(path); if(file.exists()){ File[] fs=file.listFiles(); if(fs!=null && fs.length>0){//有多个文件 for(int i=0;i<fs.length;i++){ Map map = new HashMap(); map.put("id", fs[i].getAbsolutePath()); map.put("pId", path); map.put("name", fs[i].getName()); map.put("isParent",fs[i].isFile()==true?"false":"true"); list.add(map); } } } return list; } public static void editFile(String path,String rename) throws Exception{ File file = new File(path); if(file.exists()){ String rePath=""; if(path.contains("/")){ rePath=path.substring(0, path.lastIndexOf("/")+1)+rename; }else if(path.contains("\\")){ rePath=path.substring(0, path.lastIndexOf("\\")+1)+rename; } file.renameTo(new File(rePath)); } } } </span>
效果:
时间: 2024-11-08 21:31:20