嵌套循环列表映射JSTL

时间:2016-02-23 15:12:15

标签: java jsp jstl

我无法创建列表,我需要列表

  1. 男性
      • 凉鞋
    • 弹簧
  2. 女性
    • 弹簧
      • 靴子
    • 弹簧
  3. 但是我的代码

    之后
    1. 男性
        • 凉鞋
        • 靴子
      • 弹簧
    2. 女性
      • 弹簧
        • 凉鞋
        • 靴子
      • 弹簧
    3.     List<MainMenu> listmainmenu;
      List<MergeSubMenuPojo> listmergesubmenu;
      List<MergeSubSubMenuPojo> listmergesubsubmenu;
      Map<Long, List<MergeSubMenuPojo>> submainmenumap = new HashMap<Long, List<MergeSubMenuPojo>>();
      Map<Long, List<MergeSubSubMenuPojo>> ssmmmap = new HashMap<Long, List<MergeSubSubMenuPojo>>();
      Map<Long, Map<Long,List<MergeSubSubMenuPojo>>> subsubmainmenumap = new HashMap<Long, Map<Long,List<MergeSubSubMenuPojo>>>(); 
      Iterator<MainMenu> itmainmenu;
      Iterator<MergeSubMenuPojo> itsubmainmenu;
              listmainmenu = mainmenudao.findAll();
                  for (itmainmenu = listmainmenu.iterator(); itmainmenu.hasNext();) {
                          mainmenu = (MainMenu)itmainmenu.next();
                          listmergesubmenu = submainmenudao.findAllMerge(mainmenu.getId());
                          submainmenumap.put(mainmenu.getId(), listmergesubmenu);
      
                          for(itsubmainmenu = listmergesubmenu.iterator(); itsubmainmenu.hasNext();){
                              submainmenu = itsubmainmenu.next();
                              listmergesubsubmenu = subsubmainmenudao.findAllMerge(mainmenu.getId(),submainmenu.getId());
                              ssmmmap.put(submainmenu.getId(), listmergesubsubmenu);
                              subsubmainmenumap.put(mainmenu.getId(), ssmmmap);
      
                              modelAndView.addObject("listmainmenu",listmainmenu);
                              modelAndView.addObject("submainmenu",submainmenumap);
                              modelAndView.addObject("subsubmainmenu",subsubmainmenumap);
                          }
                      }
      
      <c:forEach var="mm" items="${listmainmenu}">
                  <li><input type="hidden" value="${mm.id}" class="m">
                      <a href="#"><c:out value="${mm.menuname}"/></a>
                      <ul>
                          <c:forEach var="smm" items="${submainmenu[mm.id]}">
                              <li><input type="hidden" value="${smm.id}">
                                  <a href="#">*<c:out value="${smm.submenuname}"/></a>
                                  <ul>
                                      <c:forEach var="ssmm" items="${subsubmainmenu[mm.id][smm.id]}">
                                              <li><input type="hidden" value="${ssmm.id}">
                                                  <a href="#">**<c:out value="${ssmm.subsubmenuname}"/></a>
                                              </li>
                                      </c:forEach>
                                  </ul>
                              </li>
                          </c:forEach>
                      </ul>
                  </li>
      

1 个答案:

答案 0 :(得分:0)

你正在为其他/或嵌套的forloops中的循环,所以你不需要像数组一样的情况,只需从当前的迭代变量中选择列表。在Shouting Code

上没有这样的情况
<c:forEach var="mm" items="${not empty listmainmenu}">
            <li><input type="hidden" value="${mm.id}" class="m">
                <a href="#"><c:out value="${mm.menuname}"/></a>
                <ul>
                    <c:forEach var="smm" items="${not empty mm.submainmenu}">
                        <li><input type="hidden" value="${smm.id}">
                            <a href="#">*<c:out value="${smm.submenuname}"/></a>
                            <ul>
                                <c:forEach var="ssmm" items="${not empty smm.subsubmainmenu}">
                                        <li><input type="hidden" value="${ssmm.id}">
                                            <a href="#">**<c:out value="${ssmm.subsubmenuname}"/></a>
                                        </li>
                                </c:forEach>
                            </ul>
                           </li>
                        </c:forEach>
                    </ul>
             </li>
<c:forEach>