easyUI之后端实现

缺陷:

1.json串并没有体现出父子层级关系,数据之间都是平级的

2.json串属性并不是id,text等easyUI要求的属性

思考:

1.TreeVo对象

**2.List

–>List<TreeVo >

TreeVo  vo=new TreeVo();

vo.setid(menu.getId());

vo.setText(menu.getmenuname());**

3.让List<TreeVo

>中的数据有父子层级关系

MenuDao:


package com.zking.dao;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.zking.entity.Menu;

import com.zking.util.BaseDao;

import com.zking.util.BuildTree;

import com.zking.util.PageBean;

import com.zking.util.TreeVo;

public class MenuDao extends BaseDao

{

public List

list(Menu menu, PageBean pageBean) throws Exception {

return super.executeQuery(“select * from t_easyui_menu”, Menu.class, pageBean);

}

public List<TreeVo

> tree(Menu menu, PageBean pageBean) throws Exception {

// 拿到的是平级,没有父子层级关系的数据

List

list = this.list(menu, pageBean);

// List

–>List<TreeVo >

List<TreeVo

> listVos = new ArrayList<TreeVo >();

for (Menu m : list) {

TreeVo

vo = new TreeVo<>();

vo.setId(m.getMenuid());

vo.setText(m.getMenuname());

vo.setParentId(m.getParentid());

Map<String, Object> attributes = new HashMap<String, Object>();

// self是个关键字的名字,随便取一个名字

// node.attributes.self.menuURL

attributes.put(“self”, m);

vo.setAttributes(attributes);

listVos.add(vo);

}

// 构建父子层级关系

return BuildTree.buildList(listVos, “000”);

}

public static void main(String[] args) throws Exception {

MenuDao menuDao = new MenuDao();

// List

list = menuDao.list(null, null);

List<TreeVo

> tree = menuDao.tree(null, null);

ObjectMapper om = new ObjectMapper();

/**

  • 缺陷: 1.json串并没有体现出父子层级关系,数据之间都是平级的 2.json串属性并不是id,text等easyUI要求的属性

  • 思考: 1.TreeVo对象 2.List

    –>List<TreeVo > TreeVo vo=new TreeVo();

  • vo.setid(menu.getId()); vo.setText(menu.getmenuname());

  • 3.让List<TreeVo

    >中的数据有父子层级关系 由工具类实现

*/

System.out.println(om.writeValueAsString(tree));

}

}

TreeVo:


package com.zking.util;

import java.util.ArrayList;

import java.util.List;

import java.util.Map;

public class TreeVo {

/**

  • 节点ID

*/

private String id;

/**

  • 显示节点文本

*/

private String text;

/**

  • 节点状态,open closed

*/

private Map<String, Object> state;

/**

  • 节点是否被选中 true false

*/

private boolean checked = false;

/**

  • 节点属性

*/

private Map<String, Object> attributes;

/**

  • 节点的子节点

*/

private List<TreeVo> children = new ArrayList<TreeVo>();

/**

  • 父ID

*/

private String parentId;

/**

  • 是否有父节点

*/

private boolean hasParent = false;

/**

  • 是否有子节点

*/

private boolean hasChildren = false;

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> getState() {

return state;

}

public void setState(Map<String, Object> state) {

this.state = state;

}

public boolean isChecked() {

return checked;

}

public void setChecked(boolean checked) {

this.checked = checked;

}

public Map<String, Object> getAttributes() {

return attributes;

}

public void setAttributes(Map<String, Object> attributes) {

this.attributes = attributes;

}

public List<TreeVo> getChildren() {

return children;

}

public void setChildren(List<TreeVo> children) {

this.children = children;

}

public boolean isHasParent() {

return hasParent;

}

public void setHasParent(boolean isParent) {

this.hasParent = isParent;

}

public boolean isHasChildren() {

return hasChildren;

}

public void setChildren(boolean isChildren) {

this.hasChildren = isChildren;

}

public String getParentId() {

return parentId;

}

public void setParentId(String parentId) {

this.parentId = parentId;

}

public TreeVo(String id, String text, Map<String, Object> state, boolean checked, Map<String, Object> attributes,

List<TreeVo> children, boolean isParent, boolean isChildren, String parentID) {

super();

this.id = id;

this.text = text;

this.state = state;

this.checked = checked;

this.attributes = attributes;

this.children = children;

this.hasParent = isParent;

this.hasChildren = isChildren;

this.parentId = parentID;

}

public TreeVo() {

super();

}

}

BuildTree:


package com.zking.util;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

