java 定义mysql树形菜单
数据库中的数据如下:
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树形菜单的更多相关文章
- JAVA递归生成树形菜单
递归生成一个如图的菜单,编写两个类数据模型Menu.和创建树形的MenuTree.通过以下过程实现: 1.首先从菜单数据中获取所有根节点. 2.为根节点建立次级子树并拼接上. 3.递归为子节点建立次级 ...
- java树形菜单实现
java树形菜单实现 公司表: 部门表: 实体类: public class Node { private Integer companyId;//公司id private String compan ...
- java构建树形菜单递归工具类
1.设计菜单实体 import java.util.List; public class Menu { //菜单id private Long id; //父节点id private Long par ...
- java 传入list集合 返回树形菜单,for循环遍历
public List<SysPermissionVO> getTreeMenu(List<SysPermissionVO> list,SysPermissionVO sysP ...
- SSH框架CRUD+树形菜单案例
今天结合了案例来写ssh的增删改查 表设计 t_ssh_tree t_vue_user book 核心配置文件 struts-base.xml <?xml version="1.0 ...
- easyui实现树形菜单Tab功能、layout布局
一:常见三种前端ui框架 在初学者入门的状态下,我们常见的前端框架有三种且都有自己的官方网站: 1.easyui:官方网站(http://www.jeasyui.net/) 基于jquery的用户页面 ...
- Android 多级树形菜单
在Android里要实现树形菜单,都是用ExpandableList(也有高手自己继承ListView或者LinearLayout来做),但是ExpandableList一般只能实现2级树形菜单... ...
- dtree实现动态加载树形菜单,动态插入树形菜单
1.导入 dtree文件 dtree.css img文件夹 dtree.js 2. 建立对应 的数据库 1 父ID name id 3 建立连接 ...
- EasyUI创建异步树形菜单和动态添加标签页tab
创建异步树形菜单 创建树形菜单的ul标签 <ul class="easyui-tree" id="treeMenu"> </ul> 写j ...
随机推荐
- Android中完全退出当前应用系统
一.将统一管理Activity的类ActivityManager复制到工程里面. package com.jsmtr.www.Helper; import java.util.LinkedList; ...
- 排序算法之快速排序 JAVA快速排序算法
public static void quickSort(int[] arr, int low , int height){ int l=low, h = height; if(low < he ...
- Java-20个非常有用的程序片段
下面是20个非常有用的Java程序片段,希望能对你有用. 1.字符串有整型的相互转换 String a = String.valueOf(2); //integer to numeric string ...
- 原生js-拉勾网首页效果
拉勾网首页公司广告位的悬浮划过效果着实很吸引我.如下(不会做动图!--,感兴趣的可以去拉勾看看): 此处最吸引我的地方在于将鼠标划过上面一排公司列表时,感觉像是绿色的区块跟着你的鼠标移动一样,颇有动感 ...
- 搭建maven项目简介
http://jingyan.baidu.com/album/9f7e7ec0b714ae6f29155465.html?picindex=1 Maven学习 (一) 搭建Maven环境 http:/ ...
- mvc wcf 并发提示,存储Application,验证是否有用户在操作
System.Web.HttpContext httpContext = System.Web.HttpContext.Current; Hashtable departmentSalary = (H ...
- Jsp通过JDBC连接到SQL Server2008数据库遇到的几个问题
今天刚刚学jdbc ,之前通过main连到过Sql server 2008现在通过jsp连接sql server 2008,遇到了一些问题,跟大家分享一下,避免少走弯路 一.ClassNotFound ...
- 利用 Makefile 写的小程序
1.建立一个工程 2.写一个进度条的程序(原理就是在同一位置重复打印某一个字符(变化),达到动态显示的效果) 所以说我们这里只用回车'\r',覆盖这一行以前的输出,重新向缓冲区写数据刷新缓冲区,就能达 ...
- 学习开发jquery插件
先学习http://www.cnblogs.com/playerlife/archive/2012/05/11/2495269.html http://www.cnblogs.com/fromeart ...
- 测试RegExp对象的属性
//测试RegExp对象的属性function testRegExpProperty(){ var regexp = /abc/; //regexp.ignoreCase = true; //无效 c ...