数据库中的数据如下:

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. karma、jasmine做angularjs单元测试

    引用文:karma.jasmine做angularjs单元测试 karma和jasmine介绍 <1>技术介绍 karma karma是Testacular的新名字 karma是用来自动化 ...

  2. ServletContextListener 解析用法

    ServletContext 被 Servlet 程序用来与 Web 容器通信.例如写日志,转发请求.每一个 Web 应用程序含有一个Context,被Web应用内的各个程序共享.因为Context可 ...

  3. centos redis安装

    对redis不是很了解,先写一个简单的安装过程 系统版本:centos 6.5 redis版本:2.8.23 一.安装依赖 sudo yum install tcl gcc gcc-c++ -y 二. ...

  4. mongodb入门教程二

    title: mongodb入门教程二 date: 2016-04-07 10:33:02 tags: --- 上一篇文章说了mongodb最基本的东西,这边博文就在深入一点,说一下mongo的一些高 ...

  5. mui实现支付宝支付功能

    <!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>H ...

  6. axure RP Pro7.0加载日历控件的步骤

  7. IIS应用程序池数目

    今天突然遇到一个问题,访问线上站点,数据处理过之后,访问页面有时是404,有时不是404,而404页面刷新几次之后就正常了. 经过大神们指点之后,发现竟然是应用程序池惹的祸. 分析下原因,在代码里面数 ...

  8. MySQL教程:数据库具体操作

    1. 连接数据库服务器 $ ./mysql -h host_name -u user_name -p -h host_name(--host=host_name),连接的数据库主机名,如果在本地主机上 ...

  9. Apache 2.x+jboss6.1反向代理session共享问题设置

    2016年8月4日,第一次开笔写博客园,今天在公司解决了一个问题. apache+jboss做负载均衡的问题一直困扰了很久.问题描述如下,使用apche做反向代理转发给3台jboss 的app,app ...

  10. 【转】setStyleSheet用法

    [转自]http://blog.csdn.net/seanyxie/article/details/5925723 使用setStyleSheet来设置图形界面的外观: QT Style Sheets ...