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

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<XTreeGrid> formatTree(List<ShiroPermissions> permissions) {
        List<XTreeGrid> menuList = new ArrayList<XTreeGrid>();
        for (ShiroPermissions ps:permissions) {
            if(ps.getPermParentCode().equals("0")){ //遍历所有一级节点,并找出所有一级节点下的所有子节点
                XTreeGrid grid = getXTreeGrid(ps, permissions);
                menuList.add(grid);
            }
        }
        return menuList;
    }
 
    /**
     * 递归查找子菜单
     */
    public static List<XTreeGrid> getChild(String id, List<ShiroPermissions> rootMenu) {
        List<XTreeGrid> 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<ShiroPermissions> 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<ShiroPermissions> permissions = shiroPermissionsService.queryPermissionsAll();
        List<XTreeGrid> 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<XTreeGrid> 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<XTreeGrid> getData() {
        return data;
    }
 
    public void setData(List<XTreeGrid> data) {
        this.data = data;
    }
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
无限Java归) 2007-02-08 10:26 这几天,用java写了一个无限极的归写的,可能代码不够简洁,性能不够好,不过也算是练习,这几天再不断改进。前面几个小图标的判断,搞死我了。 package com.nickol.servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.nickol.utility.DB; public class category extends HttpServlet { /** * The doGet method of the servlet. * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out .println(""); out.println(""); out.println(" Category" + "" + "body{font-size:12px;}" + "" + "" + ""); out.println(" "); out.println(showCategory(0,0,new ArrayList(),"0")); out.println(" "); out.println(""); out.flush(); out.close(); } public String showCategory(int i,int n,ArrayList frontIcon,String countCurrent){ int countChild = 0; n++; String webContent = new String(); ArrayList temp = new ArrayList(); try{ Connection conn = DB.GetConn(); PreparedStatement ps = DB.GetPs("select * from category where pid = ?", conn); ps.setInt(1, i); ResultSet rs = DB.GetRs(ps); if(n==1){ if(rs.next()){ webContent += "";//插入结尾的减号 temp.add(new Integer(0)); } webContent += " ";//插入站点图标 webContent += rs.getString("cname"); webContent += "\n"; webContent += showCategory(Integer.parseInt(rs.getString("cid")),n,temp,"0"); } if(n==2){ webContent += "\n"; }else{ webContent += "\n"; } while(rs.next()){ for(int k=0;k<frontIcon.size();k++){ int iconStatic = ((Integer)frontIcon.get(k)).intValue(); if(iconStatic == 0){ webContent += "";//插入空白 }else if(iconStatic == 1){ webContent += "";//插入竖线 } } if(rs.isLast()){ if(checkChild(Integer.parseInt(rs.getString("cid")))){ webContent += "";//插入结尾的减号 temp = (ArrayList)frontIcon.clone(); temp.add(new Integer(0)); }else{ webContent += "";//插入结尾的直角 } }else{ if(checkChild(Integer.parseInt(rs.getString("cid")))){ webContent += "";//插入未结尾的减号 temp = (ArrayList)frontIcon.clone(); temp.add(new Integer(1)); }else{ webContent += "";//插入三叉线 } } if(checkChild(Integer.parseInt(rs.getString("cid")))){ webContent += " ";//插入文件夹图标 }else{ webContent += " ";//插入文件图标 } webContent += rs.getString("cname"); webContent += "\n"; webContent += showCategory(Integer.parseInt(rs.getString("cid")),n,temp,countCurrent+countChild); countChild++; } webContent += "\n"; DB.CloseRs(rs); DB.ClosePs(ps); DB.CloseConn(conn); }catch(Exception e){ e.printStackTrace(); } return webContent; } public boolean checkChild(int i){ boolean child = false; try{ Connection conn = DB.GetConn(); PreparedStatement ps = DB.GetPs("select * from category where pid = ?", conn); ps.setInt(1, i); ResultSet rs = DB.GetRs(ps); if(rs.next()){ child = true; } DB.CloseRs(rs); DB.ClosePs(ps); DB.CloseConn(conn); }catch(Exception e){ e.printStackTrace(); } return child; } } --------------------------------------------------------------------- tree.js文件 function changeState(countCurrent,countChild){ var object = document.getElementById("level" + countCurrent + countChild); if(object.style.display=='none'){ object.style.display='block'; }else{ object.style.display='none'; } var cursor = document.getElementById("cursor" + countCurrent + countChild); if(cursor.src.indexOf("images/tree_minus.gif")>=0) {cursor.src="images/tree_plus.gif";} else if(cursor.src.indexOf("images/tree_minusbottom.gif")>=0) {cursor.src="images/tree_plusbottom.gif";} else if(cursor.src.indexOf("images/tree_plus.gif")>=0) {cursor.src="images/tree_minus.gif";} else {cursor.src="images/tree_minusbottom.gif";} var folder = document.getElementById("folder" + countCurrent + countChild); if(folder.src.indexOf("images/icon_folder_channel_normal.gif")>=0){ folder.src = "images/icon_folder_channel_open.gif"; }else{ folder.src = "images/icon_folder_channel_normal.gif"; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值