使用< c:forEach>使用HashMap [英] Use <c:forEach> with HashMap

查看:140
本文介绍了使用< c:forEach>使用HashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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



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



我在想:

 < c:forEach var =typeitems =$ {types}> 
...
< / c:forEach>

或者,如果无法完成,如何制作自定义标签来处理此事? / p>

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



谢谢,
Jonas。

解决方案

是的,这完全可以接受。



使用< c:forEach> 迭代地图,迭代中的每个项目都是 Map.Entry 的实例。所以给出你的例子:

 < c:forEach var =typeitems =$ {types}> 
Key为$ {type.key}
价值为$ {type.value}
< / c:forEach>


I have a java class that sets an servlet attribute to a HashMap object:

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

where request is an HttpServletRequest Object, and da.getSecurityTypes() returns a HashMap Object.

Is there a way to go through the HashMap collection using c:foreach or some other JSTL tags?

I was thinking:

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

Or if it can't be done, how would one make a custom tag to process this?

Resorting to Java code in my JSP page is be my last resort and I'd like to know if this is possible with JSTL.

Thanks, Jonas.

解决方案

Yes, this is perfectly acceptable.

When you use <c:forEach> to iterate over a Map, each item in the iteration is an instance of Map.Entry. So given your example:

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

这篇关于使用&lt; c:forEach&gt;使用HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