如何利用小游戏开发框架提升企业小程序的用户体验与运营效率
402
2023-07-12
spring+srpingmvc+hibernate实现动态ztree生成树状图效果
ztree生成树状图
ztree官网
前台
导入js和css包
-
前端页面 ztree.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" isELIgnored="false" %>
//初始化
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);
})
});
后台
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;
IZjoOEAM 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
}
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
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
}
业务实现层
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
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
List
List
for (Menu m : query) {
menus.add(new Ztree(m.getId(),m.getParent_id(),m.getName()));
}
return menus;
}
}
总结
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~