如何使用JSTL forEach循环迭代HashMap? [英] How to iterate HashMap using JSTL forEach loop?

查看:88
本文介绍了如何使用JSTL forEach循环迭代HashMap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Spring MVC应用程序中,我有从我的controllerServlet返回的HashMap。现在我需要使用JSTL在我的jsp中打印。请帮助。我是新来的。

解决方案

试试这个,

假设我的MAP是: -

 地图< String,String> countryList = new HashMap< String,String>(); 
countryList.put(美国,华盛顿特区);
countryList.put(印度,德里);
countryList.put(德国,柏林);
countryList.put(法国,巴黎);
countryList.put(意大利,罗马);

request.setAttribute(capitalList,countryList);

所以在JSP中,

 < c:forEach var =countryitems =$ {capitalList}> 
国家:$ {country.key} - 资本:$ {country.value}
< / c:forEach>

希望这有助于您!


In my Spring MVC application i have HashMap returned from my controllerServlet. Now I need to print that in my jsp using JSTL. Please help on this. I'm new in all this.

解决方案

Try this,

suppose my MAP is :-

Map<String, String> countryList = new HashMap<String, String>();
countryList.put("United States", "Washington DC");
countryList.put("India", "Delhi");
countryList.put("Germany", "Berlin");
countryList.put("France", "Paris");
countryList.put("Italy", "Rome");

request.setAttribute("capitalList", countryList);

So in JSP ,

<c:forEach var="country" items="${capitalList}">
    Country: ${country.key}  - Capital: ${country.value}
</c:forEach>

Hope this helps !

这篇关于如何使用JSTL forEach循环迭代HashMap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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