EasyUI——tree组件(前端)

使用代码

记得以上篇后端为基础

MenuDao层优化

package com.rong.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.rong.entity.Menu;
import com.rong.util.BaseDao;
import com.rong.util.BuildTree;
import com.rong.util.PageBean;
import com.rong.vo.TreeVo;

public class MenuDao extends BaseDao<Menu>{
	
	public List<Menu> list(Menu menu,PageBean pagebean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql="select * from t_easyui_menu";
		return super.executeQuery(sql, Menu.class, pagebean);
	}
	
	public List<TreeVo<Menu>> topNode(Menu menu,PageBean pagebean) throws InstantiationException, IllegalAccessException, SQLException{
		List<Menu> list=this.list(menu, pagebean);
		//通过工具类完成指定格式输出
		List<TreeVo<Menu>> nodes=new ArrayList<TreeVo<Menu>>();
		//将List<T>转换成List<TreeVo<T>>
		TreeVo treeVo=null;
		for (Menu p: list) {
			treeVo=new TreeVo<>();
			treeVo.setId(p.getMenuid()+"");
			treeVo.setText(p.getMenuname());
			treeVo.setParentId(p.getParentid()+"");
			Map<String, Object> attributes=new HashMap<String, Object>();
			treeVo.setAttributes(attributes);
			nodes.add(treeVo);
		}
		return BuildTree.buildList(nodes,"-1");
	}
	
}

Action控制器

MenuAction

package com.rong.web;

import java.io.IOException;
import java.sql.SQLException;

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

import com.rong.dao.MenuDao;
import com.rong.entity.Menu;
import com.rong.framework.ActionSupport;
import com.rong.framework.ModelDriven;
import com.rong.util.ResponseUtil;
import com.rong.vo.TreeVo;

public class MenuAction extends ActionSupport implements ModelDriven<Menu> {

	private Menu menu=new Menu();
	private MenuDao menudao=new MenuDao();
	@Override
	public Menu getModel() {
		// TODO Auto-generated method stub
		return menu;
	}
	
	public String menuTree(HttpServletRequest req,HttpServletResponse resp) throws IOException {
		try {
			ResponseUtil.weiteJson(resp, this.menudao.topNode(null, null));
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

}

index.js文件

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

工具类、xml

ResponseUtil

package com.rong.util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;

public class ResponseUtil {

	public static void weite(HttpServletResponse resp,Object o) throws IOException {
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter out=resp.getWriter();
		out.print(o.toString());
		out.flush();
		out.close();
	}
	
	public static void weiteJson(HttpServletResponse resp,Object o) throws IOException {
		resp.setContentType("text/html;charset=utf-8");
		ObjectMapper om=new ObjectMapper();
		String jsonstr=om.writeValueAsString(o);
		PrintWriter out=resp.getWriter();
		out.print(jsonstr.toString());
		out.flush();
		out.close();
	}
}

mvc.xml

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<config>
	<action path="/permission" type="com.rong.web.PermissionAction">
		<!-- <forward name="index" path="/index.jsp" redirect="false" /> -->
	</action>
</config>

jsp添加代码及运行显示

index.jsp页面添加代码

在这里插入图片描述

运行index.jsp

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值