Easyui入门
市面上的ui框架大部分是三类:
easyui:基于jquery+html4的,功能强大,但是界面没有其余两种好,并且不支持响应式。属于非常老的一款框架
bootstrap:基于jquery+html5的,功能也大部分完善,最重要的是页面最美观,但缺点是大部分插件等需要付费。
layui:功能也完善,页面也比easyui好看,但略逊bootstrap一筹,且有些功能是实现不了,优点是不需要付费且也支持响应式。
jQuery EasyUI是一组基于jQuery的UI插件集合体,而jQuery EasyUI的目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面。开发者不需要编写复杂的javascript,也不需要对css样式有深入的了解,开发者需要了解的只有一些简单的html标签。(百度百科)
我用的是jQuery1.7和easyui1.5的版本,我们使用一个框架首先要引入它的文件和jar包
导入待会需要用的jar包
文件准备好后,我们再把这两个js和两个css导入就能用了
注意:顺序不能乱,因为easyui是基于jQuery的,如果顺序乱了可能会出问题
<link rel="stylesheet" type="text/css" href="${pageContest.request.contextPath}/static/js/public/easyui5/themes/gray/easyui.css"> <!-- 默认背景色 -->
<link rel="stylesheet" type="text/css" href="${pageContest.request.contextPath}/static/js/public/easyui5/themes/icon.css"> <!-- 默认图标 -->
<script type="text/javascript" src="${pageContest.request.contextPath}/static/js/public/easyui5/jquery.min.js"></script> <!--jquery -->
<script type="text/javascript" src="${pageContest.request.contextPath}/static/js/public/easyui5/jquery.easyui.min.js"></script> <!-- easyui -->
</head>
案例:
1、通过layout布局**
代码如下
<body class="easyui-layout">
<div data-options="region:'north',border:false" style="height:60px;background:#B3DFDA;padding:10px">north region</div>
<div data-options="region:'west',split:true,title:'West'" style="width:150px;padding:10px;">west content</div>
<div data-options="region:'east',split:true,collapsed:true,title:'East'" style="width:100px;padding:10px;">east region</div>
<div data-options="region:'south',border:false" style="height:50px;background:#A9FACD;padding:10px;">south region</div>
<div data-options="region:'center',title:'Center'"></div>
</body>
效果如下
2、通过tree加载菜单
下面通过layout布局,获取数据库的数据展示到页面的树形菜单,然后通过菜单打开不通的tab页
首先我们需要准备一些连数据库和后面需要用到的一些工具类,如果对工具类不理解的话可以去看我之前mvc的 博客
树(tree)在网页中以树形结构显示分层数据。它向用户提供展开、折叠、拖拽、编辑和异步加载功能。
首先有一个jsno
[{
"id":1,
"text":"My Documents",
"children":[{
"id":11,
"text":"Photos",
"state":"closed",
"children":[{
"id":111,
"text":"Friend"
},{
"id":112,
"text":"Wife"
},{
"id":113,
"text":"Company"
}]
},{
"id":12,
"text":"Program Files",
"children":[{
"id":121,
"text":"Intel"
},{
"id":122,
"text":"Java",
"attributes":{
"p1":"Custom Attribute1",
"p2":"Custom Attribute2"
}
},{
"id":123,
"text":"Microsoft Office"
},{
"id":124,
"text":"Games",
"checked":true
}]
},{
"id":13,
"text":"index.html"
},{
"id":14,
"text":"about.html"
},{
"id":15,
"text":"welcome.html"
}]
}]
然后有一个js文件
$(function(){
$('#tt').tree({
url:'tree_data1.json'
});
})
此时的jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>后台主界面</title>
<!-- ctrl+shift+r -->
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/static/js/public/easyui5/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/static/js/public/easyui5/themes/icon.css">
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/js/public/easyui5/jquery.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/js/public/easyui5/jquery.easyui.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/js/index.js"></script>
</head>
<body class="easyui-layout">
<div data-options="region:'north',border:false"
style="height: 60px; background: #B3DFDA; padding: 10px">north
region</div>
<div data-options="region:'west',split:true,title:'West'"
style="width: 150px; padding: 10px;">
<ul id="tt"></ul>
</div>
<div
data-options="region:'east',split:true,collapsed:true,title:'East'"
style="width: 100px; padding: 10px;">east region</div>
<div data-options="region:'south',border:false"
style="height: 50px; background: #A9FACD; padding: 10px;">south
region</div>
<div data-options="region:'center',title:'Center'">
<div id="menuTab" class="easyui-tabs" style="">
<div title="首页" style="padding: 20px; display: none;">欢迎界面</div>
</div>
</div>
</body>
</html>
然后下面我们运行网页看看效果(下图所示)
下面尝试连接数据库,根据数据库来操作tree
首先创建一个util类
而该类里的资料有(下面的资料我前面几次的博客中有):
将下面的拷进WEB-INF中去:(之前的mvc博客里面都有哦,分别是分页tag、MVC.xml,自定义jsp标签,还有上面的util类的都有)
完成上面的步骤我们就可以开始写实体类:
package com.HC.entity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 作用是通过TreeNode类转换成
* tree_data1.json的字符串
* @author Administrator
*
*/
public class TreeNode {
private String id;
private String text;
private List<TreeNode> children = new ArrayList<>();
private Map<String, Object> attributes = new HashMap<>();
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 List<TreeNode> getChildren() {
return children;
}
public void setChildren(List<TreeNode> children) {
this.children = children;
}
public Map<String, Object> getAttributes() {
return attributes;
}
public void setAttributes(Map<String, Object> attributes) {
this.attributes = attributes;
}
@Override
public String toString() {
return "TreeNode [id=" + id + ", text=" + text + ", children=" + children + ", attributes=" + attributes + "]";
}
}
倒方法
package com.HC.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JsonBaseDao extends BaseDao<Map<String,Object>> {
public List<Map<String,Object>> executeQuery(String sql, PageBean pageBean) throws SQLException, InstantiationException, IllegalAccessException{
return super.executeQuery(sql, pageBean, new Callback<Map<String,Object>>() {
@Override
public List<Map<String,Object>> foreach(ResultSet rs) throws SQLException, InstantiationException, IllegalAccessException {
/*
* 1、创建一个实体类的实例
* 2、给创建的实例属性赋值
* 3、将添加完类容的实体类添加到list集合中
*/
// list.add(new Book(rs.getInt("bid"), rs.getString("bname"), rs.getFloat("price")));
List<Map<String,Object>> list = new ArrayList<>();
// 获取源数据
ResultSetMetaData md = rs.getMetaData();
int count = md.getColumnCount();
Map<String,Object> map = null;
while(rs.next()) {
map = new HashMap<>();
for (int i = 1; i <= count; i++) {
map.put(md.getColumnName(i), rs.getObject(i));
}
list.add(map);
}
return list;
}
});
}
/**
*
* @param sql
* @param attrs map中的key
* @param paMap jsp向后台传递的参数集合
* @return
* @throws SQLException
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public int executeUpdate(String sql, String[] attrs, Map<String,String[]> paMap) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
Connection con = DBAccess.getConnection();
PreparedStatement pst = con.prepareStatement(sql);
for (int i = 0; i < attrs.length; i++) {
pst.setObject(i+1, JsonUtils.getParamVal(paMap, attrs[i]));
}
return pst.executeUpdate();
}
}
MenuDao(将数据库的数据转成json格式),并且实现递归调用方法,然后利用父id查子id,实现无限子查询
package com.HC.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.HC.entity.TreeNode;
import com.HC.util.JsonBaseDao;
import com.HC.util.JsonUtils;
import com.HC.util.PageBean;
import com.HC.util.StringUtils;
public class MenuDao extends JsonBaseDao {
/**
* 给前台返回tree_data1.json的字符串
*
* @param paMap
* 从前台jsp传递过来的参数集合
* @param pageBean
* @return
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public List<TreeNode> listTreeNode(Map<String, String[]> paMap, PageBean pageBean)
throws InstantiationException, IllegalAccessException, SQLException {
List<Map<String, Object>> listMap = this.listMap(paMap, pageBean);
List<TreeNode> listTreeNode = new ArrayList<>();
this.listMapToListTreeNode(listMap, listTreeNode);
return listTreeNode;
}
/**
* [{'Menuid':001,'Menuname':‘学生管理’},{{'Menuid':001,'Menuname':‘后勤管理’}}]
*
* @param paMap
* @param pageBean
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public List<Map<String, Object>> listMap(Map<String, String[]> paMap, PageBean pageBean)
throws InstantiationException, IllegalAccessException, SQLException {
String sql = "select * from t_easyui_menu where true";
String menuId = JsonUtils.getParamVal(paMap, "Menuid");
if (StringUtils.isNotBlank(menuId)) {
sql += " and parentid=" + menuId;
} else {
sql += " and parentid=-1";
}
// 这里面存放的是数据库中菜单信息
List<Map<String, Object>> listMap = super.executeQuery(sql, pageBean);
return listMap;
}
/**
* {'Menuid':001,'Menuname':‘学生管理’}
* -->
* {id:...,text:...}
* @param map
* @param treeNode
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
private void mapToTreeNode(Map<String, Object> map, TreeNode treeNode) throws InstantiationException, IllegalAccessException, SQLException {
treeNode.setId(map.get("Menuid")+"");
treeNode.setText(map.get("Menuname")+"");
treeNode.setAttributes(map);
// 将子节点添加到父节点当中,建立数据之间的父子关系 001
// treeNode.setChildren(children);
Map<String, String[]> childrenMap = new HashMap<>();
childrenMap.put("Menuid", new String[] {treeNode.getId()});
List<Map<String, Object>> listMap = this.listMap(childrenMap, null);
List<TreeNode> listTreeNode = new ArrayList<>();
this.listMapToListTreeNode(listMap, listTreeNode);
treeNode.setChildren(listTreeNode);
}
/**
* [{'Menuid':001,'Menuname':‘学生管理’},{{'Menuid':001,'Menuname':‘后勤管理’}}]
* -->
* tree_data1.json
* @param listMap
* @param listTreeNode
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
private void listMapToListTreeNode(List<Map<String, Object>> listMap, List<TreeNode> listTreeNode) throws InstantiationException, IllegalAccessException, SQLException {
TreeNode treeNode = null;
for (Map<String, Object> map : listMap) {
treeNode = new TreeNode();
mapToTreeNode(map, treeNode);
listTreeNode.add(treeNode);
}
}
}
然后写web里的MenuAction类
package com.HC.web;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.HC.dao.MenuDao;
import com.HC.entity.TreeNode;
import com.HC.framework.ActionSupport;
import com.HC.util.ResponseUtil;
public class MenuAction extends ActionSupport {
private MenuDao menuDao = new MenuDao();
public String menuTree(HttpServletRequest req,HttpServletResponse resp) {
ObjectMapper om = new ObjectMapper();
try {
// 获取到easyui框架所识别的json格式
List<TreeNode> listTreeNode = this.menuDao.listTreeNode(req.getParameterMap(), null);
ResponseUtil.write(resp, om.writeValueAsString(listTreeNode));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
然后在mvc.xml中配置
<?xml version="1.0" encoding="UTF-8"?>
<config>
<!-- <action path="/regAction" type="test.RegAction">
<forward name="failed" path="/reg.jsp" redirect="false" />
<forward name="success" path="/login.jsp" redirect="true" />
</action> -->
<action path="/menuAction" type="com.HC.web.MenuAction">
</action>
<!-- <action path="/userAction" type="com.HC.web.UserAction"> -->
<!-- <forward name="index" path="/index.jsp" redirect="false" /> -->
<!-- </action> -->
</config>
此时的jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>后台主界面</title>
<!-- ctrl+shift+r -->
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/static/js/public/easyui5/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/static/js/public/easyui5/themes/icon.css">
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/js/public/easyui5/jquery.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/js/public/easyui5/jquery.easyui.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/js/index.js"></script>
</head>
<body class="easyui-layout">
<div data-options="region:'north',border:false"
style="height: 60px; background: #B3DFDA; padding: 10px">north
region</div>
<div data-options="region:'west',split:true,title:'West'"
style="width: 150px; padding: 10px;">
<ul id="tt"></ul>
</div>
<div
data-options="region:'east',split:true,collapsed:true,title:'East'"
style="width: 100px; padding: 10px;">east region</div>
<div data-options="region:'south',border:false"
style="height: 50px; background: #A9FACD; padding: 10px;">south
region</div>
<div data-options="region:'center',title:'Center'">
<div id="menuTab" class="easyui-tabs" style="">
<div title="首页" style="padding: 20px; display: none;">欢迎界面</div>
</div>
</div>
</body>
</html>
**tree思路:我们要利用递归,根据第一个节点的id,把这个id当成子节点的parentid,依次找到所有子节点,依次类推,直到找完所有节点,最后再把我们从数据库查出来的数据格式转化为easyui树形展示的数据格式
3然后继续实现通过菜单去打开不同的tab页
按照下面的步骤创建index.js
普通的弹出窗口
$(function() {
$('#tt').tree({
url:'menuAction.action?methodName=menuTree',
onClick: function(node){
alert(node.text); // 在用户点击的时候提示
}
});
});
效果如下
然后下一步:
判断是否存在,如果table页已经被打开,那么你再次点击的话,就直接定位到当前的table页**
$(function(){
$(’#tt’).tree({
url:‘menuAction.action?methodName=menuTree’,
onClick: function(node){
// alert(node.text); // 在用户点击的时候提示
// // add a new tab panel
.
e
x
t
e
n
d
s
v
a
r
c
o
n
t
e
n
t
=
′
<
i
f
r
a
m
e
s
c
r
o
l
l
i
n
g
=
"
n
o
"
f
r
a
m
e
b
o
r
d
e
r
=
"
0
"
s
r
c
=
"
′
+
n
o
d
e
.
a
t
t
r
i
b
u
t
e
s
.
m
e
n
u
U
R
L
+
′
"
w
i
d
t
h
=
"
99
i
f
(
.extends var content = '<iframe scrolling="no" frameborder="0" src="'+node.attributes.menuURL+'" width="99%" height="99%"></iframe>'; if(
.extendsvarcontent=′<iframescrolling="no"frameborder="0"src="′+node.attributes.menuURL+′"width="99if((’#menuTab’).tabs(‘exists’,node.text)){
// 存在执行选项卡选中已有选项卡的操作
$(’#menuTab’).tabs(‘select’,node.text);
}else{
// 不存在执行新增的操作
$(’#menuTab’).tabs(‘add’,{
title:node.text,
content:content,
closable:true
});
}
}
});
})
index.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>后台主界面</title>
<!-- ctrl+shift+r -->
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/static/js/public/easyui5/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/static/js/public/easyui5/themes/icon.css">
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/js/public/easyui5/jquery.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/js/public/easyui5/jquery.easyui.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/js/index.js"></script>
</head>
<body class="easyui-layout">
<div data-options="region:'north',border:false"
style="height: 60px; background: #B3DFDA; padding: 10px">north
region</div>
<div data-options="region:'west',split:true,title:'West'"
style="width: 150px; padding: 10px;">
<ul id="tt"></ul>
</div>
<div
data-options="region:'east',split:true,collapsed:true,title:'East'"
style="width: 100px; padding: 10px;">east region</div>
<div data-options="region:'south',border:false"
style="height: 50px; background: #A9FACD; padding: 10px;">south
region</div>
<div data-options="region:'center',title:'Center'">
<div id="menuTab" class="easyui-tabs" style="">
<div title="首页" style="padding: 20px; display: none;">欢迎界面</div>
</div>
</div>
</body>
</html>
效果如下
对应不同的tab
404的原因是因为没设置