這篇文章給大家分享的是有關(guān)EasyUI如何實現(xiàn)樹形功能菜單的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
成都創(chuàng)新互聯(lián)主要為客戶提供服務(wù)項目涵蓋了網(wǎng)頁視覺設(shè)計、VI標(biāo)志設(shè)計、營銷網(wǎng)站、網(wǎng)站程序開發(fā)、HTML5響應(yīng)式重慶網(wǎng)站建設(shè)公司、移動網(wǎng)站建設(shè)、微商城、網(wǎng)站托管及成都企業(yè)網(wǎng)站維護(hù)、WEB系統(tǒng)開發(fā)、域名注冊、國內(nèi)外服務(wù)器租用、視頻、平面設(shè)計、SEO優(yōu)化排名。設(shè)計、前端、后端三個建站步驟的完善服務(wù)體系。一人跟蹤測試的建站服務(wù)標(biāo)準(zhǔn)。已經(jīng)為成都OPP膠袋行業(yè)客戶提供了網(wǎng)站開發(fā)服務(wù)。
頁面展示截圖如下:
為了實現(xiàn)以上效果,在開始前必須先將環(huán)境配置一下。
第一步: 首先,先將 jquery-easyui-1.2.6 文件引入到工程項目下,并在jsp頁面上進(jìn)入引入3個jsp文件和2個css文件。如下:
引入順序必須按照以上順序引入,否則頁面展示效果將出錯。
第二步:引入jar包,分別為:commons-beanutils-1.8.3.jar、commons-collections-3.2.1.jar、commons-lang-2.5.jar、commons-logging-1.1.1.jar、ezmorph-1.0.6.jar、json-lib-2.3-jdk15.jar、MySQL-connector-java-5.1.17-bin.jar
代碼實現(xiàn)
1、創(chuàng)建數(shù)據(jù)表
drop database easyui; create database easyui; use easyui; show tables; #創(chuàng)建菜單表 create table menu( id int(11) not null auto_increment, ####菜單id### name varchar(20) default null, ####菜單名#### url varchar(100) default null, #### 菜單url#### checked varchar(10) default null, ####菜單是否被選中 icon varchar(30) default null, ####菜單圖標(biāo)#### parent_id int(11) default null, ####父節(jié)點菜單的id#### primary key(id) ####id是主鍵#### ); #插入測試數(shù)據(jù) ####測試數(shù)據(jù)#### insert into menu(id,name,url,checked,icon,parent_id) values (1,'權(quán)限菜單',null,'',null,0), (2,'用戶管理',null,'0',null,1), (3,'崗位管理',null,'',null,1), (4,'資源管理',null,'',null,1), (5,'用戶功能1',null,'',null,2), (6,'崗位功能1',null,'0',null,3), (7,'資源功能2','/easyui/index.jsp','0',null,3), (8,'資源功能1','sss','0',null,4), (9,'崗位功能2',null,'',null,3), (10,'資源功能3','111','0',null,4), (11,'資源管理4','222','',null,4), (14,'崗位功能3','dfds',null,null,3), (17,'用戶功能2','sss','0',null,2); #查看數(shù)據(jù) select * from menu; //查詢跟節(jié)點 select * from menu where parent_id=0; #查看指定父節(jié)點下有哪些子節(jié)點 select * from menu where parent_id=2;
2、JDBC連接工具類
JDBCUtils.Java
package com.hsj.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class JDBCUtils { static { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection() throws Exception { return DriverManager.getConnection( "jdbc:mysql:///easyui?useUnicode=true&characterEncoding=UTF-8", "root", "zxczxc"); } public static void close(ResultSet rs, PreparedStatement ps, Connection conn) { try { if (rs != null) rs.close(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (ps != null) ps.close(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (conn != null) conn.close(); } catch (Exception e) { e.printStackTrace(); } } } } }
3、創(chuàng)建實體類domain
Menu.java
package com.hsj.domain;
public class Menu {
private int id; //菜單id
private String name; //菜單名
private String url; //菜單鏈接的網(wǎng)址
private String checked; //菜單是否被選中
private String icon; //菜單圖標(biāo)
private int parent_id; //當(dāng)前菜單的父節(jié)點id
public Menu(){}
public Menu(int id, String name, String url, String checked, String icon,int parentId) {
this.id = id;
this.name = name;
this.url = url;
this.checked = checked;
this.icon = icon;
parent_id = parentId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getChecked() {
return checked;
}
public void setChecked(String checked) {
this.checked = checked;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public int getParent_id() {
return parent_id;
}
public void setParent_id(int parentId) {
parent_id = parentId;
}
}
TreeDTD.java
package com.hsj.domain;
import java.util.HashMap;
import java.util.Map;
public class TreeDTO {
private int id;
private String text;
private String iconCls;
private String checked;
private int parent_id;
private String state;
/**
* 自定義屬性信息
*/
private Map attributes = new HashMap();
public TreeDTO() {
}
public TreeDTO(int id, String text, String iconCls, String checked,
int parent_id, String state, Map attributes) {
this.id = id;
this.text = text;
this.iconCls = iconCls;
this.checked = checked;
this.parent_id = parent_id;
this.state = state;
this.attributes = attributes;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getIconCls() {
return iconCls;
}
public void setIconCls(String iconCls) {
this.iconCls = iconCls;
}
public String getChecked() {
return checked;
}
public void setChecked(String checked) {
this.checked = checked;
}
public int getParent_id() {
return parent_id;
}
public void setParent_id(int parentId) {
parent_id = parentId;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Map getAttributes() {
return attributes;
}
public void setAttributes(Map attributes) {
this.attributes = attributes;
}
@Override
public String toString() {
return "TreeDTO [attributes=" + attributes + ", checked=" + checked
+ ", iconCls=" + iconCls + ", id=" + id + ", parent_id="
+ parent_id + ", state=" + state + ", text=" + text + "]";
}
}
4、創(chuàng)建接口DAO
MeunDao.java
package com.hsj.dao;
import java.util.List;
import com.hsj.domain.Menu;
import com.hsj.domain.TreeDTO;
public interface MenuDao{
/**
* 根據(jù)父節(jié)點的值查詢所有的子節(jié)點
* @param parentId
* @return
*/
public List getChildrenByParentId(String parentId);
/**
* 根據(jù)id值查詢當(dāng)前對象
* @param id
* @return
*/
public Menu findMenuById(int id);
/**
* 保存指定對象
* @param
* @param t
*/
public void save(T t);
/**
* 修改菜單對象
* @param menu
*/
public void update(Menu menu);
/**
* 根據(jù)id刪除指定對象
* @param id
*/
public void delete(int id);
/**
* 根據(jù)父節(jié)點刪除當(dāng)前父節(jié)點下所有的子節(jié)點
* @param parentId 父節(jié)點id
*/
public void deleteChildrenByParentId(int parentId);
/**
* 根據(jù)當(dāng)前節(jié)點 的id值刪除它的所有子節(jié)點
* @param id
*/
public void deleteChildren(int id);
}
5、實現(xiàn)DAO接口方法Bean
MenuDaoBean.java
package com.hsj.dao.bean;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.hsj.dao.MenuDao;
import com.hsj.domain.Menu;
import com.hsj.domain.TreeDTO;
import com.hsj.utils.JDBCUtils;
public class MenuDaoBean implements MenuDao{
public int getTotalRecord(Class clazz) {
// TODO Auto-generated method stub
return 0;
}
public void save(T t)
{
//1.根據(jù)對象得到類模板對象
Class clazz= (Class) t.getClass();
//2.得到當(dāng)前類中所有字段組成的數(shù)組,不管訪問權(quán)限如何,但不包括父類中的字段
Field[] fields=clazz.getDeclaredFields();
//insert into t_menu(field1,field2,....) values(value1,value2,....)
List key=new ArrayList();//key用來存儲字段列表
List
6、創(chuàng)建Servlet 并配置映射文件
MenuServlet.java
package com.hsj.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import com.hsj.dao.MenuDao;
import com.hsj.dao.bean.MenuDaoBean;
import com.hsj.domain.Menu;
import com.hsj.domain.TreeDTO;
public class MenuServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String method = request.getParameter("method");
if (method != null && !"".equals(method) && "getMenu".equals(method)) {
getMenu(request, response);
} else if (method != null && !"".equals(method)
&& "changeMenu".equals(method)) {
changeMenu(request, response);
} else if (method != null && !"".equals(method)
&& "addMenu".equals(method)) {
addMenu(request, response);
} else if (method != null && !"".equals(method)
&& "updateMenu".equals(method)) {
updateMenu(request, response);
} else if (method != null && !"".equals(method)
&& "deleteMenu".equals(method)) {
deleteMenu(request, response);
}
out.flush();
out.close();
}
/**
* 刪除當(dāng)前菜單及其當(dāng)前菜單的所有子菜單
* @param request
* @param response
*/
private void deleteMenu(HttpServletRequest request,
HttpServletResponse response) {
String id=request.getParameter("id");
MenuDao menuDao=new MenuDaoBean();
System.out.println(id);
menuDao.deleteChildren(Integer.valueOf(id));
}
/**
* 修改菜單
* @param request
* @param response
*/
private void updateMenu(HttpServletRequest request,
HttpServletResponse response) {
String id=request.getParameter("id");
String name=request.getParameter("name");
String url=request.getParameter("url");
MenuDao menuDao=new MenuDaoBean();
Menu menu=menuDao.findMenuById(Integer.valueOf(id));
menu.setName(name);
menu.setUrl(url);
menuDao.update(menu);
}
/**
* 添加菜單
* @param request
* @param response
*/
private void addMenu(HttpServletRequest request,HttpServletResponse response) {
String parentId=request.getParameter("parentId");
String name=request.getParameter("name");
String url=request.getParameter("url");
Menu menu=new Menu();
menu.setName(name);
menu.setUrl(url);
menu.setParent_id(Integer.valueOf(parentId));
MenuDao menuDao=new MenuDaoBean();
menuDao.save(menu);
}
/**
* 菜單菜單的父菜單
* @param request
* @param response
*/
private void changeMenu(HttpServletRequest request,
HttpServletResponse response) {
String targetId= request.getParameter("targetId");
String sourceId= request.getParameter("sourceId");
String point= request.getParameter("point");
System.out.println("point="+point);
MenuDao menuDao=new MenuDaoBean();
Menu target= menuDao.findMenuById(Integer.valueOf(targetId));
Menu source= menuDao.findMenuById(Integer.valueOf(sourceId));
if("append".equals(point))
{
//源菜單作為目標(biāo)菜單的子菜單
source.setParent_id(target.getId());
}else{
//源菜單和目標(biāo)菜單使用相同的父菜單的id值
source.setParent_id(target.getParent_id());
}
menuDao.update(source);
}
/**
* 根據(jù)父id得到它所有的子菜單
* @param request
* @param response
*/
private void getMenu(HttpServletRequest request,HttpServletResponse response) {
System.out.println("getMenu-------");
//獲取當(dāng)前展的節(jié)點的id
try {
String parentId=request.getParameter("id");
MenuDao menuDao=new MenuDaoBean();
List treeDTOS=menuDao.getChildrenByParentId(parentId);
System.out.println(treeDTOS.toString());
response.setContentType("text/html;charset=utf-8");
String json=JSONArray.fromObject(treeDTOS).toString();
response.getWriter().write(json);
System.out.println("json="+json);
} catch (Exception e) {
e.printStackTrace();
}
}
}
映射文件信息:
Web.xml
MenuServlet
com.hsj.servlet.MenuServlet
MenuServlet
/menuServlet
index.jsp
7、JSP網(wǎng)頁代碼
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
">
tree的使用
Append
Editor
Remove
感謝各位的閱讀!關(guān)于“EasyUI如何實現(xiàn)樹形功能菜單”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!