tree前端实现

文章目录

    • 运行结果
    • 代码

运行结果

tree前端实现_第1张图片

代码

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 {

	/**
	 * 直接重数据库获取数据
	 * @param ermission
	 * @param pageBean
	 * @return
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public List 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> topNode(Permission ermission,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException {
		List list = this.list(ermission, pageBean);
		
		//通过工具类完成指定格式的输出
		List> nodes=new ArrayList>();
		//Permission的格式是不满足easyui的tree组件的展现的数据格式的
		//目的:将List转换成List>
		//实现:将List得到的单个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 attributes=new HashMap();
			attributes.put("self", p);
			treeVo.setAttributes(attributes);
			nodes.add(treeVo);
			
			
		}
		return BuildTree.buildList(nodes,"0");
		}
	}

web:

public class PermissionAction extends ActionSupport implements ModelDriven {

	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 topNode =this.permissionDao.topNode(null, null);
//				List> list=new ArrayList>();
//				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"%>





   

   
  

   
   

登录后的主界面

 
 

	
xxx管理系统
    east region
    版权专属

    你可能感兴趣的:(easyui)