javaweb递归调用封装树型结构菜单树(无限层级)

1.先放一张最终结果图片,如果你认为这主是你要的结果,那请把接着往下看代码 

 

 

 

2.代码为封装成了一个工具类,有需要的小伙伴直接control+c吧

package com.sunkee.business.admin.common.utils;

import com.sunkee.business.admin.common.domain.XTreeGrid;
import com.sunkee.business.admin.common.model.ShiroPermissions;
import com.sunkee.business.make.utils.StringUtil;
import net.sf.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

/**
 * ☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆
 * @developers  LONGZHIQIANG
 * @createtime  2018/11/2 10:36.
 * @describe    用于封装树型结构树(无限层级)
 * ☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆
 */
public class XTreeKit {

    public static List formatTree(List permissions) {
        List menuList = new ArrayList();
        for (ShiroPermissions ps:permissions) {
            if(ps.getPermParentCode().equals("0")){ //遍历所有一级节点,并找出所有一级节点下的所有子节点
                XTreeGrid grid = getXTreeGrid(ps, permissions);
                menuList.add(grid);
            }
        }
        return menuList;
    }

    /**
     * 递归查找子菜单
     */
    public static List getChild(String id, List rootMenu) {
        List childList = new ArrayList<>();
        for (ShiroPermissions root : rootMenu) {
            // 遍历所有节点,将父菜单编码与传过来的编码进行比较、若相同则继续查看该节点下是否还有子节点
            if (!StringUtil.isEmpty(root.getPermParentCode())) {
                if (root.getPermParentCode().equals(id)) {
                    XTreeGrid grid = getXTreeGrid(root, rootMenu);
                    childList.add(grid);
                }
            }
        }
        return childList;
    }

    //构建 XTreeGrid(实体对象)
    public static XTreeGrid getXTreeGrid(ShiroPermissions ps, List permissions){
        XTreeGrid x = new XTreeGrid();
        x.setId(ps.getPermId());
        x.setCode(ps.getPermCode());
        x.setParenCode(ps.getPermParentCode());
        x.setTitle(ps.getPermName());
        x.setValue(ps.getPermPermission());
        x.setData(getChild(ps.getPermCode(),permissions));  //递增遍历所有子节点(无限层级)
        return x;
    }
}

3.直接调用

  /**
     * ☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆
     * @developers  LONGZHIQIANG
     * @createtime  2018/10/31 17:56.
     * @describe    前往添加角色页面
     * ☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆
     */
    @RequiresPermissions(PER_ADD)
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(Model model) throws Exception{
        List permissions = shiroPermissionsService.queryPermissionsAll();
        List grid = XTreeKit.formatTree(permissions);
        model.addAttribute("permissions",JSONArray.fromObject(grid));
        return TABLE_VIEW+"/role-add";
    }

4.java实体类

package com.sunkee.business.admin.common.domain;

import java.io.Serializable;
import java.util.List;
 
/**
 * xtree树形表格基础对象
 *
 */
public class XTreeGrid implements Serializable{
 
	private static final long serialVersionUID = -9189631784252440402L;
	
	public Integer id;					//节点id(记录主健)并不是上下级的ID

	public String code;					//节点code (元素本身编码用于上下级关连关系)

	public String parenCode;		    //节点父Code(与节点code区分上下级关系)
	
	public String title;				// 显示的值 (必填 xTree插件中所需属性)
	
	public String value;				// 隐藏的值 (必填 xTree插件中所需属性)
	
	public Boolean checked = false;	    //默认是否选中(选填 xTree插件中所需属性) true为选中,false与不填都为不选中
	
	public Boolean disabled = false;    //是否可用   (选填 xTree插件中所需属性)   true为不可用,false与不填都为可用
	
	public Boolean expanded = false;   //是否展开    (选填 xTree插件中所需属性)true为展开,默认为不展开
	
	public List data;		//孩子节点   (必填 xTree插件中所需属性 没子节点该值为空集合)
 
 
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getParenCode() {
		return parenCode;
	}

	public void setParenCode(String parenCode) {
		this.parenCode = parenCode;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public Boolean getChecked() {
		return checked;
	}

	public void setChecked(Boolean checked) {
		this.checked = checked;
	}

	public Boolean getDisabled() {
		return disabled;
	}

	public void setDisabled(Boolean disabled) {
		this.disabled = disabled;
	}

	public Boolean getExpanded() {
		return expanded;
	}

	public void setExpanded(Boolean expanded) {
		this.expanded = expanded;
	}

	public List getData() {
		return data;
	}

	public void setData(List data) {
		this.data = data;
	}
}

以上就是所有代码,是不是很简单啊

你可能感兴趣的:(java,jqueryt插件,javascript,layer,tomcat,spring-mvc)