简体   繁体   English

键和值通过使用JSTL在Map中具有ArrayList

[英]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) 管理员(MenuName) - 用户创建(第1项) - 分支创建(第2项)查找创建(MenuName) - 国家(第1项) - 货币(第2项)

i am writing jstl like this 我正在写这样的jstl

Map,ArrayList> myMap = new LinkedHashMap,ArrayList>(); 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>> :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 Administrator26

  • 02_User Creation_14 02_User Creation_14
  • 02_Branch Creation_14 02_Branch Creation_14

Lookup Configuration26 查找配置26

  • 02_Country_14 02_Country_14
  • 02_Language_14 02_Language_14

I used LinkedHashMap to remember order of keys i putted in Map. 我使用LinkedHashMap来记住我在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>>(); 我的地图是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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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