EasyUI02.Tree

回顾: EasyUI01.搭建简易管理员页面

sql语句 

CREATE TABLE bs_permission
(
    id   number          DEFAULT NULL,
    pid  number          DEFAULT NULL,
    text varchar2(10) DEFAULT NULL,
    url  varchar2(255) DEFAULT NULL
);
 
create sequence seq_permission;
 
insert into bs_permission values(seq_permission.nextval,0,'会员管理','');
insert into bs_permission values(seq_permission.nextval,0,'商品管理','');
insert into bs_permission values(seq_permission.nextval,0,'订单管理','');
insert into bs_permission values(seq_permission.nextval,0,'数据报表','');
insert into bs_permission values(seq_permission.nextval,0,'系统管理','');
 
insert into bs_permission values(seq_permission.nextval,2,'会员卡充值','manager/user/recharge.jsp');
insert into bs_permission values(seq_permission.nextval,2,'会员等级','manager/user/level.jsp');
insert into bs_permission values(seq_permission.nextval,3,'状态管理','manager/goods/state.jsp');
insert into bs_permission values(seq_permission.nextval,3,'类目管理','manager/user/topic.jsp');
insert into bs_permission values(seq_permission.nextval,3,'评论管理','manager/user/comment.jsp');
--当在数据库新插入一条数据时,界面上的子菜单项也会增加
insert into bs_permission values(seq_permission.nextval,4,'用户订单','manager/order/order.jsp');
 
commit;

index.jsp 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
    <title>Title</title>
    <script src="jquery-easyui-1.5.5.2/jquery.min.js"></script>
    <script src="jquery-easyui-1.5.5.2/jquery.easyui.min.js"></script>
    <link rel="stylesheet" href="jquery-easyui-1.5.5.2/themes/default/easyui.css">
</head>
<body class="easyui-layout">
<div data-options="region:'north',title:'网站导航栏',collapsible:false" style="height:100px;"></div>
<div data-options="region:'west',title:'菜单',split:true" style="width:20%;">
    <ul id="asideMenu"></ul>
</div>
<div data-options="region:'center',collapsible:false,border:false" style="padding:5px;background:#eee;">
    <div id="mainTab" class="easyui-tabs" style="width:100%;height:100%;"></div>
</div>
<script>
    let mainTab = $('#mainTab')
 
    $('#asideMenu').tree({
        url: '${pageContext.request.contextPath}/permission.do', //远程数据的地址
        method: "POST",//访问方式
        lines: true, //显示虚线
        onClick(node) {
            //判断是否是父节点
            if (!node.attributes['pid']) {
                return
            }
            //判断是不是已经打开了
            if(mainTab.tabs('exists',node.text)){
                mainTab.tabs('select',node.text)
                return
            }
            mainTab.tabs('add', {
                id:node.id,
                title: node.text,
                content:"<iframe src='"+node.attributes['url']+"'></iframe>"
            });
        }
    });
</script>
</body>
</html>

DBHelper.java

