【问题标题】:Unable to get Map<Integer, String> values in JSTL Loop [duplicate]无法在 JSTL 循环中获取 Map<Integer, String> 值 [重复]
【发布时间】:2015-11-12 12:26:33
【问题描述】:

我正在尝试获取在 JSP 页面中呈现的 Servlet 中设置的 Map 值,如下所述:

Servlet 代码

   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 代码

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

问题是我没有从这个循环中获取 Map 值,我可以在 Servlet 中看到带有 System.out 的值,但我认为 '${item.id}' 值没有正确传递,这就是 Map 没有返回的原因任何值。

有人可以请在这里指导我吗?如果需要更多解释或澄清,请告诉我。

谢谢!

【问题讨论】:

    标签: java jsp servlets jstl


    【解决方案1】:

    尝试使用这个:-

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

    【讨论】:

    • 你太棒了!!它确实有效! :-D 我不明白它有什么区别,但它的工作原理很好,当我搜索相关问题时,人们说 EL 将 Key 传递为 Long 而不是整数,但我不知道'认为这不是问题所在。 .谢谢!
    猜你喜欢
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    • 2023-03-26
    • 2015-10-03
    • 1970-01-01
    • 2019-12-06
    • 2020-03-28
    • 2019-12-24
    相关资源
    最近更新 更多