目录
代码区:
实体类代码演示:
package com.zking.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
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>