EasyUi之Tree组件(二)

目录

  • 自定义mvc框架前端实现Tree结构
    • 总结

结合上一篇博客
Tree组件(一)

自定义mvc框架前端实现Tree结构

实现步骤
1、将结果集改为一个封装的节点

/**
	 * 封装节点的方法
	 * @param permission
	 * @param pageBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
public List> topNode(Permission permission,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
	List list = this.list(permission, pageBean);
	List> nodes=new ArrayList>();
	TreeVo treeVo=null;
	for (Permission p : list) {
		treeVo=new TreeVo<>();
		treeVo.setId(p.getId()+"");//设置节点id
		treeVo.setText(p.getName());//设置节点文本
		treeVo.setParentId(p.getPid()+"");//设置父节点
		nodes.add(treeVo);
	}
	return BuildTree.buildList(nodes, "0");
}

2、建立web层action处理返回值json的方法

package com.zhangsiwen.web;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhangsiwen.dao.PermissionDao;
import com.zhangsiwen.entity.Permission;
import com.zhangsiwen.framework.ActionSupport;
import com.zhangsiwen.framework.DispatcherServlet;
import com.zhangsiwen.framework.ModelDriven;
import com.zhangsiwen.util.ResponseUtil;
import com.zhangsiwen.vo.TreeVo;

public class PermissionAction extends ActionSupport implements ModelDriven {
	private Permission permission=new Permission();
	private PermissionDao permissionDao=new PermissionDao();
	@Override
	public Permission getModel() {
		return permission;
	}
	public String menuTree(HttpServletRequest req,HttpServletResponse resp) {
		try {		
			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();
		}
		return null;
	}
}

3、写一个xml进行解析



	
		
	

4、工具类 ResponseUtil用来处理字符编码的问题

package com.zhangsiwen.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();
		out.println(jsonstr.toString());
		out.flush();
		out.close();
	}
	
}

5、在js文件中渲染数据

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

运行结果
EasyUi之Tree组件(二)_第1张图片

总结

今天的分享到这了,觉得写得还行的小伙伴可以点一下赞!

你可能感兴趣的:(EasyUi之Tree组件(二))