在HashMap中使用<c:forEach>

jon*_*ita 49 java jsp jstl

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

request.setAttribute("types", da.getSecurityTypes());
Run Code Online (Sandbox Code Playgroud)

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

有没有办法使用c:foreach或其他一些JSTL标签来浏览HashMap集合?

我刚在想:

 <c:forEach var="type" items="${types}">
                 ...
     </c:forEach>
Run Code Online (Sandbox Code Playgroud)

或者,如果无法完成,如何制作自定义标签来处理此问题?

在我的JSP页面中使用Java代码是我的最后手段,我想知道JSTL是否可以实现这一点.

谢谢,乔纳斯.

ska*_*man 112

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

当您使用<c:forEach>迭代a时Map,迭代中的每个项都是一个实例Map.Entry.所以举个例子:

<c:forEach var="type" items="${types}">
   Key is ${type.key}
   Value is ${type.value}
</c:forEach>
Run Code Online (Sandbox Code Playgroud)


dav*_*veb 5

它有效,你将拥有type.keytype.value在循环中玩.