小编典典

采用 与HashMap

jsp

我有一个Java类,将servlet属性设置为HashMap对象:

request.setAttribute("types", da.getSecurityTypes());

其中request是HttpServletRequest对象,并da.getSecurityTypes()返回HashMap对象。

有没有一种方法可以使用c:foreach或其他JSTL标签浏览HashMap集合?

我刚在想:

 <c:forEach var="type" items="${types}">
                 ...
     </c:forEach>

或者,如果无法完成,那么如何制作自定义标签来处理呢?

在我的JSP页面中诉诸Java代码是我的最后选择,我想知道JSTL是否可以实现。

谢谢,乔纳斯。


阅读 307

收藏
2020-06-08

共1个答案

小编典典

是的,这是完全可以接受的。

当您用于<c:forEach>迭代时,迭代中的Map每个项目都是的实例Map.Entry。所以给你的例子:

<c:forEach var="type" items="${types}">
   Key is ${type.key}
   Value is ${type.value}
</c:forEach>
2020-06-08