博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
dtree实现动态加载树形菜单,动态插入树形菜单
阅读量:5280 次
发布时间:2019-06-14

本文共 10127 字,大约阅读时间需要 33 分钟。

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

/dtree.jsp
/dtree.jsp
/dtree.jsp

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" %>    
树形结构例子

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

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ page import="com.dtree.test.*" %><%@ taglib prefix="s" uri="/struts-tags" %><% %>                 
         

m >

         

 

         

 

         

 

         

 

         
           
             
             
             
             
             
           
           
             
             
             
             
             
           
           
             
             
             
             
             
           
         
插入一级名称
               
             
 
               
             
 
插入二级名称
               
             
             
               
()" name="Submit2" value="提交">             
 
插入三级名称
               
             
               
             
 
       
 

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

转载于:https://www.cnblogs.com/azhqiang/p/4097115.html

你可能感兴趣的文章
Magento开发文档(三):Magento控制器
查看>>
性能调优攻略
查看>>
ie6解决png图片透明问题
查看>>
瞬间的永恒
查看>>
2019-8-5 考试总结
查看>>
JS中实现字符串和数组的相互转化
查看>>
web service和ejb的区别
查看>>
Windows Azure Cloud Service (29) 在Windows Azure发送邮件(下)
查看>>
CS61A Efficiency 笔记
查看>>
微信上传素材返回 '{"errcode":41005,"errmsg":"media data missing"}',php5.6返回
查看>>
div或者p标签单行和多行超出显示省略号
查看>>
Elasticsearch 滚动重启 必读
查看>>
Hadoop基本概念
查看>>
java.util.zip压缩打包文件总结一:压缩文件及文件下面的文件夹
查看>>
浅说 apache setenvif_module模块
查看>>
MySQL--数据插入
查看>>
重新学习python系列(二)? WTF?
查看>>
shell脚本统计文件中单词的个数
查看>>
SPCE061A学习笔记
查看>>
sql 函数
查看>>