1.导入  dtree文件    dtree.css   img文件夹   dtree.js

2. 建立对应 的数据库      1      父ID     name    id

3    建立连接类     mysql   例子

package com.dtree.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import com.opensymphony.xwork2.ActionSupport; public class ConnectionManager { private static final String driver ="org.gjt.mm.mysql.Driver";
private static final String url = "jdbc:mysql://localhost:3306/yuqing?useUnicode=true&characterEncoding=utf8";
private static final String username="root";
private static final String userpwd="zfn"; public static Connection getConnection(){ Connection conn=null;
try{
Class.forName(driver);
conn=DriverManager.getConnection(url,username,userpwd);
}catch(Exception ex){
ex.printStackTrace();
}
return conn;
}
public static void closeConnection(Connection conn){
try{
if(conn!=null && (!conn.isClosed())){
conn.close();
}
}catch(SQLException ex){
ex.printStackTrace();
}
}
public static void closeResultSet(ResultSet res){
try{
if(res!=null){
res.close();
res=null;
}
}catch(SQLException ex){
ex.printStackTrace();
}
}
public static void closeStatement(PreparedStatement stmt){
try{
if(stmt!=null){
stmt.close();
}
}catch(SQLException ex){
ex.printStackTrace();
}
}
}

4. 实现类

package com.dtree.test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; public class OrganizationService { private Connection conn;
private PreparedStatement titleQuery;
private ResultSet results; public List getOrganization(){
List list = new ArrayList(); try {
conn=ConnectionManager.getConnection();
String sql="select * from treetest";
titleQuery=conn.prepareStatement(sql);
results=titleQuery.executeQuery(); while(results.next()){
Organization org1 = new Organization();
org1.setId(results.getInt("id"));
org1.setParentId(results.getInt("parentId"));
org1.setName(results.getString("name")); list.add(org1);
} } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{ ConnectionManager.closeResultSet(results);
ConnectionManager.closeStatement(titleQuery);
ConnectionManager.closeConnection(conn);
} return list; } public int insertUser1(String name) {
int num=0;
try {
conn=ConnectionManager.getConnection();
String sql="insert into treetest (parentId,name,pid)values(0,?,1)";
titleQuery=conn.prepareStatement(sql);
titleQuery.setString(1,name); num=titleQuery.executeUpdate(); } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
ConnectionManager.closeResultSet(results);
ConnectionManager.closeStatement(titleQuery);
ConnectionManager.closeConnection(conn);
} return num;
} public int insertUser2(int parentId,String name) {
int num=0;
try {
conn=ConnectionManager.getConnection();
String sql="insert into treetest (parentId,name,pid)values(?,?,2)";
titleQuery=conn.prepareStatement(sql);
titleQuery.setInt(1,parentId);
titleQuery.setString(2,name); num=titleQuery.executeUpdate(); } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
ConnectionManager.closeResultSet(results);
ConnectionManager.closeStatement(titleQuery);
ConnectionManager.closeConnection(conn);
} return num;
}
public List getOrganization1(){
List list = new ArrayList(); try {
conn=ConnectionManager.getConnection();
String sql="select * from treetest where pid=1";
titleQuery=conn.prepareStatement(sql);
results=titleQuery.executeQuery(); while(results.next()){
Organization org1 = new Organization();
org1.setId(results.getInt("id"));
org1.setParentId(results.getInt("parentId"));
org1.setName(results.getString("name")); list.add(org1);
} } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{ ConnectionManager.closeResultSet(results);
ConnectionManager.closeStatement(titleQuery);
ConnectionManager.closeConnection(conn);
} return list;
}
}

5. 实体类

package com.dtree.test;

public class Organization {
private int id; private String name; private int parentId; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getParentId() {
return parentId;
} public void setParentId(int parentId) {
this.parentId = parentId;
}
}

6 action

