将数据库中的数据绑定到页面上去
数据库:PL/SQL 数据库中的代码
create table tb_module (
id number not null,
pid number not null,
text varchar2(150) not null,
iconCls varchar2(100) not null,
url varchar2(100) null,
sort number not null,
primary key(id)
)
insert into TB_MODULE (id, pid, text, iconcls, url, sort)
values (20, -1, '订单管理', 'icon-cart', null, 2);
insert into TB_MODULE (id, pid, text, iconcls, url, sort)
values (2001, 20, '订单管理', 'icon-cart-edit', '/orderList.jsp', 6);
insert into TB_MODULE (id, pid, text, iconcls, url, sort)
values (2002, 20, '订单统计', 'icon-organization', '/orderStatistics.jsp', 7);
insert into TB_MODULE (id, pid, text, iconcls, url, sort)
values (21, -1, '系统管理', 'icon-cog', null, 3);
insert into TB_MODULE (id, pid, text, iconcls, url, sort)
values (2101, 21, '用户管理', 'icon-user-edit', 'jsp/system/userManage.jsp', 8);
insert into TB_MODULE (id, pid, text, iconcls, url, sort)
values (2102, 21, '权限管理', 'icon-heart', 'jsp/system/authManage.jsp', 10);
insert into TB_MODULE (id, pid, text, iconcls, url, sort)
values (2103, 21, '字典管理', 'icon-page-edit', '/dictList.jsp', 11);
insert into TB_MODULE (id, pid, text, iconcls, url, sort)
values (22, -1, '书本管理', 'icon-book-open', null, 1);
insert into TB_MODULE (id, pid, text, iconcls, url, sort)
values (2201, 22, '新增书本', 'icon-book-add', 'jsp/book/addBook.jsp', 5);
insert into TB_MODULE (id, pid, text, iconcls, url, sort)
values (2202, 22, '书本管理', 'icon-book-edit', 'jsp/book/bookList.jsp', 4);
commit;
使用三层架构和MVC模式
com.zking.dao
public interface IModuleDao {
/**
* 根据父级ID找到其对应的子节点的集合
* @param pid 父级ID
* @return 子节点的集合
*/
public List<Module> getAllByPid(int pid);
}
public class ModuleDao implements IModuleDao{
private Connection con = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
@Override
public List<Module> getAllByPid(int pid) {
List<Module> ls = new ArrayList<Module>();
try {
//获得连接
con=DBHelper.getCon();
//定义SQL语句
String sql = "select * from tb_module where pid=? order by sort";
//获得执行对象
ps=con.prepareStatement(sql);
//给占位符赋值
ps.setInt(1, pid);
//获得结果集
rs=ps.executeQuery();
//遍历结果集
while(rs.next()) {
//实例化
Module md = new Module();
md.setId(rs.getInt(1));
md.setPid(rs.getInt(2));
md.setText(rs.getString(3));
md.setIconCls(rs.getString(4));
md.setUrl(rs.getString(5));
md.setSort(rs.getInt(6));
//加进去
ls.add(md);
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
DBHelper.myClo(con, ps, rs);
}
return ls;
}
com.zking.biz
public interface IModuleBiz {
/**
* 根据父级ID找到其对应的子节点的集合
* @param pid 父级ID
* @return 子节点的集合
*/
public List<Module> getAllByPid(int pid);
}
/**
* 业务逻辑层
*/
public class ModuleBiz implements IModuleBiz{
//调用数据库访问层
IModuleDao imd = new ModuleDao();
@Override
public List<Module> getAllByPid(int pid) {
List<Module> ls = imd.getAllByPid(pid);
//遍历集合
for (Module m : ls) {
if(m.getPid()==-1) {//说明本身是父节点 理应有字节点
//递归
m.setChildren(getAllByPid(m.getId()));
}
}
return ls;
}
com.zking.entity
public class Module implements Serializable{
private static final long serialVersionUID = -677083529681328678L;
private int id;
private int pid;
private String text;
private String url;
private String iconCls;
private int sort;
private List<Module> children = new ArrayList<Module>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPid() {
return pid;
}
public void setPid(int 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;
}
public String getIconCls() {
return iconCls;
}
public void setIconCls(String iconCls) {
this.iconCls = iconCls;
}
public int getSort() {
return sort;
}
public void setSort(int sort) {
this.sort = sort;
}
public List<Module> getChildren() {
return children;
}
public void setChildren(List<Module> children) {
this.children = children;
}
public Module() {
// TODO Auto-generated constructor stub
}
public Module(int id, int pid, String text, String url, String iconCls, int sort, List<Module> children) {
this.id = id;
this.pid = pid;
this.text = text;
this.url = url;
this.iconCls = iconCls;
this.sort = sort;
this.children = children;
}
public Module( int pid, String text, String url, String iconCls, int sort, List<Module> children) {
this.pid = pid;
this.text = text;
this.url = url;
this.iconCls = iconCls;
this.sort = sort;
this.children = children;
}
@Override
public String toString() {
return "Module [id=" + id + ", pid=" + pid + ", text=" + text + ", url=" + url + ", iconCls=" + iconCls
+ ", sort=" + sort + ", children=" + children + "]";
}
com.zking.util
package com.zking.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import oracle.jdbc.driver.OracleDriver;
/**
* 数据库辅助类
* @author zjjt
*
*/
public class DBHelper {
//两个静态常量
private static final String URL = "jdbc:oracle:thin:@localhost:1521:orcl";
private static final String CNAME = "oracle.jdbc.driver.OracleDriver";
/**
* 加载驱动
*/
static {
try {
Class.forName(CNAME);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 创建连接
* @return 返回连接
*/
public static Connection getCon() {
Connection con = null;
try {
con = DriverManager.getConnection(URL,"scott","tiger");
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
/**
* 关闭资源
* @param con 连接
* @param ps 执行对象
* @param rs 结果集
*/
public static void myClo(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();
}
}
// public static void main(String[] args) {
// System.out.println(DBHelper.getCon());
// }
}
com.zking.servlet
package com.zking.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
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.alibaba.fastjson.JSON;
import com.zking.biz.IModuleBiz;
import com.zking.biz.ModuleBiz;
import com.zking.entity.Module;
@WebServlet("/moduleServlet")
public class ModuleServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码方式
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=UTF-8");
//获得out
PrintWriter out = response.getWriter();
//servlet调用biz biz调用dao
IModuleBiz imb = new ModuleBiz();
//调用方法
List<Module> ls = imb.getAllByPid(-1);
//把集合转成JSON格式的字符串
String str = JSON.toJSONString(ls);
//把响应输送到客户端
out.write(str);;
out.flush();
out.close();
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- 引入公共页面 -->
<%@ include file="common/head.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
$(function(){
$('#myTree').tree({
url:ctx+'/moduleServlet' ,//请求地址 Ajax
animate:true ,//折叠或者展开的时候是否有动画效果
onDblClick: function(node){
//alert(node.text); // 在用户双击的时候提示
//alert(node.url);//路径
//拿到后代节点的集合
var cs = $('#myTree').tree('getChildren',node.target);
//alert(cs);
if(cs.length==0){//没有后代节点
var f = $('#myTab').tabs('exists',node.text);
//判断是否存在
if(!f){//说明不存在
//新打开一个选项卡(tab页)
$('#myTab').tabs('add',{
title:node.text,//标题
content:'<iframe frameborder="0px" width="99%" height="99%" src="'+node.url+'" ></iframe>', //内容
closable:true, //是否可关闭
/* tools:[{
iconCls:'icon-ok',
handler:function(){
alert('想干啥就干啥');
}
}]*/
iconCls:node.iconCls
});
}
else{//说明存在 让其对应选中
$('#myTab').tabs('select',node.text);
}
}
}
});
})
</script>
</head>
<body class="easyui-layout">
<div data-options="region:'north',split:true" style="height:85px;text-align:center;">
<h1>后台书籍管理</h1>
</div>
<div data-options="region:'south',split:true" style="height:60px;text-align:center;">
<b>©玉渊工作室所有,未经允许不可擅自使用</b>
</div>
<div data-options="region:'west',title:'功能导航',split:false" style="width:150px;">
<!--左侧 tree控件 -->
<ul id="myTree" class="easyui-tree">
</ul>
</div>
<div data-options="region:'center'" style="padding:5px;background:#fff;">
<!--中间的tab控件 -->
<div id="myTab" data-options="pill:true" class="easyui-tabs" style="width:100%;height:100%;">
<div data-options="iconCls:'icon-application-home'" title="首页" style="padding:20px;display:none;">
<img src="images/1.jpeg" width="100%" height="100%">
</div>
</div>
</div>
</body>
</html>
效果图: