tree前端实现

文章目录

运行结果

在这里插入图片描述

代码

dao:

package com.jiangjiayan.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jiangjiayan.entity.Permission;
import com.jiangjiayan.util.BaseDao;
import com.jiangjiayan.util.BuildTree;
import com.jiangjiayan.util.PageBean;
import com.jiangjiayan.vo.TreeVo;

public class PermissionDao extends BaseDao<Permission> {

	/**
	 * 直接重数据库获取数据
	 * @param ermission
	 * @param pageBean
	 * @return
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public List<Permission> list(Permission permission,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException {
			String sql="select * from t_easyui_permission";
			return super.executeQuery(sql, Permission.class, pageBean);
			
	}
	
	/**
	 * 能够将数据库中的数据,体现父子结构
	 * @param ermission
	 * @param pageBean
	 * @return
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public List<TreeVo<Permission>> topNode(Permission ermission,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException {
		List<Permission> list = this.list(ermission, pageBean);
		
		//通过工具类完成指定格式的输出
		List<TreeVo<Permission>> nodes=new ArrayList<TreeVo<Permission>>();
		//Permission的格式是不满足easyui的tree组件的展现的数据格式的
		//目的:将List<Permission>转换成List<TreeVo<T>>
		//实现:将List<Permission>得到的单个Permission转成TreeeVo,将TreeeVo加入到nodes
		
		TreeVo treeVo=null;
		for (Permission p : list) {
			treeVo=new TreeVo<>();
			treeVo.setId(p.getId()+" ");
			treeVo.setText(p.getName());
			treeVo.setParentId(p.getPid()+"");
			Map<String, Object> attributes=new HashMap<String, Object>();
			attributes.put("self", p);
			treeVo.setAttributes(attributes);
			nodes.add(treeVo);
			
			
		}
		return BuildTree.buildList(nodes,"0");
		}
	}

web:

public class PermissionAction extends ActionSupport implements ModelDriven<Permission> {

	private Permission permission =new Permission();
	private PermissionDao permissionDao =new PermissionDao();


	@Override
	public Permission getModel() {
		// TODO Auto-generated method stub
		return permission;
	}

	
	public String menuTree(HttpServletRequest req,HttpServletResponse resp) {
	
			try {
//				TreeVo<Permission> topNode =this.permissionDao.topNode(null, null);
//				List<TreeVo<Permission>> list=new ArrayList<TreeVo<Permission>>();
//				list.add(topNode);
//				ResponseUtil.writeJson(resp,list);
//				
				
				ResponseUtil.writeJson(resp, this.permissionDao.topNode(null, null));
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
	
		//结果码的配置就是为了。在mvc.xml中寻找到底是重定向还是转发
		return null;
		
	}

	
}

工具类(ResponseUtil):

package com.jiangjiayan.util;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;

public class ResponseUtil {

	public static void write(HttpServletResponse response,Object o)throws Exception{
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		out.println(o.toString());
		out.flush();
		out.close();
	}
	
	public static void writeJson(HttpServletResponse response,Object o)throws Exception{
		response.setContentType("text/html;charset=utf-8");
		ObjectMapper om = new ObjectMapper();
		String jsonstr = om.writeValueAsString(o);
		PrintWriter out=response.getWriter();
//		System.out.println(jsonstr.toString());
		out.println(jsonstr.toString());
		out.flush();
		out.close();
	}
}

js:

$(function(){
	
	$('#tt').tree({    
	    url: $("#ctx").val()+'/permission.action?methodName=menuTree',
	});
})

界面(index):

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- 全局样式 -->
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/themes/default/easyui.css">   
<!-- 定义图标的样式 -->
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/themes/icon.css">   
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/jquery.min.js"></script>  
<!--组件库源文件的js文件  -->
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/jquery.easyui.min.js"></script>   
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/index.js"></script>   

<title>登录后的主界面</title>

</head> 
<input type="hidden" id="ctx" value="${pageContext.request.contextPath }"> 
<body class="easyui-layout">
	<div data-options="region:'north',border:false" style="height:60px;background:#B3DFDA;padding:10px">xxx管理系统</div>
	<div data-options="region:'west',split:true,title:'West'" style="width:150px;padding:10px;">
		<ul id="tt"></ul>  
	</div>
	<div data-options="region:'east',split:true,collapsed:true,title:'East'" style="width:100px;padding:10px;">east region</div>
	<div data-options="region:'south',border:false" style="height:50px;background:#A9FACD;padding:10px;">版权专属</div>
	<div data-options="region:'center',title:'Center'"></div>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值