欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java递归实现ztree树结构数据展示

程序员文章站 2022-03-26 17:13:06
//获得zTree结构的数据(测试AuthInfo)@RequestMapping("/getAuthInfoTree.action")public void getAuthInfoTree(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {System.out.println("===========getAuthInfoTree========....
//获得zTree结构的数据(测试AuthInfo)
		@RequestMapping("/getAuthInfoTree.action")
		public void getAuthInfoTree(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
			System.out.println("===========getAuthInfoTree============");
			//读取树型结构数据
			List<AuthInfo> list=new ArrayList<AuthInfo>();
			//1.查询pid为0的信息(*节点)(找出规律使用递归)
			//调用递归查询数据
			AuthInfo authInfo=new AuthInfo();
			authInfo.setAuthId(0);
			selectChildrenAuthInfo(authInfo);
			//最终结果(在AuthInfo中添加children集合)
			List<AuthInfo> authlist=authInfo.getChildren();
			for(AuthInfo au:authlist) {
				System.out.println("au="+au);
			}
			//list.add(authInfoList);
			//将list集合转为JSONArray
			JSONArray jsonArray=JSONArray.fromObject(authlist);
			request.setAttribute("testTree", jsonArray);
			request.getRequestDispatcher("pages/testTree.jsp").forward(request, response);
			
		}
		//递归查询方法
		/**
		 * 递归自己调用自己,一定要有跳出逻辑
		 * 方法调用时,参数之间有规律
		 * @param parent
		 */
		private void selectChildrenAuthInfo(AuthInfo parent) {
			List<AuthInfo>	childrenAuthInfoList=authInfoService.selectAuthInfoone(parent.getAuthId());
			//集合为空的时候跳出循环
			for(AuthInfo authInfo:childrenAuthInfoList) {
				selectChildrenAuthInfo(authInfo);
			}
			parent.setChildren(childrenAuthInfoList);
		}
public class AuthInfo {
	/**
	 * 为了获得zTree数据做如下操作
	 * 
	 */
	//树id
    private Integer authId;
	//树pid
    private Integer parentId;
    //树name
    private String authName;
    
    private String authDesc;

    private Integer authGrade;

    private String authType;
    //树url
    private String authUrl;

    private String authCode;

    private Integer authOrder;

    private String authState;

    private Integer createBy;

    private Date createTime;

    private Integer updateBy;

    private Date updateTime;
    //树open
    private boolean open;
    //树children
    private List<AuthInfo> children=new ArrayList<AuthInfo>();  
    //为了关联二级权限
    private List<AuthInfo> childrenAuth;
}

 

本文地址:https://blog.csdn.net/likun1239656678/article/details/110295585

相关标签: idea Java