数据库中的数据如下:

1.首先在Dao中实现查找方法

public List<ProductType> findAllByRoot() {
//定义集合,添加ProductType对象
List<ProductType> list = new ArrayList<ProductType>();
//查询语句,查询producttype表中的所有数据
String sql = "SELECT * FROM producttype ";
try {
//采用从C3P0获取connection连接
connection = jdbcUtil.getConnection();
//预编译
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
//定义ProductType对象,封装信息,并添加到集合当中,返回
ProductType productType = new ProductType();
productType.setTid(resultSet.getInt(1));
productType.setTypename(resultSet.getString(2));
productType.setPno(resultSet.getString(3));
productType.setParentTid(resultSet.getInt(4));
productType.setSort(resultSet.getInt(5));
list.add(productType);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
//关闭连接
jdbcUtil.closeAll(resultSet, preparedStatement, connection);
}
return list;
}

2.定义工具类

package com.bw.shop.util;

import java.util.ArrayList;
import java.util.List; import com.bw.shop.bean.ProductType;
import com.bw.shop.dao.impl.ProductTypeDaoImpl; public class TypeTree {
private List<ProductType> list = null;
//list所所有数据
private StringBuffer sb = new StringBuffer();
// 瓶装结果
String s = ""; // var tree1 = new WebFXTreeItem('电脑整机','javascript:cx(1)');
String x = ""; // tree.add(tree1); public StringBuffer getSb() {
return sb;
} public TypeTree(List<ProductType> list) {
this.list = list;
// list所所有数据
addTree(0);
// 从跟节点0开始调用 } // 调用递归方法 ,该方法就是 将生产 js 字符串存入StringBuffer中
public void addTree(int parentTid) {
for (ProductType productType : list) {
if (productType.getParentTid() == parentTid) {
// var tree1 = new WebFXTreeItem('电脑整机','javascript:cx(1)');
s = "var tree" + productType.getTid()
+ " = new WebFXTreeItem('" + productType.getTypename()
+ "','javascript:cx(" + productType.getTid() + ")');";
sb.append(s + "\n");
// tree.add(tree1);
if (parentTid == 0) {
x = "tree.add(tree" + productType.getTid() + ");";
} else {
x = "tree" + productType.getParentTid() + ".add(tree"
+ productType.getTid() + ");";
}
sb.append(x + "\n");
addTree(productType.getTid());// 递归调用 查看当前的数据的子分类
}
}
}
//返回结果
public static String getTree() {
return new TypeTree(new ProductTypeDaoImpl().findAllByRoot()).getSb()
.toString();
} }

3.页面赋值

<%@ page contentType="text/html; charset=UTF-8" %>
<%@page import="com.bw.shop.util.TypeTree"%> <script language="javascript">
function cx(tid){
if(tid==0){
document.baseInfoForm.action="productTypeRoot.jsp";
}else{
document.baseInfoForm.action="productTypeList.jsp";
} document.getElementById("tid").value=tid;
document.baseInfoForm.submit();
} </script>
<html> <head>
<title>菜单</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
<!-- .mouse{
cursor: hand;
font-size: 9pt;
}
.selected{
background-color:#003366;
color: #FFFFFF;
/*font-weight:bold*/
}
div {
white-space:nowrap;
color: #FFFFFF;
}
-->
</style>
<link href="css/style.css" rel="stylesheet" type="text/css">
<link rel="StyleSheet" href="css/xtree.css" type="text/css" />
<script type="text/javascript" src="js/xtree.js"></script>
</head> <body class="body_left">
<script type="text/javascript"> //实例一个根节点new WebFXTree( 节点名,节点事件 )
var tree = new WebFXTree('商品类别管理','javascript:cx(0)');
//设置样式
tree.setBehavior('classic'); <%=TypeTree.getTree()%> document.write(tree); </script> <form name="baseInfoForm" method="post" action="" target="typeright" >
<input name="pageNo" type="hidden" value="1" id="pageNo">
<input name="tid" type="hidden" value="" id="tid" >
</form>
</body>
</html>

运行结果如下:

