一.什么是easyui?
easyui是一种基于jQuery的用户界面插件集合
二.easyui能带给我们什么好处?
1.easyui是个完美支持HTML5网页的完整框架
2.easyui节省网页开发的时间和规模
3.easyui很简单但功能强大
3.1 防止页面缓存
3.2 路径问题
3.2.1 相对路径/绝对路径
绝对路径:${pageContext.request.contextPath}
3.2.2 base标签
三.示例
1.layout布局
首先导入需要用的各类文件
引用css,jQuery,easyUI样式到jsp页面上。找到各种文件名,替换下列文件的路径,并且改为绝对路径(注:jquery.min.js必须放在jquery.easyui.min.js,避免路径错误)
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/js/public/easyui/themes/black/easyui.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/js/public/easyui/themes/icon.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/public/easyui/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/public/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}//static/js/index.js"></script>
找到文件demo,并在其中的layout中找到full.html页面,复制下列源代码到jsp页面
<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>
</body>
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=ISO-8859-1">
<title>后台主界面</title>
<!-- 找到各种文件名,替换下列文件的路径,并且改为绝对路径 -->
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/js/public/easyui/themes/black/easyui.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/js/public/easyui/themes/icon.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/public/easyui/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/public/easyui/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>
</body>
</html>
2.树形(tree)菜单
导入jar包和各种工具类
创建一个仿json形式的实体类TreeNode
作用是通过TreeNode的类转换成tree_data.json的字符串
package com.ningjie.entity;
/**
* 作用是通过TreeNode的类转换成tree_data.json的字符串
* @author NJ321
*
*/
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 List<TreeNode> children=new ArrayList<>();
//跳转页面的集合
private Map<String, Object> attribute=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> getAttribute() {
return attribute;
}
public void setAttribute(Map<String, Object> attribute) {
this.attribute = attribute;
}
@Override
public String toString() {
return "TreeNode [id=" + id + ", text=" + text + ", children=" + children + ", attribute=" + attribute + "]";
}
}
编写一个dao方法类访问数据库里的数据,进行填充(MenuDao)
package com.ningjie.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.ningjie.entity.TreeNode;
import com.zking.util.JsonBaseDao;
import com.zking.util.JsonUtils;
import com.zking.util.PageBean;
import com.zking.util.StringUtils;
public class MenuDao extends JsonBaseDao{
/**
* 给前台返回tree_data.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{
// 获取到easyUI框架所识别的json格式
<Map<String, Object>> listMap = this.listMap(paMap, pageBean);
List<TreeNode> list=new ArrayList<>();
this.listMapToListTreeNode(listMap, list);
return null;
}
/**
* [{'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;
}
private void MapToTreeNode(Map<String, Object> map,TreeNode treeNode) throws InstantiationException, IllegalAccessException, SQLException {
treeNode.setId(map.get("Menuid")+"");
treeNode.setText(map.get("Menuname")+"");
treeNode.setAttribute(map);
// 将子节点放到父节点当中,建立父子关系
// treeNode.setChildren(children);
Map<String, String[]> childrenMap=new HashMap<>();
childrenMap.put("Menuid", new String[] {treeNode.getId()});
this.listMap(childrenMap, null);
List<TreeNode> list=new ArrayList<>();
this.listMapToListTreeNode(listMap, list);
treeNode.setChildren(list);
}
private void listMapToListTreeNode(Map<String, Object> listMap,List<TreeNode> list) {
TreeNode treeNode=null;
for (Map<String, Object> map : listMap) {
treeNode=new TreeNode();
MapToTreeNode(map, treeNode);
list.add(treeNode);
}
}
}
MenuAction
package com.ningjie.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ningjie.dao.MenuDao;
import com.ningjie.entity.TreeNode;
import com.zking.framework.ActionSupport;
import com.zking.util.ResponseUtil;
public class MenuAction extends ActionSupport{
private MenuDao menuDao=new MenuDao();
public String meunTree(HttpServletRequest req,HttpServletResponse resp) {
ObjectMapper om=new ObjectMapper();
try {
List<TreeNode> listTreeNode = this.menuDao.listTreeNode(req.getParameterMap(), null);
ResponseUtil.write(resp, om.writeValueAsBytes(listTreeNode));
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
进行mvc.xml配置
<action path="/menuAction" type="com.ningjie.web.MenuAction">
</action>
编写index.js
$(function () {
$('#tt').tree({
url:'menuAction.action?methodName=meunTree'
});
});
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=ISO-8859-1">
<title>后台主界面</title>
<!-- 找到各种文件名,替换下列文件的路径,并且改为绝对路径 -->
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/js/public/easyui/themes/black/easyui.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/js/public/easyui/themes/icon.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/public/easyui/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/public/easyui/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>
</body>
</html>
3.tab页
在API文件中找到下列文件
<div id="tt" class="easyui-tabs" style="width:500px;height:250px;">
<div title="Tab1" style="padding:20px;display:none;">
tab1
</div>
<div title="Tab2" data-options="closable:true" style="overflow:auto;padding:20px;display:none;">
tab2
</div>
<div title="Tab3" data-options="iconCls:'icon-reload',closable:true" style="padding:20px;display:none;">
tab3
</div>
</div>
// add a new tab panel
$('#tt').tabs('add',{
title:'New Tab',
content:'Tab Body',
closable:true,
tools:[{
iconCls:'icon-mini-refresh',
handler:function(){
alert('refresh');
}
}]
});
index.js
$(function(){
$('#tt').tree({
url:'menuAction.action?methodName=menuTree',
onClick: function(node){
// alert(node.text); 在用户点击的时候提示
// add a new tab panel
var content = '<iframe scrolling="no" frameborder="0" src="'+node.attributes.menuURL+'" width="99%" height="99%"></iframe>';
if($('#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=ISO-8859-1">
<title>后台主界面</title>
<!-- 找到各种文件名,替换下列文件的路径,并且改为绝对路径 -->
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/js/public/easyui/themes/black/easyui.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/js/public/easyui/themes/icon.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/public/easyui/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/public/easyui/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="tt" class="easyui-tabs" style="width:500px;height:250px;">
<div title="首页" style="padding:20px;display:none;">
欢迎界面
</div>
</div>
</div>
</body>
</html>