在 servlet 中我有:
HashMap eventsByDayNo = new HashMap();
eventsByDayNo.put (new Integer(12), "day 12 info");
eventsByDayNo.put (new Integer(11), "day 11 info");
eventsByDayNo.put (new Integer(15), "day 15 info");
eventsByDayNo.put (new Integer(16), "day 16 info");
request.setAttribute("eventsByDayNo", eventsByDayNo);
request.setAttribute("daysInMonth", new Integer(31));
在 jsp 中我有:
<c:forEach var="dn" begin="1" end="${daysInMonth}" step="1" varStatus="status">
Day Number=<c:out value="${dn}" /> Value=<c:out value="${eventsByDayNo[dn]}" /><br>
</c:forEach>
上面的 JSTL 工作正常,但如果我尝试偏移日期数字 <c:out value="${eventsByDayNo[dn+3]}" />
没有一个 HashMap 条目不会被打印。有什么答案可以解释为什么不吗?
以上只是我实际应用程序的概念证明。
最佳答案
EL 中的数字(至少是整数)被隐式视为 Long
。所以替换你的 Map<Integer, String>
通过Map<Long, String>
它会起作用的。
关于java - JSTL 和 hashmap 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3919025/