spring+srpingmvc+hibernate實現(xiàn)動態(tài)ztree生成樹狀圖效果
ztree生成樹狀圖
前臺
導(dǎo)入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
}
};
//數(shù)據(jù)集
var zNodes =[
//根據(jù)這種格式生成樹狀圖
// {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:"海淀區(qū)"},
// {id:23, pId:2, name:"朝陽區(qū)"},
// {id:24, pId:2, name:"長安區(qū)"}
];
//點擊后的操作
function beforeClick(treeId, treeNode, clickFlag) {
//獲取父窗口中id為Text1
var parentControl=parent.document.getElementById("Text1");
//把值設(shè)置為treeNode.name;
parentControl.value=treeNode.name;
}
//訪問控制層,獲取數(shù)據(jù)。
$(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的實現(xiàn)類
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();
}
}
業(yè)務(wù)接口
package com.shp.dev.menu.service;
import com.shp.dev.menu.pojo.Menu;
import java.util.List;
public interface MenuService {
List<Menu> queryAll();
}
業(yè)務(wù)實現(xiàn)層
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)//只讀事務(wù)
// @Transactional(rollbackFor = Exception.class)//啟動事務(wù),所有異常都回滾
@Autowired
private MenuDao menuDao;
@Override
@Transactional(readOnly = true)//只讀事務(wù)
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;
}
}
總結(jié)
以上所述是小編給大家介紹的spring+srpingmvc+hibernate實現(xiàn)動態(tài)ztree生成樹狀圖,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
java實現(xiàn)隨機森林RandomForest的示例代碼
本篇文章主要介紹了java實現(xiàn)隨機森林RandomForest的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
Spring Transaction事務(wù)實現(xiàn)流程源碼解析
此文就Spring 事務(wù)實現(xiàn)流程進(jìn)行源碼解析,我們可以借此對Spring框架更多一層理解,下面以xml形式創(chuàng)建一個事務(wù)進(jìn)行分析2022-09-09
Spring Cloud 2020.0.0正式發(fā)布再見了Netflix
這篇文章主要介紹了Spring Cloud 2020.0.0正式發(fā)布再見了Netflix,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
mybatis修改int型數(shù)據(jù)無法修改成0的解決
這篇文章主要介紹了mybatis修改int型數(shù)據(jù)無法修改成0的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
java開發(fā)中的誤區(qū)和細(xì)節(jié)整理
這篇文章給大家整理了關(guān)于JAVA開發(fā)中的細(xì)節(jié)以及經(jīng)常進(jìn)入的誤區(qū)整理,希望我們整理的內(nèi)容能夠給大家提供到幫助。2018-04-04
Java日常練習(xí)題,每天進(jìn)步一點點(15)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07