java 定义mysql树形菜单的更多相关文章

  1. JAVA递归生成树形菜单

    递归生成一个如图的菜单,编写两个类数据模型Menu.和创建树形的MenuTree.通过以下过程实现: 1.首先从菜单数据中获取所有根节点. 2.为根节点建立次级子树并拼接上. 3.递归为子节点建立次级 ...

  2. java树形菜单实现

    java树形菜单实现 公司表: 部门表: 实体类: public class Node { private Integer companyId;//公司id private String compan ...

  3. java构建树形菜单递归工具类

    1.设计菜单实体 import java.util.List; public class Menu { //菜单id private Long id; //父节点id private Long par ...

  4. java 传入list集合 返回树形菜单,for循环遍历

    public List<SysPermissionVO> getTreeMenu(List<SysPermissionVO> list,SysPermissionVO sysP ...

  5. SSH框架CRUD+树形菜单案例

    今天结合了案例来写ssh的增删改查 表设计 t_ssh_tree t_vue_user  book 核心配置文件  struts-base.xml <?xml version="1.0 ...

  6. easyui实现树形菜单Tab功能、layout布局

    一:常见三种前端ui框架 在初学者入门的状态下,我们常见的前端框架有三种且都有自己的官方网站: 1.easyui:官方网站(http://www.jeasyui.net/) 基于jquery的用户页面 ...

  7. Android 多级树形菜单

    在Android里要实现树形菜单,都是用ExpandableList(也有高手自己继承ListView或者LinearLayout来做),但是ExpandableList一般只能实现2级树形菜单... ...

  8. dtree实现动态加载树形菜单,动态插入树形菜单

    1.导入  dtree文件    dtree.css   img文件夹   dtree.js 2. 建立对应 的数据库      1      父ID     name    id 3    建立连接 ...

  9. EasyUI创建异步树形菜单和动态添加标签页tab

    创建异步树形菜单 创建树形菜单的ul标签 <ul class="easyui-tree" id="treeMenu"> </ul> 写j ...

随机推荐

  1. poj 3660 Cow Contest (传递闭包)

    /* floyd 传递闭包 开始Floyd 之后统计每个点能到的或能到这个点的 也就是他能和几个人确定胜负关系 第一批要有n-1个 然后每次减掉上一批的人数 麻烦的很 复杂度上天了.... 正难则反 ...

  2. nyoj 42

    #include <iostream> #include <stdio.h> #include <cstring> #include <algorithm&g ...

  3. querydsl的好处

    http://www.querydsl.com/ 封装了很多访问不同数据层平台的方法,提供统一的通用框架(统一的书写格式,以一种通用的API方式来构建查询).便于抽成统一数据层,昨晚底层,以后其他模块 ...

  4. jQuery验证框架 .

          目录视图 摘要视图 订阅 “程序人生”中国软件开发者职业生涯调查     CSDN社区“三八节”特别活动      开发者职业生涯调查之未来 jQuery验证框架 分类: JQuery 2 ...

  5. GIt/Github常用命令

    1)git init:初始化本地仓库 2)创建文件:touch read.txt 3)当操作本地的文件时,使用常用的命令,如(mv,ls..)就可以操作,当操作暂存区的文件时需要在命令前家git,并且 ...

  6. Ubuntu11.10与r8168网卡不兼容导致网络时断时续的问题

    安装了ubuntu11.10之后,感觉上网啥的很不稳定,ssh连接内网机器也是一条命令卡半天,检查了各方面的原因,网络没有什么问题,最后才发现是网卡驱动的问题,网上搜了一下是由于linux(ubunt ...

  7. jquery插件:点击拉出的右侧滑动菜单

    就是一个停留在页面右侧的滑动菜单,点击可以拉出,带回调函数.宽高位置可以参数指定.插件代码如下: (jquery的路径请自己修改) (function($){ $.fn.sideSwitch = fu ...

  8. CentOS下几种软件安装方式

    1.rpmRPM RedHat Package Manager(RedHat软件包管理工具)的缩写,这一文件格式名称虽然打上了RedHat的标志, 但是其原始设计理念是开放式的,现在包括OpenLin ...

  9. 转:测试计划(出处:: 51Testing软件测试网--zfx081)

    测试计划阶段主要处于测试的先期准备阶段,在该阶段中主要是对将要进行的测试工作做一个整体的规划.包括一下内容:   1.测试目的和测试项目简介. 1.1测试目的:××××系统的测试计划有助于实现一下目标 ...

  10. linux中top命令详解

    linux的top命令里的cpu信息是什么意思呢? Cpu(s): 62.1% us, 15.9% sy,0.1% ni, 19.4% id,2.0% wa,0.1% hi,0.4% si Mem:  ...