easyui
ui框架
easyui=jquery+html4(用来做后台的管理界面)
bootstrap=jquery+html5
layui
首先我们先要导入jar包以及工具类:
导入好这些文件之后,在导入js以及css文件:
代码如下:
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/static/easyui5/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/static/easyui5/themes/icon.css">
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/easyui5/jquery.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/easyui5/jquery.easyui.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/js/index.js"></script>
<title>Insert title here</title>
铭记:导包的顺序一定要根据api顺序要一致不然就会显示不出来
然后进入api拿到自己想要的布局:
<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>
{}tree_data1.json
[{
"id":1,
"text":"顶级菜单",
"children":[{
"id":11,
"text":"学生管理",
"state":"closed",
"children":[{
"id":111,
"text":"特色课程"
},{
"id":112,
"text":"作业提交"
},{
"id":113,
"text":"Company"
}]
},{
"id":12,
"text":"后勤管理",
"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"
}]
}]
TreeNode 实体类:
package com.wangjing.entity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 针对easyui属性展示的json格式串进行了实体类的描述
* @author wj
*
*/
public class TreeNode {
private String id;
private String text;
private List<TreeNode> children = new ArrayList<TreeNode>();
private Map<String, Object> attributes = new HashMap<String, Object>();
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 + "]";
}
}
menuAction :
package com.wangjing.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wangjing.dao.MenuDao;
import com.wangjing.entity.TreeNode;
import com.wangjing.util.ResponseUtil;
import com.wangjing.framework.ActionSupport;
public class MenuAction extends ActionSupport {
private MenuDao menuDao = new MenuDao();
public String menuTree(HttpServletRequest req,HttpServletResponse resp) {
try {
List<TreeNode> listTreeNode = this.menuDao.listTreeNode(req.getParameterMap(), null);
ObjectMapper om = new ObjectMapper();
ResponseUtil.write(resp, om.writeValueAsString(listTreeNode));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
MenuDao :
package com.wangjing.dao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.wangjing.entity.TreeNode;
import com.wangjing.util.JsonBaseDao;
import com.wangjing.util.JsonUtils;
import com.wangjing.util.PageBean;
import com.wangjing.util.StringUtils;
/**
* 1.查询数据库所有数据用于easyUI的tree树形态展示(但是直接得来的数据格式easyUI不识别)
* 2.递归查询节点集合,形成父节点关系,具备层次结构
* 3.转格式
* @author wj
*
*/
public class MenuDao extends JsonBaseDao {
/**
* List<TreeNode>加上ObjectMapper可以转换easyUI的tree控件识别的json串
* @param map
* @param pageBean
* @return
* @throws Exception
*/
public List<TreeNode> listTreeNode (Map<String, String[]> map,PageBean pageBean) throws Exception{
List<Map<String, Object>> listMenu = this.listMenu(map, pageBean);
List<TreeNode> ListTreeNode = new ArrayList<TreeNode>();
this.listMapToListTreeNode(listMenu, ListTreeNode);
return ListTreeNode;
}
/**
* List<Map<String, Object>>
* -->{menuid:001,Menuname:学生管理},{menuid:001,Menuname:学生管理}
* 接下来需要递归查询子节点的集合存入当前节点
* @param map
* @param pageBean
* @return
* @throws Exception
* @throws Exception
* @throws Exception
*/
public List<Map<String, Object>> listMenu (Map<String, String[]> map,PageBean pageBean) throws Exception, Exception, Exception{
String sql = "select * from t_easyui_menu where true ";
String id = JsonUtils.getParamVal(map, "Menuid");
if(StringUtils.isNotBlank(id)) {
sql += " and parentid="+id;
}else {
sql += " and parentid=-1";
}
return super.executeQuery(sql, pageBean);
}
/**
* 需要将后台数据库查出来的数据格式转换成前台easyUI所识别的数据
* @param map
* @param treeNode
* @throws Exception
*/
public void mapToTreeNode(Map<String, Object> map ,TreeNode treeNode) throws Exception {
treeNode.setId(map.get("menuid").toString());
treeNode.setText(map.get("menuname").toString());
treeNode.setAttributes(map);
// treeNode.setChildren(children);
Map<String, String[]> ChildMap = new HashMap<String, String[]>();
ChildMap.put("Menuid", new String[] {treeNode.getId()});
// 查询出当前节点所拥有的子节点的集合
List<Map<String, Object>> listMenu = this.listMenu(ChildMap, null);
List<TreeNode> ListTreeNode = new ArrayList<TreeNode>();
this.listMapToListTreeNode(listMenu, ListTreeNode);
treeNode.setChildren(ListTreeNode);
}
public void listMapToListTreeNode(List<Map<String, Object>> list,List<TreeNode> ListTreeNode) throws Exception {
TreeNode treeNode = null;
for (Map<String, Object> map : list) {
treeNode = new TreeNode();
this.mapToTreeNode(map, treeNode);
ListTreeNode.add(treeNode);
}
}
}
index.js :
$(function() {
$('#tt').tree({
url:'menuAtion.action?methodName=menuTree',
onClick: function(node){
var content = '<iframe scrolling="no" frameborder="0" src="'+node.attribtes.menuURL+'" width="99%" height="99%"></iframe>';
if($('#menuTab').tabs('exists',onde.text)){
$('#menuTab').tabs('select',onde.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">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/static/easyui5/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/static/easyui5/themes/icon.css">
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/easyui5/jquery.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/easyui5/jquery.easyui.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/js/index.js"></script>
<title>Insert title here</title>
</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="height: 800px;">
<div title="首页" style="padding:20px;display:none;">
默认首页展示内容
</div>
</div>
</div>
</body>
</html>