package com.dtree.test;
import java.util.ArrayList;
import java.util.List; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport; public class DtreeAction extends ActionSupport { List list = new ArrayList();
private String name = null;
private String name2 = null;
private int parentId;
public int getParentId() {
return parentId;
}
public void setParentId(int parentId) {
this.parentId = parentId;
}
public String getName2() {
return name2;
}
public void setName2(String name2) {
this.name2 = name2;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute() {
OrganizationService service = new OrganizationService();
list = service.getOrganization(); return SUCCESS;
} public String insertname1() {
OrganizationService service = new OrganizationService();
System.out.print(name);
int num=service.insertUser1(name); return SUCCESS;
} public String insertname2() {
OrganizationService service = new OrganizationService();
System.out.println(parentId);
System.out.println(name);
int num=service.insertUser2(parentId, name2); return SUCCESS;
} public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}

6 struts  xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>
<package name="dtree" namespace="/dtree" extends="struts-default">
<action name="dtree" class="com.dtree.test.DtreeAction">
<result name="success">/dtree.jsp</result>
</action> <action name="insertname1" class="com.dtree.test.DtreeAction" method="insertname1">
<result name="success">/dtree.jsp</result>
</action> <action name="insertname2" class="com.dtree.test.DtreeAction" method="insertname2">
<result name="success">/dtree.jsp</result>
</action> </package> </struts>

7 显示树形菜单  jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<base href="<%=basePath%>"> <title>树形结构例子</title>
<link rel="stylesheet" type="text/css" href="styles.css">
<link rel="StyleSheet" href="css/dtree.css" type="text/css" />
<script type="text/javascript" src="script/dtree.js"></script>
</head> <body>
<div class="dtree">
<script type="text/javascript">
d = new dTree('d');
//构造根节点
d.add(0,-1,'学校人员分布'); <s:iterator value="list">
d.add(<s:property value="id"/>,<s:property value="parentId"/>,'<s:property value="name"/>','example01.html');
</s:iterator>
document.write(d);
</script> </div>
</body>
</html>

8  动态增加 树形菜单列的jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.dtree.test.*" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<% &nbsp;%>
<html>
&nbsp; <head>&nbsp;
&nbsp; &nbsp; <title></title>
&nbsp; &nbsp; <script type="text/javascript">
&nbsp; &nbsp; &nbsp; &nbsp; function sbmt(){
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;document.fm.action="dtree/insertname1.action";
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fm.submit();
&nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; function sbmt2(){
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;document.fm.action="dtree/insertname2.action";
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fm.submit();
&nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</script> &nbsp; </head>
&nbsp;&nbsp;
&nbsp; <body> <form name="fm" method="post" action="">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>m &gt; <br></p>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp;</p>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp;</p>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp;</p>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp;</p>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <table width="846" height="63" border="0">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th width="118" scope="col"><div align="center">插入一级名称</div></th>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th width="217" scope="col"><div align="center">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="name">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div></th>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th width="185" scope="col">&nbsp;</th>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th width="239" scope="col"><div align="center">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="button" onClick="return sbmt()" name="Submit" value="提交">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div></th>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th width="65" scope="col">&nbsp;</th>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><div align="center">插入二级名称</div></td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><div align="center">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="name2">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div></td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><select name="parentId" >
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<%
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;OrganizationService o=new OrganizationService();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;List list=o.getOrganization1();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(int i=0;i<list.size();i++){
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Organization o1=(Organization)list.get(i);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; %>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value=<%=o1.getId() %> selected><%=o1.getName() %></option>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<%} %>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </select> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><div align="center">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="button" onClick="return sbmt2<span style="white-space:pre"> </span>()" name="Submit2" value="提交">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div></td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp;</td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><div align="center">插入三级名称</div></td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><div align="center">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="name3">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div></td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><select name="select2">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </select></td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><div align="center">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="button" name="Submit3" value="提交">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div></td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp;</td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </table>
&nbsp; &nbsp; &nbsp; &nbsp; </form> &nbsp; </body>
</html>

http://blog.csdn.net/zhangfuning1986/article/details/7362109

dtree实现动态加载树形菜单,动态插入树形菜单的更多相关文章

  1. 雷林鹏分享:jQuery EasyUI 树形菜单 - 树形网格动态加载

    jQuery EasyUI 树形菜单 - 树形网格动态加载 动态加载树形网格有助于从服务器上加载部分的行数据,避免加载大型数据的长时间等待.本教程将向您展示如何创建带有动态加载特性的树形网格(Tree ...

  2. vue路由动态加载

    注意:是动态加载不是动态路由 解决的问题: 动态配置菜单栏的路由参数--实现菜单级的权限控制 问题成因: 在vue实例化的时候vuex.vue-router 就需要加载完毕,无法使用异步的方式从服务器 ...

  3. c#实现动态加载Dll(转)

    c#实现动态加载Dll 分类: .net2009-12-28 13:54 3652人阅读 评论(1) 收藏 举报 dllc#assemblynullexceptionclass 原理如下: 1.利用反 ...

  4. 动态加载js和css

    开发过程中经常需要动态加载js和css,今天特意总结了一下常用的方法. 1.动态加载js 方法一:动态加载js文件 // 动态加载js脚本文件 function loadScript(url) { v ...

  5. CS.动态加载DLL.动态生成.运行代码.BS.AutoFac管理实现类

    以英雄联盟为例.界面上经常有Load....xxxx.dll.一般都是加载子系统.比如装备系统.英雄系统等.在实际开发中很多项目非常庞大.都会分割成独立子解决方案开发.后期就需要加载回来.一般都是利用 ...

  6. flash的动态加载技术

    这里所说的动态加载技术, 主要是指代码模块(可以是swc也可以是swf)的动态加载.即主swf在运行的时候, 可以根据需要动态加载所需的代码模块. 为了讨论方便, 下面所说的代码模块都用swc表示,用 ...

  7. 第一百一十八节,JavaScript,动态加载脚本和样式

    JavaScript,动态加载脚本和样式 一动态脚本 当网站需求变大,脚本的需求也逐步变大.我们就不得不引入太多的JS脚本而降低了整站的性能,所以就出现了动态脚本的概念,在适时的时候加载相应的脚本. ...

  8. 动态加载js,css(项目中需要的)

    最近做的一个项目需要加入百度统计,大家都知道百度统计在页面引用就是一坨js,实现方法很简单引用到页面就ok了. 那么问题来了,虽然我不知道百度统计的原理是啥,我的测试服引用了百度统计,百度统计账号里面 ...

  9. 动态加载js css 插件

    简介 动态加载js,css在现在以及将来肯定是很重要的.目前来看前端代码编写的业务量已经远远超过后端编写的.随着对用户体验度逐渐增强,前端业务复杂,加载速度变得很慢很慢.为了解决这个问题,目前出现的两 ...

  10. 通过Activity动态加载Fragment创建主界面构架

    在做项目中,需要建立一个主界面框架,尝试过使用ViewPager ,后来又换成了使用Activity动态加载Fragment实现选项卡的效果.总结一下方便以后回顾. 先给出总体效果: 要实现上述效果, ...

随机推荐

  1. Ansible介绍及安装部署

    本节内容: 运维工具 Ansible特性 Ansible架构图和核心组件 安装Ansible 演示使用示例 一.运维工具 作为一个Linux运维人员,需要了解大量的运维工具,并熟知这些工具的差异,能够 ...

  2. Spark(五)Spark任务提交方式和执行流程

    一.Spark中的基本概念 (1)Application:表示你的应用程序 (2)Driver:表示main()函数,创建SparkContext.由SparkContext负责与ClusterMan ...

  3. 矩阵链乘(UVa 442)

    结构体 struct matrix 用来保存矩阵的行和列: map<string,matrix> 用来保存矩阵名和相应的行列数: stack<string> 用来保存表达式中遇 ...

  4. day6 subprocess模块、logging模块

        logging模块 很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储 ...

  5. ASP.NET MVC5+ 路由特性

    概述 ASP.NET MVC 5支持一种新的路由协议,称为路由特性. MVC5也支持以前定义路由的方式,你可以在一个项目中混合使用这两种方式来定义路由. 案例 1.使用Visual Studio 20 ...

  6. USACO 5.5 Twofive

    TwofiveIOI 2001 In order to teach her young calvess the order of the letters in the alphabet, Bessie ...

  7. imageio 载入 Buffer 格式的图片

    题注:OpenCV 使用 pip install -U opencv-python 即可安装. import zipfile import imageio import cv2 # 载入压缩文件 Z ...

  8. 跑对抗样本库 CleverHans 的例子时,遇到的问题

    环境:Ubuntu+TensorFlow 首先是GPU被其他人占用了,怎么也跑不起来最简单的TensorFlow小例子. 所以先学会如何查看显卡使用情况,转去使用其他空闲显卡. Linux查看Nvid ...

  9. mysql高性能索引

    独立索引: 独立索引是指索引列不能是表达式的一部分,也不能是函数的参数 例1: SELECT actor_id FROM actor WHERE actor_id+1=5 --这种写法,就算在acto ...

  10. [BZOJ4700]适者(CDQ分治+DP/李超线段树)

    如果没有秒杀,就是经典的国王游戏问题,按t/a从小到大排序即可. 考虑删除两个数i<j能给答案减少的贡献:S[i]*T[i]+P[i-1]*A[i]-A[i]+S[j]*T[j]+P[j-1]* ...