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

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

问题描述

在我的 Spring MVC 应用程序中,我从我的 controllerServlet 返回了 HashMap.现在我需要使用 JSTL 在我的 jsp 中打印它.请帮忙解决这个问题.我对这一切都很陌生.

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.

推荐答案

试试这个,

假设我的地图是:-

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);

所以在 JSP 中,

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

希望这有帮助!

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

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