我有一个将 servlet 属性设置为 HashMap 对象的 java 类:
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。
谢谢, 乔纳斯。
最佳答案
是的,这是完全可以接受的。
当您使用 <c:forEach>
时遍历 Map
,迭代中的每一项都是 Map.Entry
的一个实例.所以给出你的例子:
<c:forEach var="type" items="${types}">
Key is ${type.key}
Value is ${type.value}
</c:forEach>
关于java - 使用 <c :forEach> with HashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2210907/