市面上的ui框架大部分是三类:
easyui:基于jquery+html4的,功能强大,但是界面没有其余两种好,并且不支持响应式。
bootstrap:基于jquery+html5的,功能也大部分完善,最重要的是页面最美观,但缺点是大部分插件等需要付费。
easyui简介
jQuery EasyUI是一组基于jQuery的UI插件集合体,而jQuery EasyUI的目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面。开发者不需要编写复杂的javascript,也不需要对css样式有深入的了解,开发者需要了解的只有一些简单的html标签
什么是easyui
easyui是一种基于jQuery的用户界面插件集合。
easyui为创建现代化,互动,JavaScript应用程序,提供必要的功能。
使用easyui你不需要写很多代码,你只需要通过编写一些简单HTML标记,就可以定义用户界面。
easyui是个完美支持HTML5网页的完整框架。
easyui节省您网页开发的时间和规模。
easyui很简单但功能强大的。
easyui的导入
首先我们先要导入easyui的库:
强调一下:他们都是有先后顺序的,因为easyui是基于jQuery的,如果顺序乱了就会出问题
<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>
导入它的文件和jar包
导入jar,里面有一个我们自己写的jar,就是mvc.jar,
我们还需要准备连数据库和后面需要用到的一些工具类,
还有我们之前用的web.xml
获取数据库的树形菜单
这次我们主要实现以下几点:
1、通过layout布局
2、通过tree加载菜单
3、通过菜单去打开不同的tab页
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加载菜单
树控件也可以定义在一个空
-
元素中并使用Javascript加载数据。
现在这个ul是空的,
<ul id="tt"></ul>
现在能展示出来,但是数据是死的,
$(function(){
$('#tt').tree({
url:'tree_date1.json',
});
})
tree_date1.json我们看一下这个是什么,
tree_date1.json是一种数据格式,这个格式不能变,它是json的串,它是json里面的混合模式;
我们的树形菜单必须符合这种格式,它才能展示出来:
[{
"id":1,
"text":"菜单管理",
"children":[{
"id":11,
"text":"财务",
"state":"closed",
"children":[{
"id":111,
"text":"学费缴纳"
},{
"id":112,
"text":"Wife"
},{
"id":113,
"text":"Company"
}]
},{
"id":12,
"text":"后勤",
"children":[{
"id":121,
"text":"Intel"
},{
"id":122,
"text":"宿舍费用缴纳",
"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"
}]
}]
我们要建一个展示用的实体类:
作用是通过TreeName类转换成tree_data1.json的字符串
package com.tanhaiafang.entity;
/**
* 作用是通过TreeName类转换成,tree_data1.json的字符串
* @author Admin
*
*/
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> 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 + "]";
}
}
接下来我们要建一个MenuDao继承JsonBaseDao:
我们要查数据库就要继承JsonBaseDao,我在jQuery里面写了这个dao,
思路:这个地方是我们最难理解的地方我们要利用递归,根据第一个节点的id,把这个id当成子节点的parentid,依次找到所有子节点,直到找完所有节点,最后再把我们从数据库查出来的数据格式转化为easyui树形展示的数据格式
package com_tanhaifang.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.tanhaiafang.entity.TreeNode;
import com_tanhaifang.util.JsonBaseDao;
import com_tanhaifang.util.JsonUtils;
import com_tanhaifang.util.PageBean;
import com_tanhaifang.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{
//我们调用下面的方法 ListMapTOListTreeNode
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");//获取meuid,从前台传过来的
if(StringUtils.isNotBlank(menuId)) {//如果id 不为空我们就拼接
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.setAttributes(map);
//将子节点添加到父节点当中,建立数据之间的父子关系
//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':’菜单管理‘:‘学生管理’},{''},]
*
* @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);
}
}
}
MenuAction
它要继承ActionSupport
package com_tanhaifang.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhaiafang.entity.TreeNode;
import com.tanhaifang.framework.ActionSupport;
import com_tanhaifang.dao.MenuDao;
import com_tanhaifang.util.ResponseUtil;
public class MenuAction extends ActionSupport{
private MenuDao menuDao=new MenuDao();//调用后台的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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
编写index.js
$(function(){
$('#tt').tree({
url:'menuAction.action?methodName=menuTree',
onClick: function(node){
// alert(node.text); // 在用户点击的时候提示
// // add a new tab panel $.extends
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
});
}
}
});
})
效果如下:
3、通过菜单去打开不同的tab页
index .js
$(function(){
$('#tt').tree({
url:'menuAction.action?methodName=menuTree',
onClick: function(node){
// alert(node.text); // 在用户点击的时候提示
// // add a new tab panel $.extends
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
});
}
}
});
})
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>
<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="width:500px;height:250px;">
<div title="首页" style="padding:20px;display:none;">
欢迎界面
</div>
</div>
</div>
</body>
</html>
编写index.js
$(function(){
$('#tt').tree({
url:'menuAction.action?methodName=menuTree',
onClick: function(node){
// alert(node.text); // 在用户点击的时候提示
// // add a new tab panel $.extends
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
});
}
}
});
})
效果如下:
这个报错404是因为我们还没有这个页面,