EasyUI——tree组件(前端)

Tree组件(前端)

  • 使用代码
    • MenuDao层优化
    • Action控制器
    • index.js文件
  • 工具类、xml
    • ResponseUtil
    • mvc.xml
  • jsp添加代码及运行显示
    • index.jsp页面添加代码
    • 运行index.jsp

使用代码

记得以上篇后端为基础

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转换成List>
		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

EasyUI——tree组件(前端)_第1张图片

你可能感兴趣的:(EasyUI——tree组件(前端))