This question already has answers here:
How to loop through a HashMap in JSP?

(3个答案)


5年前关闭。




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

最佳答案

试试这个,

假设我的MAP是:

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>

希望这可以帮助 !

10-08 20:15