无限极树形结构

java类

public class Node {
    private int id;
    private String name;
    private int pid;

    public Node() {
    }

    public Node(int id, String name, int pid) {
        this.id = id;
        this.name = name;
        this.pid = pid;
    }
    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPid() {
        return pid;
    }

    public void setPid(int pid) {
        this.pid = pid;
    }
}

生成无线级结构

 public class Treeuser {
        public static Map mapArray = new LinkedHashMap();
        public List menuCommon;
        public List list = new ArrayList();
        private SimpleDateFormat dsf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
        public List menuList(List menu){
            this.menuCommon = menu;
            List nodeList = new ArrayList<>();
            for (Fuser x : menu) {
                Map mapArr = new LinkedHashMap();
                if(x.getfIntroUser_id() != null){
                    mapArr.put("name","账号:"+x.getFid()+",注册于:"+dsf.format( x.getFregisterTime()));
                    if (menuChild(x.getFid()).size()>0){
                        mapArr.put("open",true);
                        mapArr.put("children", menuChild(x.getFid()));
                    }
                    nodeList.add(mapArr);
                }else {
                    //说明是根节点
                    mapArr.put("name","账号:"+x.getFid()+",注册于:"+dsf.format( x.getFregisterTime()));
                    mapArr.put("open",true);
                    //根据根节点查找个所有子节点
                    mapArr.put("children", menuChild(x.getFid()));
                    nodeList.add(mapArr);
                }
            }
            return nodeList;
        }
        public List menuList(List menu,Integer id){
            this.menuCommon = menu;
            List nodeList = new ArrayList<>();
            for (Fuser x : menu) {
                if (x.getFid()==id){
                    Map mapArr = new LinkedHashMap();
                    if(x.getfIntroUser_id() != null){
                        mapArr.put("name","账号:"+x.getFid()+",注册于:"+dsf.format( x.getFregisterTime()));
                        if (menuChild(x.getFid()).size()>0){
                            mapArr.put("open",false);
                            mapArr.put("children", menuChild(x.getFid()));
                        }
                        nodeList.add(mapArr);
                    }else {
                        //说明是根节点
                        mapArr.put("name","账号:"+x.getFid()+",注册于:"+dsf.format( x.getFregisterTime()));
                        mapArr.put("open",false);
                        //根据根节点查找个所有子节点
                        mapArr.put("children", menuChild(x.getFid()));
                        nodeList.add(mapArr);
                    }
                }
            }
            return nodeList;
        }
        public List menuChild(Integer id){
            List lists = new ArrayList();
            for(Fuser a:menuCommon){
                Map childArray = new LinkedHashMap();
                if (a.getfIntroUser_id()!=null){
                    int fid = a.getfIntroUser_id().getFid();
                    if(fid==id){
                        childArray.put("name","账号:"+a.getFid()+",注册于:"+dsf.format( a.getFregisterTime()));
                        if (menuChild(a.getFid()).size()>0){
                            childArray.put("open",false);
                            childArray.put("children", menuChild(a.getFid()));
                        }
                        lists.add(childArray);
                    }
                }
            }
            return lists;
        }
    }
 控制层

@Controller
public class TreeController extends BaseController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private TreeService treeService;
    @ResponseBody
    @RequestMapping(value = "/ssadmin/TreeUser",produces = {JsonEncode})
    public ModelAndView getTree()throws Exception{
        JspPage modelAndView = new JspPage(request);
        modelAndView.setViewName("ssadmin/treeUser");
        String keywords = request.getParameter("keywords");
        JSONObject jsonObject = null;
        List nodes = null;
        try {
            jsonObject = new JSONObject();
            Treeuser treeuser = new Treeuser();
            //查询所有的用户
            List uesr = treeService.queryUser();
            if (StringUtils.isNotEmpty(keywords)) {
                modelAndView.addObject("keywords", keywords);
                nodes = treeuser.menuList(uesr,Integer.parseInt(keywords));
            }else {
                nodes = treeuser.menuList(uesr);
            }
            jsonObject.put("data",nodes);
            String s = jsonObject.toString();
            modelAndView.addObject("treeUser",s);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return modelAndView;
    }
}

jsp页面

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="comm/include.inc.jsp"%>
<%
	String treeUser = (String) request.getAttribute("treeUser");
	String substring = treeUser.substring(0, treeUser.length() - 1);
	String substring1 = substring.substring(8, substring.length());
%>

    你可能感兴趣的:(javaWeb)