Easyui (一)

easyui是什么:
easyui就是一个前端框架,JQuery EasyUI是一组基于jQuery的UI插件集合体,而jQuery EasyUI的目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面.开发者不需要编写复杂的javascript,也不需要对css样式有深入的了解。
案例:
1、通过layout布局
2、通过tree加载菜单
3、通过菜单去打开不同的tab页
我们使用一个框架首先要引入它的文件和jar包
在这里插入图片描述
然后把文件导入(切记文件顺序一定不要乱放)

<link rel="stylesheet" type="text/css"href="${pageContext.request.contextPath }/static/easyui5/themes/metro/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>

在这里插入代码片

导入架包
在这里插入图片描述
导入我们的需要的类
在这里插入图片描述
实体类
TreeNode

package com.yq.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 针对easyui属性展示的json格式串进行了实体类的描述
 * @author 陌陌
 *
 */
public class TreeNode {
	private String id;
	private String text;
	private Map<String, Object> attributes = new HashMap<>();
	private List<TreeNode> children = new ArrayList<>();

	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 Map<String, Object> getAttributes() {
		return attributes;
	}

	public void setAttributes(Map<String, Object> attributes) {
		this.attributes = attributes;
	}

	public List<TreeNode> getChildren() {
		return children;
	}

	public void setChildren(List<TreeNode> children) {
		this.children = children;
	}

	public TreeNode(String id, String text, Map<String, Object> attributes, List<TreeNode> children) {
		super();
		this.id = id;
		this.text = text;
		this.attributes = attributes;
		this.children = children;
	}

	public TreeNode() {
		super();
	}

	@Override
	public String toString() {
		return "TreeNode [id=" + id + ", text=" + text + ", attributes=" + attributes + ", children=" + children + "]";
	}

}

在这里插入代码片

MenuDao

package com.yq.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yq.entity.TreeNode;
import com.yq.util.JsonBaseDao;
import com.yq.util.JsonUtils;
import com.yq.util.PageBean;
import com.yq.util.StringUtils;

/**
 * 1.查询数据库所以数据用于easyui的tree树形展示(但是直接得来的数据格式easyui不识别)
 * 2.递归查询节点集合,形成父节点关系,具备层次结构
 * 3.转格式
 * @author 陌陌
 *
 */
public class MenuDao extends JsonBaseDao{
	/**
	 *  List<TreeNode>加上ObjectMapper可以转换成easyui控件识别的json串
	 * @param map
	 * @param pageBean
	 * @return
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
public List<TreeNode> listTreeNode(Map<String, String[]> map,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
	List<Map<String, Object>> listMenu=this.listMenu(map, pageBean);
	List<TreeNode> listTreeNode=new ArrayList<TreeNode>();
	this.listMapToTreeNode(listMenu, listTreeNode);
	return listTreeNode;
	
}

/**
 * List<Map<String, Object>> 
 * ->[{Menuid:001,Menuname:学生管理,children:[]},{Menuid:001,Menuname:学生管理}]
 * 接下来需要递归查询子节点的集合存入当前节点
 * 
 * @param map
 * @param pageBean
 * @return
 * @throws InstantiationException
 * @throws IllegalAccessException
 * @throws SQLException
 */
public List<Map<String, Object>> listMenu(Map<String, String[]> map,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
	String sql="select * from t_easyui_menu";
	String id=JsonUtils.getParamVal(map, "Menuid");
	if(StringUtils.isNotBlank(id)) {
//	原生节点的id当作子节点父id进行查询	
		sql+=" and pareentid="+id;
	}else {
		sql +=" and parentid=-1";
	}
	return super.executeQuery(sql, pageBean);
	
}

/**
 * 需要将后台数据库查出来的数据格式转换成前台easyui所识别的数据
 * @param map
 * @param treeNode
 * @throws SQLException 
 * @throws IllegalAccessException 
 * @throws InstantiationException 
 */
public void mapToTreeNode(Map<String,Object>map,TreeNode treeNode) throws InstantiationException, IllegalAccessException, SQLException {
	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.listMapToTreeNode(listMenu, listTreeNode);
	treeNode.setChildren(listTreeNode);
}

public void listMapToTreeNode(List<Map<String, Object>> list,List<TreeNode> listTreeNode) throws InstantiationException, IllegalAccessException, SQLException {
	TreeNode treeNode=null;
	for (Map<String, Object> map : list) {
		treeNode=new TreeNode();
		this.mapToTreeNode(map, treeNode);
		listTreeNode.add(treeNode);
	}
	
	
	
}
}

在这里插入代码片

MenuAction

package com.yq.web;

import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yq.dao.MenuDao;
import com.yq.entity.TreeNode;
import com.yq.util.ResponseUtil;
import com.zking.framework.ActionSupport;

public class MenuAction extends ActionSupport{

	 private MenuDao  menuDao = new MenuDao();

		 public String treeMenu(HttpServletRequest req,HttpServletResponse resp) throws Exception {
		  try {
		   List<TreeNode> list = this.menuDao.list(req.getParameterMap(), null);
		   ObjectMapper om = new ObjectMapper();
		   //将listjihe 转换成json串
		   String jsonStr = om.writeValueAsString(list);
		   ResponseUtil.write(resp, jsonStr);
		  } catch (Exception e) {
		   e.printStackTrace();
		  }
		  return null;
		 }
	}


在这里插入代码片

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/metro/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>后台管理主界面</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>
</body>

</html>
在这里插入代码片

index.js

$(function(){
	$('#tt').tree({    
	     url:'menuAction.action?methodName=treeMenu',
	     onClick:function(node){
	      if($('#menuTabs').tabs('exists',node.text)){
	       $('#menuTabs').tabs('select',node.text)
	      }
	      else{
	       $('#menuTabs').tabs('add',{    
	              title: node.text,    
	              content:'Tab Body',    
	              closable:true,    
	              tools:[{    
	                  iconCls:'icon-mini-refresh',    
	                  handler:function(){    
	                      alert('refresh');    
	                  }    
	              }]    
	          });
	      }
	     }
	 });
	})

在这里插入代码片

mvc.xml:

<?xml version="1.0" encoding="UTF-8"?>
<config>
	<action path="/menuAction" type="com.yq.web.MenuAction">
	</action>
	<action path="/userAction" type="com.yq.web.UserAction">
		<forward name="index" path="/index.jsp" redirect="false" />
	</action>
</config>
在这里插入代码片

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>easyui</display-name>
   <filter>
  	<filter-name>encodingFiter</filter-name>
  	<filter-class>com.yq.util.EncodingFiter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>encodingFiter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <servlet>
  	<servlet-name>actionServlet</servlet-name>
  	<servlet-class>com.zking.framework.ActionServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>actionServlet</servlet-name>
  	<url-pattern>*.action</url-pattern>
  </servlet-mapping>
</web-app>
在这里插入代码片

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值