spring+srpingmvc+hibernate实现动态ztree生成树状图效果
作者:No baldness 时间:2022-07-21 21:41:59
ztree生成树状图
ztree官网
前台
导入js和css包
下载地址
前端页面 ztree.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" isELIgnored="false" %>
<!DOCTYPE html>
<HTML>
<HEAD>
<TITLE>ztree</TITLE>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="${pageContext.request.contextPath}/Css/demo.css" type="text/css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/Css/zTreeStyle/zTreeStyle.css" type="text/css">
<script type="text/javascript" src="${pageContext.request.contextPath}/Js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/Js/jquery.ztree.core-3.5.js"></script>
<SCRIPT type="text/javascript">
//初始化
var setting = {
data: {
simpleData: {
enable: true
}
},
callback: {
beforeClick: beforeClick
}
};
//数据集
var zNodes =[
//根据这种格式生成树状图
// {id:1, pId:0, name:"河北省"},
// {id:12, pId:1, name:"石家庄"},
// {id:13, pId:1, name:"邢台"},
// {id:14, pId:1, name:"邯郸"},
// {id:2, pId:0, name:"北京市"},
// {id:22, pId:2, name:"海淀区"},
// {id:23, pId:2, name:"朝阳区"},
// {id:24, pId:2, name:"长安区"}
];
//点击后的操作
function beforeClick(treeId, treeNode, clickFlag) {
//获取父窗口中id为Text1
var parentControl=parent.document.getElementById("Text1");
//把值设置为treeNode.name;
parentControl.value=treeNode.name;
}
//访问控制层,获取数据。
$(document).ready(function(){
var url="${pageContext.request.contextPath}/menu/findZtree";
$.getJSON(url,{},function(nodes){
//alert(nodes);
console.log(JSON.stringify(nodes));
zNodes=nodes;
$.fn.zTree.init($("#treeDemo"), setting, zNodes);
})
});
</SCRIPT>
</HEAD>
<BODY>
<div class="content_wrap">
<div class="zTreeDemoBackground left">
<ul id="treeDemo" class="ztree"></ul>
</div>
</div>
</BODY>
</HTML>
后台
ztree所需实体类
package com.shp.dev.common;
public class Ztree {
private String id;
private String pId;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getpId() {
return pId;
}
public void setpId(String pId) {
this.pId = pId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Ztree() {
}
public Ztree(String id, String pId, String name) {
this.id = id;
this.pId = pId;
this.name = name;
}
@Override
public String toString() {
return "Ztree{" +
"id='" + id + '\'' +
", pId='" + pId + '\'' +
", name='" + name + '\'' +
'}';
}
}
dao接口
package com.shp.dev.menu.dao;
import com.shp.dev.menu.pojo.Menu;
import java.util.List;
public interface MenuDao {
List<Menu> queryAll();
}
dao的实现类
package com.shp.dev.menu.dao;
import com.shp.dev.menu.pojo.Menu;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository("menuDao")
public class MenuDaoImpl implements MenuDao{
@Autowired
private SessionFactory sessionFactory;
@Override
public List<Menu> queryAll() {
return sessionFactory.getCurrentSession().createQuery("from Menu").list();
}
}
业务接口
package com.shp.dev.menu.service;
import com.shp.dev.menu.pojo.Menu;
import java.util.List;
public interface MenuService {
List<Menu> queryAll();
}
业务实现层
package com.shp.dev.menu.service;
import com.shp.dev.menu.dao.MenuDao;
import com.shp.dev.menu.pojo.Menu;
import com.shp.dev.role.dao.RoleDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service("menuService")
public class MenuServiceImpl implements MenuService{
//@Transactional(readOnly = true)//只读事务
// @Transactional(rollbackFor = Exception.class)//启动事务,所有异常都回滚
@Autowired
private MenuDao menuDao;
@Override
@Transactional(readOnly = true)//只读事务
public List<Menu> queryAll() {
return menuDao.queryAll();
}
}
控制层
package com.shp.dev.menu.web;
import com.shp.dev.common.Ztree;
import com.shp.dev.menu.pojo.Menu;
import com.shp.dev.menu.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping("/menu")
public class MenuController {
@Autowired
private MenuService menuService;
@RequestMapping("/findZtree")
@ResponseBody // 返回json对象
public List<Ztree> findZtree(){
List<Ztree> menus=new ArrayList<Ztree>();
List<Menu> query = menuService.queryAll();
for (Menu m : query) {
menus.add(new Ztree(m.getId(),m.getParent_id(),m.getName()));
}
return menus;
}
}
总结
以上所述是小编给大家介绍的spring+srpingmvc+hibernate实现动态ztree生成树状图网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
来源:https://blog.csdn.net/Baldprogrammer/article/details/102984459
标签:spring,srpingmvc,hibernate,ztree,树状图
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot路径映射实现过程图解
2023-11-13 04:01:11
![](https://img.aspxhome.com/file/2023/6/59376_0s.png)
SpringBoot整合Swagger的方法示例
2021-05-29 02:42:16
Java读写txt文件时防止中文乱码问题出现的方法介绍
2023-06-23 04:40:32
Java使用Math.random()结合蒙特卡洛方法计算pi值示例
2023-05-10 10:43:13
![](https://img.aspxhome.com/file/2023/6/60606_0s.jpg)
JPA Specification常用查询+排序实例
2023-11-23 04:56:32
![](https://img.aspxhome.com/file/2023/3/59423_0s.png)
maven如何打包动态环境变量(包括启动脚本)
2023-07-16 03:48:56
Docker搭建前端Java的开发环境详解
2023-05-21 04:23:25
C# WinForm导出Excel方法介绍
2022-12-01 13:25:57
IDEA远程管理docker镜像及容器服务的实现
2022-01-07 16:26:48
![](https://img.aspxhome.com/file/2023/9/63439_0s.png)
分享15款Java程序员必备的开发工具
2021-12-07 19:09:07
![](https://img.aspxhome.com/file/2023/3/69343_0s.jpg)
深入解析Java的Spring框架中bean的依赖注入
2023-12-20 18:50:52
![](https://img.aspxhome.com/file/2023/8/66188_0s.png)
java实现单词搜索迷宫游戏
2023-11-10 22:44:32
基于Spring Mvc实现的Excel文件上传下载示例
2022-01-22 02:02:56
Java.try catch finally 的执行顺序说明
2022-07-06 00:30:25
![](https://img.aspxhome.com/file/2023/2/63312_0s.jpg)
Java基础之反射详解
2022-06-16 12:25:11
![](https://img.aspxhome.com/file/2023/5/61955_0s.png)
java常用工具类 Random随机数、MD5加密工具类
2023-02-14 17:55:08
java获取json中的全部键值对实例
2023-08-23 19:53:03
Java 如何实现一个http服务器
2022-03-27 05:40:04
![](https://img.aspxhome.com/file/2023/2/61292_0s.png)
Java开发人员需知的十大戒律
2023-09-17 07:33:50
Java使用Jdbc连接Oracle执行简单查询操作示例
2021-07-25 03:34:25
![](https://img.aspxhome.com/file/2023/8/63408_0s.jpg)