package util;
import java.sql.*;
public class DBHelper {
    //定义链接字符串
    private static final String URL = "jdbc:oracle:thin:@localhost:1521:orcl";
    //加载驱动
    static {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    //获得链接
    public static Connection getCon() {
        try {
            return DriverManager.getConnection(URL, "scott", "123");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    //关闭资源
    public static void close(Connection con, PreparedStatement ps, ResultSet rs) {
        try {
            if (con != null && !con.isClosed()) {
                con.close();
            }
            if (ps != null) {
                ps.close();
            }
            if (rs != null) {
                rs.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

TreeFactroy.java

package util;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class TreeFactory {
    public static List<TreeNode> buildList(List<TreeNode> nodeList) {
        List<TreeNode> list = buildList(nodeList, "0");
        TreeNode root;
        if (!list.isEmpty()) {
            return list;
        } else {
            root = new TreeNode();
            root.setId("000");
            root.setParentId("-1");
            root.setHasParent(false);
            root.setHasChildren(true);
            root.setChecked(true);
            root.setChildren(list);
            root.setText("主菜单");
            root.setState("closed");
        }
        return Arrays.asList(root);
    }
    public static List<TreeNode> buildList(List<TreeNode> nodeList, String topMenuId) {
        return Optional.ofNullable(nodeList).map(list -> {
            list.forEach(item -> {
                list.stream().filter(i -> {
                    return i.getId().equals(item.getParentId());
                }).forEach(i -> {
                    i.setHasChildren(Boolean.TRUE);
                    i.getChildren().add(item);
                });
            });
            return list.stream().filter(i -> {
                return i.getParentId().equals(topMenuId);
            }).collect(Collectors.toList());
        }).get();
    }
}

TreeNode.java 

package util;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class TreeNode {
	    private String id;
	    private String text;
	    private String state;
	    private Boolean checked = false;
	    private Map<String, Object> attributes=new HashMap<>();
	    private List<TreeNode> children = new ArrayList<>();
	    private String parentId;
	    private Boolean hasParent = false;
	    private Boolean hasChildren = false;   
	    public TreeNode() {
			// TODO Auto-generated constructor stub
		}
		public TreeNode(String id, String text, String state, Boolean checked, Map<String, Object> attributes,
				List<TreeNode> children, String parentId, Boolean hasParent, Boolean hasChildren) {
			super();
			this.id = id;
			this.text = text;
			this.state = state;
			this.checked = checked;
			this.attributes = attributes;
			this.children = children;
			this.parentId = parentId;
			this.hasParent = hasParent;
			this.hasChildren = hasChildren;
		}
		@Override
		public String toString() {
			return "TreeNode [id=" + id + ", text=" + text + ", state=" + state + ", checked=" + checked + ", parentId="
					+ parentId + ", hasParent=" + hasParent + ", hasChildren=" + hasChildren + "]";
		}
		public String getId() {
			return id;
		}
		public void setId(String id) {
			this.id = id;
		}
		public String getText() {
			return text;
		}
		public void setText(String text) {
			this.text = text;
		}
		public String getState() {
			return state;
		}
		public void setState(String state) {
			this.state = state;
		}
		public Boolean getChecked() {
			return checked;
		}
		public void setChecked(Boolean checked) {
			this.checked = checked;
		}
		public Map<String, Object> getAttributes() {
			return attributes;
		}
		public void setAttributes(Map<String, Object> attributes) {
			this.attributes = attributes;
		}
		public List<TreeNode> getChildren() {
			return children;
		}
		public void setChildren(List<TreeNode> children) {
			this.children = children;
		}
		public String getParentId() {
			return parentId;
		}
		public void setParentId(String parentId) {
			this.parentId = parentId;
		}
		public Boolean getHasParent() {
			return hasParent;
		}
		public void setHasParent(Boolean hasParent) {
			this.hasParent = hasParent;
		}
		public Boolean getHasChildren() {
			return hasChildren;
		}
		public void setHasChildren(Boolean hasChildren) {
			this.hasChildren = hasChildren;
		}    
}

PermissionServlet.java 

package servlet;
 
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
 
import dao.PermissionDao;
@WebServlet("/permission.do")
public class PermissionServlet extends HttpServlet{
	private PermissionDao permissionDao=new PermissionDao();
    private ObjectMapper mapper = new ObjectMapper();
 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	doPost(req, resp);
    }
 
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询所有的菜单,将菜单写出去
        mapper.writeValue(resp.getOutputStream(),permissionDao.listNodes());
    }
}

Permission.java

package pojo;
public class Permission {
	    private Integer id;
	    private Integer pid;
	    private String text;
	    private String url;
	    
	    public Permission() {
			// TODO Auto-generated constructor stub
		}
		public Permission(Integer id, Integer pid, String text, String url) {
			super();
			this.id = id;
			this.pid = pid;
			this.text = text;
			this.url = url;
		}
		@Override
		public String toString() {
			return "Permission [id=" + id + ", pid=" + pid + ", text=" + text + ", url=" + url + "]";
		}
		public Integer getId() {
			return id;
		}
		public void setId(Integer id) {
			this.id = id;
		}
		public Integer getPid() {
			return pid;
		}
		public void setPid(Integer pid) {
			this.pid = pid;
		}
		public String getText() {
			return text;
		}
		public void setText(String text) {
			this.text = text;
		}
		public String getUrl() {
			return url;
		}
		public void setUrl(String url) {
			this.url = url;
		}	    
}

PermissionDao.java 

package dao;
 
import pojo.Permission;
import util.DBHelper;
import util.TreeFactory;
import util.TreeNode;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
 
public class PermissionDao {
 
    private Connection con;
    private PreparedStatement ps;
    private ResultSet rs;
 
    public List<Permission> list() {
        List<Permission> list = new ArrayList<Permission>();
        try {
            con = DBHelper.getCon();
            ps = con.prepareStatement("select * from bs_permission");
            rs = ps.executeQuery();
            while (rs.next()) {
                Permission permission = new Permission();
                permission.setId(rs.getInt(1));
                permission.setPid(rs.getInt(2));
                permission.setText(rs.getString(3));
                permission.setUrl(rs.getString(4));
                list.add(permission);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBHelper.close(con, ps, rs);
        }
        return list;
    }
 
    public List<TreeNode> listNodes() {
        List<TreeNode> nodes = new ArrayList<TreeNode>();
        //数据库中
        List<Permission> list = list();
        //将permission封装成node
        for (Permission permission : list) {
            TreeNode node = new TreeNode();
            node.setId(String.valueOf(permission.getId()));
            node.setParentId(String.valueOf(permission.getPid()));
            node.setText(permission.getText());
            //把pid放到自定义属性中
            node.getAttributes().put("pid",permission.getPid());
            //把url放到自定义属性中
            node.getAttributes().put("url",permission.getUrl());
            nodes.add(node);
        }
        return TreeFactory.buildList(nodes, "0");
    }
}

state.jsp 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>我是第3个内容</h1>
</body>
</html>

你要尽全力保护你的梦想。那些嘲笑你梦想的人,他们注定失败,他们想把你变成和他们一样。我坚信,只要心中有梦想,我就会与众不同。你也是。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值