问题描述
我想建立如图所示的层次结构,但是做出来JSL遍历不出来。我是通过grandParent.add(headerNavListGrandParent);设置一级层,再headerNavListParent.setParent(headerNavListGrandParent);headerNavListGrandParent.getChild().add(headerNavListParent);设置二级层headerNavListChild.setParent(headerNavListParent);headerNavListParent.getChild().add(headerNavListChild);这是第三层,headerNavListChild.getChild().add(headerNavListNephew);headerNavListNephew.setParent(headerNavListChild);这是最后一层,编译能通过,但是不能遍历求解!源码:publicList<HeaderVO>getHeaderNav(){List<HeaderVO>headerNavList=newArrayList<HeaderVO>();DBConnectionDBC=newDBConnection();HeaderVOmyHeaderNav=null;Stringsql="select*fromTest";ResultSetrs;try{rs=DBC.executeQuery(sql);while(rs.next()){intid=rs.getInt("id");intpId=rs.getInt("pId");StringmyHeaderNavName=rs.getString("navName");myHeaderNav=newHeaderVO();myHeaderNav.setId(id);myHeaderNav.setpId(pId);myHeaderNav.setMyTmallName(myHeaderNavName);HeaderVOgrandParent=newHeaderVO();grandParent.setId(pId);myHeaderNav.setParent(grandParent);HeaderVOparent=newHeaderVO();parent.setId(pId);myHeaderNav.setParent(parent);HeaderVOnephew=newHeaderVO();nephew.setId(pId);myHeaderNav.setParent(nephew);headerNavList.add(myHeaderNav);}}catch(Exceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}finally{DBC.close();}returnwrapHeaderNavList(headerNavList);privateList<HeaderVO>wrapHeaderNavList(List<HeaderVO>headerNavList){List<HeaderVO>grandParent=newArrayList<HeaderVO>();if(headerNavList!=null&&headerNavList.size()>0){for(HeaderVOheaderNavListGrandParent:headerNavList){if(headerNavListGrandParent.getParent().getId()==0){grandParent.add(headerNavListGrandParent);for(HeaderVOheaderNavListParent:headerNavList){if(headerNavListParent.getParent().getId()==headerNavListGrandParent.getId()){headerNavListParent.setParent(headerNavListGrandParent);headerNavListGrandParent.getChild().add(headerNavListParent);for(HeaderVOheaderNavListChild:headerNavList){if(headerNavListChild.getParent().getId()==headerNavListParent.getId()){headerNavListChild.setParent(headerNavListParent);headerNavListParent.getChild().add(headerNavListChild);for(HeaderVOheaderNavListNephew:headerNavList){if(headerNavListNephew.getParent().getId()==headerNavListChild.getId()){headerNavListChild.getChild().add(headerNavListNephew);headerNavListNephew.setParent(headerNavListChild);}}}}}}}}}returngrandParent;}这是VO;publicclassHeaderVO{privateintid;privateintpId;privateStringmyTmallName;privateHeaderVOparent;privateList<HeaderVO>child=newArrayList<HeaderVO>();publicList<HeaderVO>getChild(){returnchild;}publicvoidsetChild(List<HeaderVO>child){this.child=child;}publicHeaderVOgetParent(){returnparent;}publicvoidsetParent(HeaderVOparent){this.parent=parent;}publicintgetId(){returnid;}publicvoidsetId(intid){this.id=id;}publicintgetpId(){returnpId;}publicvoidsetpId(intpId){this.pId=pId;}publicStringgetMyTmallName(){returnmyTmallName;}publicvoidsetMyTmallName(StringmyTmallName){this.myTmallName=myTmallName;}}
解决方案
解决方案二:
补图
解决方案三:
好长。排版一下撒。你这是想要jstl嵌套遍历吗?怎么没有看到jstl遍历标签代码?
解决方案四:
VO类:packageTmall.com.VO;importjava.util.ArrayList;importjava.util.List;publicclassHeaderVO{privateintid;privateintpId;privateStringmyTmallName;privateHeaderVOparent;privateList<HeaderVO>child=newArrayList<HeaderVO>();publicList<HeaderVO>getChild(){returnchild;}publicvoidsetChild(List<HeaderVO>child){this.child=child;}publicHeaderVOgetParent(){returnparent;}publicvoidsetParent(HeaderVOparent){this.parent=parent;}publicintgetId(){returnid;}publicvoidsetId(intid){this.id=id;}publicintgetpId(){returnpId;}publicvoidsetpId(intpId){this.pId=pId;}publicStringgetMyTmallName(){returnmyTmallName;}publicvoidsetMyTmallName(StringmyTmallName){this.myTmallName=myTmallName;}}
publicList<HeaderVO>getHeaderNav(){List<HeaderVO>headerNavList=newArrayList<HeaderVO>();DBConnectionDBC=newDBConnection();HeaderVOmyHeaderNav=null;Stringsql="select*fromTest";ResultSetrs;try{rs=DBC.executeQuery(sql);while(rs.next()){intid=rs.getInt("id");intpId=rs.getInt("pId");StringmyHeaderNavName=rs.getString("navName");myHeaderNav=newHeaderVO();myHeaderNav.setId(id);myHeaderNav.setpId(pId);myHeaderNav.setMyTmallName(myHeaderNavName);HeaderVOgrandParent=newHeaderVO();grandParent.setId(pId);myHeaderNav.setParent(grandParent);HeaderVOparent=newHeaderVO();parent.setId(pId);myHeaderNav.setParent(parent);HeaderVOnephew=newHeaderVO();nephew.setId(pId);myHeaderNav.setParent(nephew);headerNavList.add(myHeaderNav);}}catch(Exceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}finally{DBC.close();}returnwrapHeaderNavList(headerNavList);}privateList<HeaderVO>wrapHeaderNavList(List<HeaderVO>headerNavList){List<HeaderVO>grandParent=newArrayList<HeaderVO>();if(headerNavList!=null&&headerNavList.size()>0){for(HeaderVOheaderNavListGrandParent:headerNavList){if(headerNavListGrandParent.getParent().getId()==0){grandParent.add(headerNavListGrandParent);for(HeaderVOheaderNavListParent:headerNavList){if(headerNavListParent.getParent().getId()==headerNavListGrandParent.getId()){headerNavListParent.setParent(headerNavListGrandParent);headerNavListGrandParent.getChild().add(headerNavListParent);for(HeaderVOheaderNavListChild:headerNavList){if(headerNavListChild.getParent().getId()==headerNavListParent.getId()){headerNavListChild.setParent(headerNavListParent);headerNavListParent.getChild().add(headerNavListChild);for(HeaderVOheaderNavListNephew:headerNavList){if(headerNavListNephew.getParent().getId()==headerNavListChild.getId()){headerNavListChild.getChild().add(headerNavListNephew);headerNavListNephew.setParent(headerNavListChild);}}}}}}}}}returngrandParent;}
JSTL:<c:forEachitems="${myHeaderNav}"var="myHeaderNavs"><i><imgsrc="IMG/nav.png"></i><ahref="#">${myHeaderNavs.myTmallName}</a><b></b><divclass="headerNavDiv"><c:forEachitems="${myHeaderNavs.parent}"var="myHeaderNavParents"><divclass="headerNavDivListLeft"><p>${myHeaderNavParents.myTmallName}</p><c:forEachitems="${myHeaderNavParents.child}"var="myHeaderNavParentChilds"><olclass="headerleft"><c:forEachitems="${myHeaderNavParentChilds.nephew}"var="myHeaderNavnephews"><li><ahref="#">${myHeaderNavnephews.myTmallName}</a></li></c:forEach></ol></c:forEach><divclass="block"></div></div></c:forEach></c:forEach>
解决方案五:
你有把对象setAttribute()吗?我怎么没看到。你这是放哪个作用域了?
解决方案六:
你能把你使用的框架配置文件信息给看吗?"${myHeaderNavs.parent}"你这个得到是一个对象?还是一个属性?JSL是什么东西?你说的是JSTL吧
解决方案七:
importjava.util.*;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.http.*;importTmall.com.DAO.HeaderDAO;importTmall.com.VO.HeaderVO;publicclassIndexServletextendsHttpServlet{@OverrideprotectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{//TODOAuto-generatedmethodstubthis.doPost(req,resp);}@OverrideprotectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{//TODOAuto-generatedmethodstubHeaderDAOheaderDAO=newHeaderDAO();List<HeaderVO>myTmall=headerDAO.getMyTmall();req.setAttribute("myTmall",myTmall);List<HeaderVO>myCollection=headerDAO.getCollection();req.setAttribute("myCollection",myCollection);List<HeaderVO>myHeaderNav=headerDAO.getHeaderNav();req.setAttribute("myHeaderNav",myHeaderNav);req.getRequestDispatcher("/index.jsp").forward(req,resp);}}