简体   繁体   中英

Key and Value has ArrayList in Map by Using JSTL

i have a

Map<ArrayList<String>, ArrayList<String> myMap = new HashMap<ArrayList<String>,ArrayList<String>>();

List<String> list1 = new ArrayList<String>();
list1.add("Administrator");
list1.add("Lookup Configuration");

List<String> list2 = new ArrayList<String>();
list2.add("User Creation");
list2.add("Branch Creation");
list2.add("Country");
list2.add("Language"):

the above is dummy data, i am creating menu management like this

Administrator (MenuName) --User Creation (item1) --Branch Creation (item2) Lookup Creation (MenuName) --Country (item1) --Currency (item2)

i am writing jstl like this

Map,ArrayList> myMap = new LinkedHashMap,ArrayList>();

and i am doing like this

<c:forEach items="${mainMenu}" var="myMenu">
  <c:forEach items="${myMenu.key}" var="menuName" varStatus="loop">
    <li id="lookup" class="mail"><a href="#lookup">${menuName}<span>26</span></a>   
    <ul class="sub-menu">
      <c:forEach items="${myMenu.value}" var="items" varStatus="loop">
         <li><a href="#"><em>02</em>${items.itemName}<span>14</span></a></li>
      </c:forEach>                                            
    </ul>                   
    </li>
  </c:forEach>                                                                         
</c:forEach>

i am getting key perfect, but i am struck the value

and the values are not iterating the realted key any help would be apreciated

Regards Pradeep

Are you using the correct data structure?

Do you mean:

    Administrator
      --User Creation
      --Branch Creation
    Lookup Configuration
      --Country
      --Currency

Wouldn't this be a map of lists? : Map<String,List<String>>

Then you could have a nested loop where you iterate over each key and then over that key's values.

I am not sure, but maybe you want something like this:

<%
Map<String, List<String>> myMap = new LinkedHashMap<String,List<String>>();
request.setAttribute("mainMenu", myMap);
List<String> adminItemsList = new ArrayList<String>();
adminItemsList.add("User Creation");
adminItemsList.add("Branch Creation");

List<String> lookupItemsList = new ArrayList<String>();
lookupItemsList.add("Country");
lookupItemsList.add("Language");

myMap.put("Administrator", adminItemsList);
myMap.put("Lookup Configuration", lookupItemsList);
%>

<c:forEach items="${mainMenu}" var="myMenu">
    <li id="lookup" class="mail"><a href="#lookup">${myMenu.key}<span>26</span></a>   
    <ul class="sub-menu">
    <c:forEach items="${myMenu.value}" var="item" varStatus="loop">
         <li><a href="#"><em>02</em>_${item}_<span>14</span></a></li>
    </c:forEach>                                            
    </ul>                   
    </li>
</c:forEach>

out:

Administrator26

  • 02_User Creation_14
  • 02_Branch Creation_14

Lookup Configuration26

  • 02_Country_14
  • 02_Language_14

I used LinkedHashMap to remember order of keys i putted in Map.

its really helpfull your both posts, thank you very very much i got the solution on both of your answers

here it is

my map is Map<String,List<String> myMenu = new HashMap<String, List<String>>();

<c:forEach items="${myMenu}" var="menuName" varStatus="loop">
   <li id="lookup" class="mail"><a href="#lookup">${menuName.key}</a>   
     <ul class="sub-menu">                                      
       <c:forEach items="${menuName.value}" var="item" varStatus="loop">
          <li><a href="${item.pageURL}">${item.itemName}</a></li>
       </c:forEach>
     </ul>                  
   </li>
</c:forEach>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM