public class Module implements Serializable{
private Integer id;
private Integer pid;
private String text;
private String icon;
private String url;
private int sort;
private List<Module> children = new ArrayList<>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public int getSort() {
return sort;
}
public void setSort(int sort) {
this.sort = sort;
}
public List<Module> getChildren() {
return children;
}
public void setChildren(List<Module> children) {
this.children = children;
}
@Override
public String toString() {
return "Module [id=" + id + ", pid=" + pid + ", text=" + text + ", icon=" + icon + ", url=" + url + ", sort="
+ sort + ", children=" + children + "]";
}
}
biz层:
package com.zking.service;
import java.util.List;
import com.zking.euidemo.IModuleDao;
import com.zking.euidemo.ModuleDao;
import com.zking.model.Module;
public class ModuleService implements IModuleService{
private IModuleDao dao = new ModuleDao();
@Override
public List<Module> listModel(int pid) {
List<Module> list = dao.getOne(pid);
//递归
for(Module m: list) {
//判断表中url是否为空
if(m.getUrl() == null || "".equals(m.getUrl().trim())) {
m.setChildren(listModel(m.getId()));
}
}
return list;
}
//测试
public static void main(String[] args) {
IModuleService service = new ModuleService();
List<Module> list = service.listModel(-1);
list.forEach(t->System.out.println(t));
}
}
Servlet
package com.zking.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import com.zking.model.Module;
import com.zking.service.IModuleService;
import com.zking.service.ModuleService;
@WebServlet("/ModuleServlet")//相当于配置和映射
public class ModuleServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码方式
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=UTF-8");
PrintWriter out = resp.getWriter();
IModuleService ims = new ModuleService();
List<Module> list = ims.listModel(-1);
//将list转为JSON格式
String str = JSON.toJSONString(list);
out.write(str);
out.flush();//刷新
out.close();//关闭
}
}
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>easyui03</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
界面代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<%@ include file="common/head.jsp" %>
<title>Insert title here</title>
<script type="text/javascript">
$(function(){
$("#funcTree").tree({
url:ctx+'/ModuleServlet',
//用户双击一个节点触发
onDblClick: function(node){
if(!$("#tt").tabs('exists',node.text)){
$('#tt').tabs('add',{
title:node.text,
content:node.text,
closable:true,
tools:[{
iconCls:'icon-mini-refresh',
handler:function(){
alert('refresh');
}
}]
});
}
}
});
});
</script>
</head>
<body class="easyui-layout">
<div data-options="region:'west',title:'功能导航',split:true" style="width:150px;">
<ul id="funcTree" class="easyui-tree"></ul>
</div>
<div data-options="region:'center',title:'center title'" style="padding:5px;background:#eee;">
<div id="tt" class="easyui-tabs" style="width:100%;height:100%;">
<div title="首页" style="padding:20px;display:none;">
首页
</div>
</div>
</div>
<div data-options="region:'south',split:true" style=" text-align:center;height:30px;background: #E0ECFF" class="panel-title">
</div>
</body>
</html>
easyui02
最新推荐文章于 2022-05-06 21:22:37 发布