0

I am trying to get the Map values set up in a Servlet rendered in a JSP page as explained below:

Servlet Code

   Map<Integer, String> anotherItemMap = new HashMap<>();
   for (Item item : itemList) {
       anotherItemMap.put(item.getId(), "someValue");
   }

   request.setAttribute("itemList", itemList);
   request.setAttribute("anotherItemMap", anotherItemMap);

   request.getRequestDispatcher(forwardToAddress).forward(request, response);

JSP Code

   <c:forEach var="item" items="${itemList}">
       <h4><c:out value="${anotherItemMap['${item.id}']}" /></h4>
   </c:forEach>

Problem is I am not getting the Map<> Values from this loop, I can see the values in Servlet with System.out but I think the '${item.id}' value is not getting passed properly and this is why Map is not returning any value.

Could anyone please please guide me here? Let me know if any more explanation or clarification needed.

Thanks!

Felix Gerber
  • 1,615
  • 3
  • 30
  • 40
Asif
  • 4,980
  • 8
  • 38
  • 53

1 Answers1

1

Try using this :-

<c:forEach var="item" items="${itemList}">
   <c:set var="id" value="${item.id}"/>
   <h4><c:out value="${anotherItemMap[id]}"/></h4>
</c:forEach>
karim mohsen
  • 2,164
  • 1
  • 15
  • 19
  • You are awesome!! It did work!! :-D I don't understand what difference it makes but good that it works, when I was searching for related questions people were saying that EL pass the *Key* as `Long` rather than Integer, but I don't think this was the problem. . Thank You! – Asif Nov 12 '15 at 13:01