user2183807
user2183807

Reputation: 21

Expression Language access a map value by key

I am iterating a list named listEvents (of type List<String>) in JSP using <c:forEach> tag. Inside the for loop I need to display a value from a HashMap<String,String>. The key for the hashmap will be the element in the list. Please find below the code snippet.

<c:forEach items="${listEvents}" var="listEvent" varStatus="eventCount">
     <c:out value="${eventMap[listEvent]}</
</c:forEach> 

When I try with the above code, I am getting

PropertyNotFoundException ["Key" property not found on java.lang.String].

How do I fix this?

Upvotes: 0

Views: 5665

Answers (1)

Uri Lukach
Uri Lukach

Reputation: 1121

This is the right way to do it:

         <c:forEach var="listEvent" items="${eventMap}" varStatus="eventCount">
                ${listEvent.value}
         </c:forEach>

To access the key add this line:

         ${listEvent.key}

Upvotes: 1

Related Questions