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>
运行结果如下:
转载于:https://www.cnblogs.com/houjiie/p/6266272.html
java 定义mysql树形菜单相关推荐
- java中遍历树形菜单,你可能不知道还有这样的方法
版权声明:本文为CSDN博主「穆雄雄」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明. 原文链接:https://blog.csdn.net/qq_34137397/ ...
- java实现遍历树形菜单方法——实体类VoteTree
package org.entity;import java.util.ArrayList; import java.util.List;/*** * * 项目名称:testTree * 类名称:Vo ...
- java实现遍历树形菜单方法——设计思路【含源代码】
开发工具:MyEclipse 10 后台框架:Hibernate + Struts2 数据库:Oracle 11g 前台框架:EasyUi 浏览器:谷歌 在开发中我们经常会遇到左边是树形菜单,右边是一 ...
- java实现遍历树形菜单方法——数据库表的创建
这里主要是oracle数据库表的创建: --创建树形菜单表 create table vote_tree (id number(10) not null,text varchar2(30) not n ...
- JAVA定义一个树形的结构_Java自定义树形结构
Java自定义树形结构 Java自定义树形结构 为什么80%的码农都做不了架构师?>>> 树形结构 Java package com.zyl.comm; import java.ut ...
- java实现遍历树形菜单方法——Dao层
Dao层接口: /** * @Title: IVoteTreeDao.java * @Package org.dao * @Description: TODO该方法的主要作用: * @author A ...
- java实现遍历树形菜单方法——service层
Service接口: /** * @Title: IVoteTreeService.java * @Package org.Service * @Description: TODO该方法的主要作用: ...
- java实现遍历树形菜单方法——HibernateUtil实现
/** * @Title: HibernateUtil.java * @Package org.web * @Description: TODO该方法的主要作用: * @author A18ccms ...
- java实现遍历树形菜单方法——OpenSessionView实现
/** * @Title: OpenSessionView.java * @Package org.util * @Description: TODO该方法的主要作用: * @author A18cc ...
- java实现遍历树形菜单方法——TreeAction实现
/** * @Title: TreeAction.java * @Package org.web * @Description: TODO该方法的主要作用: * @author A18ccms A18 ...
最新文章
- P4597 序列sequence
- 如何让IDEA启动多个SpringBoot实例
- C++获取文件夹下所有文件名
- 1251: [蓝桥杯2015初赛]星系炸弹
- 两校合并!新部属大学,来了!
- 3种思路,快速打破制造业质量追溯困境,成为行业领先企业
- mysql fn_2019 数据库趋势报告,最受欢迎的是 MySQL
- 用计算机程序求n,计算机编程 算法 求n!.doc
- Python接口自动化之logging封装及实战
- 2012,2013年总结:在视音频技术道路上摸索
- C语言分支循环结构1,c语言——运算符、分支结构、循环结构(示例代码)
- JAVA minaio模型_Mina的线程模型
- Android应用文本字体设置
- 用计算机显示器主屏区域造句,显示器造句
- PLM设备维修系统技术
- 美食杰-菜谱大全(二)
- ajax传递数组,后台接收
- PB数据窗口9种风格
- 卡巴斯基KAV/KIS 6.0.1.411正式版下载 附MP1版中文汉化+注册码
- 遇到的奇葩问题和解决方法