public class BuildTree {

/**

  • 默认-1为顶级节点

  • @param nodes

  • @param

  • @return

*/

public static TreeVo build(List<TreeVo> nodes) {

if (nodes == null) {

return null;

}

List<TreeVo> topNodes = new ArrayList<TreeVo>();

for (TreeVo children : nodes) {

String pid = children.getParentId();

if (pid == null || “-1”.equals(pid)) {

topNodes.add(children);

continue;

}

for (TreeVo parent : nodes) {

String id = parent.getId();

if (id != null && id.equals(pid)) {

parent.getChildren().add(children);

children.setHasParent(true);

parent.setChildren(true);

continue;

}

}

}

TreeVo root = new TreeVo();

if (topNodes.size() == 1) {

root = topNodes.get(0);

} else {

root.setId(“000”);

root.setParentId(“-1”);

root.setHasParent(false);

root.setChildren(true);

root.setChecked(true);

root.setChildren(topNodes);

root.setText(“顶级节点”);

Map<String, Object> state = new HashMap<>(16);

state.put(“opened”, true);

root.setState(state);

}

return root;

}

/**

  • 指定idparam为顶级节点

  • @param nodes

  • @param idParam

  • @param

  • @return

*/

public static List<TreeVo> buildList(List<TreeVo> nodes, String idParam) {

if (nodes == null) {

return null;

}

List<TreeVo> topNodes = new ArrayList<TreeVo>();

for (TreeVo children : nodes) {

String pid = children.getParentId();

if (pid == null || idParam.equals(pid)) {

topNodes.add(children);

continue;

}

for (TreeVo parent : nodes) {

String id = parent.getId();

if (id != null && id.equals(pid)) {

parent.getChildren().add(children);

children.setHasParent(true);

parent.setChildren(true);

continue;

}

}

}

return topNodes;

}

}

ResponseUtil:


package com.zking.util;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;

public class ResponseUtil {

public static void write(HttpServletResponse response, Object o) throws IOException {

response.setContentType(“text/html;charset=utf-8”);

PrintWriter out = response.getWriter();

out.println(o.toString());

out.flush();

out.close();

}

public static void writeJson(HttpServletResponse response, Object o) throws IOException {

ObjectMapper om = new ObjectMapper();

// om.writeValueAsString(o)代表了json串

write(response, om.writeValueAsString(o));

}

}

子控制器 MenuAction


package com.zking.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.zking.dao.MenuDao;

import com.zking.entity.Menu;

import com.zking.framework.ActionSupport;

import com.zking.framework.ModelDriver;

import com.zking.util.ResponseUtil;

import com.zking.util.TreeVo;

public class MenuAction extends ActionSupport implements ModelDriver

{

private Menu menu = new Menu();

private MenuDao menuDao = new MenuDao();

@Override

public Menu getModel() {

return menu;

}

public String tree(HttpServletRequest req, HttpServletResponse resp) {

try {

List<TreeVo

> tree = menuDao.tree(null, null);

ResponseUtil.write(resp, tree);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return null;

}

}

配置xml文件


<?xml version="1.0" encoding="UTF-8"?>

修改js文件的url


$(function() {

/**

  • . a j a x 无刷新 1. t r e e 方法是通过 .ajax无刷新 1.tree方法是通过 .ajax无刷新1.tree方法是通过.extends()拓展出来的 2.tree方法做的事情 $(‘#tt’).append("

  • File21
  • "); 需求: 1.点击左侧显示右侧Tab ①给菜单添加点击事件 ②调用Tabs选项卡打开对应的页面 选项卡打开 选项卡对应的页面展示

  • 2.不能打开重复的Tab 拿到目前所有打开的Tab选项卡,与将要打开的选项卡做对比exists 存在true:不打开 不存在false:打开

  • 3.对于已经存在的Tab选项,被点击时应默认被选中 4.点击菜单,能够访问对应的页面,而非文字内容

  • 注意:js文件中不支持el表达式

*/

$(‘#stuMenu’)

.tree(

{

url : $(“#ctx”).val() + ‘/menu.action?methodName=tree’,

onClick : function(node) {

// 判断当前选项卡是否存在

var exists = $(‘#stuTabs’)

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 3.对于已经存在的Tab选项,被点击时应默认被选中 4.点击菜单,能够访问对应的页面,而非文字内容

  • 注意:js文件中不支持el表达式

*/

$(‘#stuMenu’)

.tree(

{

url : $(“#ctx”).val() + ‘/menu.action?methodName=tree’,

onClick : function(node) {

// 判断当前选项卡是否存在

var exists = $(‘#stuTabs’)

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-lqv6wtrB-1715346120139)]

[外链图片转存中…(img-TKM12lI7-1715346120139)]

[外链图片转存中…(img-9ieMTTOx-1715346120139)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值