Loop through a Map with JSTL

Dean picture Dean · May 17, 2010 · Viewed 105.3k times · Source

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?

Answer

cletus picture cletus · May 17, 2010

Like this:

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