Loop through a Map with JSTL


Map Problem Overview

I'm looking to have JSTL loop through a Map<String, String> and output the value of the key and it's value.

For example I have a Map<String, String> which can have any number of entries, i'd like to loop through this map using JSTL and output both the key and it's value.

I know how to access the value using the key, ${myMap['keystring']}, but how do I access the key?

Map Solutions

Solution 1 - Map

Like this:

<c:forEach var="entry" items="${myMap}">
  Key: <c:out value="${entry.key}"/>
  Value: <c:out value="${entry.value}"/>

Solution 2 - Map

You can loop through a hash map like this

ArrayList list = new ArrayList();
TreeMap itemList=new TreeMap();
itemList.put("test", "test");
pageContext.setAttribute("itemList", list);                            

  <c:forEach items="${itemList}" var="itemrow">
   <input  type="text"  value="<c:out value='${itemrow.test}'/>"/>

For more JSTL functionality look here


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDeanView Question on Stackoverflow
Solution 1 - MapcletusView Answer on Stackoverflow
Solution 2 - MapsayanView Answer on Stackoverflow