java jsp 树_jsp 树形结构

packagecom.ytkj.rose.util;importorg.springframework.util.StringUtils;importjavax.validation.constraints.NotNull;importjava.lang.reflect.Field;importjava.util.ArrayList;importjava.util.Collection;importjava.util.HashSet;importjava.util.Set;public classTreeUtils {/*** 集合转树结构

*

*@paramcollection 目标集合

*@paramclazz 集合元素类型

*@return转换后的树形结构*/

public static Collection toTree(@NotNull Collection collection, @NotNull Classclazz) {return toTree(collection, null, null, null, clazz);

}/*** 集合转树结构

*

*@paramcollection 目标集合

*@paramid 节点编号字段名称

*@paramparent 父节点编号字段名称

*@paramchildren 子节点集合属性名称

*@paramclazz 集合元素类型

*@return转换后的树形结构*/

public static Collection toTree(@NotNull Collection collection, String id, String parent, String children, @NotNull Classclazz) {try{if (collection == null || collection.isEmpty()) return null;//如果目标集合为空,直接返回一个空树

if (StringUtils.isEmpty(id)) id = "id"; //如果被依赖字段名称为空则默认为id

if (StringUtils.isEmpty(parent)) parent = "parent"; //如果依赖字段为空则默认为parent

if (StringUtils.isEmpty(children)) children = "children"; //如果子节点集合属性名称为空则默认为children//初始化根节点集合, 支持 Set 和 List

Collectionroots;if (collection.getClass().isAssignableFrom(Set.class)) {

roots= new HashSet<>();

}else{

roots= new ArrayList<>();

}//获取 id 字段, 从当前对象或其父类

Field idField;try{

idField=clazz.getDeclaredField(id);

}catch(NoSuchFieldException e1) {

idField=clazz.getSuperclass().getDeclaredField(id);

}//获取 parentId 字段, 从当前对象或其父类

Field parentField;try{

parentField=clazz.getDeclaredField(parent);

}catch(NoSuchFieldException e1) {

parentField=clazz.getSuperclass().getDeclaredField(parent);

}//获取 children 字段, 从当前对象或其父类

Field childrenField;try{

childrenField=clazz.getDeclaredField(children);

}catch(NoSuchFieldException e1) {

childrenField=clazz.getSuperclass().getDeclaredField(children);

}//设置为可访问

idField.setAccessible(true);

parentField.setAccessible(true);

childrenField.setAccessible(true);//找出所有的根节点

for(T c : collection) {

Object parentId=parentField.get(c);if(isRootNode(parentId)) {

roots.add(c);

}

}//从目标集合移除所有根节点

collection.removeAll(roots);//遍历根节点, 依次添加子节点

for(T root : roots) {

addChild(root, collection, idField, parentField, childrenField);

}//关闭可访问

idField.setAccessible(false);

parentField.setAccessible(false);

childrenField.setAccessible(false);returnroots;

}catch(Exception e) {

e.printStackTrace();throw newRuntimeException(e);

}

}/*** 为目标节点添加孩子节点

*

*@paramnode 目标节点

*@paramcollection 目标集合

*@paramidField ID 字段

*@paramparentField 父节点字段

*@paramchildrenField 字节点字段*/

private static void addChild(@NotNull T node, @NotNull Collection collection, @NotNull Field idField, @NotNull Field parentField, @NotNull Field childrenField) throwsIllegalAccessException {

Object id=idField.get(node);

Collection children = (Collection) childrenField.get(node);//如果子节点的集合为 null, 初始化孩子集合

if (children == null) {if (collection.getClass().isAssignableFrom(Set.class)) {

children= new HashSet<>();

}else children = new ArrayList<>();

}for(T t : collection) {

Object o=parentField.get(t);if(id.equals(o)) {//将当前节点添加到目标节点的孩子节点

children.add(t);//重设目标节点的孩子节点集合,这里必须重设,因为如果目标节点的孩子节点是null的话,这样是没有地址的,就会造成数据丢失,所以必须重设,如果目标节点所在类的孩子节点初始化为一个空集合,而不是null,则可以不需要这一步,因为java一切皆指针

childrenField.set(node, children);//递归添加孩子节点

addChild(t, collection, idField, parentField, childrenField);

}

}

}/*** 判断是否是根节点, 判断方式为: 父节点编号为空或为 0, 则认为是根节点. 此处的判断应根据自己的业务数据而定.

*@paramparentId 父节点编号

*@return是否是根节点*/

private static booleanisRootNode(Object parentId) {boolean flag = false;if (parentId == null) {

flag= true;

}else if (parentId instanceof String && (StringUtils.isEmpty(parentId) || parentId.equals("0"))) {

flag= true;

}else if (parentId instanceof Integer && Integer.valueOf(0).equals(parentId)) {

flag= true;

}returnflag;

}

}

javax.validation

validation-api

2.0.1.Final

bean:

package com.ytkj.rose.bean;

import java.util.ArrayList;

import java.util.List;

public class TMenu {

private Integer id;

private Integer pid;

private String name;

private String icon;

private String url;

private List 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 getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

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 List getChildren() {

return children;

}

public void setChildren(List children) {

this.children = children;

}

@Override

public String toString() {

return "TMenu{" +

"id=" + id +

", pid=" + pid +

", name='" + name + '\'' +

", icon='" + icon + '\'' +

", url='" + url + '\'' +

", children=" + children +

'}';

}

}

controller:

@RequestMapping("/main")publicString main(HttpSession session) {

logger.debug("跳转到主面");

List menuList=tMenuService.getMenuAll();

session.setAttribute("menuList",menuList);return "main";

}

service:

package com.ytkj.rose.service;

import com.ytkj.rose.bean.TMenu;

import java.util.List;

public interface TMenuService {

List getMenuAll();

}

serviceImpl

package com.ytkj.rose.service.impl;

import com.ytkj.rose.bean.TMenu;

import com.ytkj.rose.mapper.TMenuMapper;

import com.ytkj.rose.service.TMenuService;

import com.ytkj.rose.util.TreeUtils;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.*;

@Service

public class TMenuServiceImpl implements TMenuService {

Logger logger= LoggerFactory.getLogger(TAdminServiceImpl.class);

@Autowired

TMenuMapper tMenuMapper;

@Override

public List getMenuAll() {

List list = tMenuMapper.selectAll();

Collection tMenus = TreeUtils.toTree(list, "id", "pid", "children", TMenu.class);

logger.debug("菜单={}",tMenus);

return (List) tMenus;

}

}

mapper:

package com.ytkj.rose.mapper;

import com.ytkj.rose.bean.TMenu;

import java.util.List;

public interface TMenuMapper {

List selectAll();

}

mapper.xml

/p>

PUBLIC"-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select*from t_menu

jsp页面:

Created by IntelliJ IDEA.

User: 3600X

Date: 2020/2/29

Time: 14:53

To change this template use File | Settings | File Templates.

--%>

页面效果:

ca4263497a9a823659e94c598bbbda5e.png

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页