开发者

Loop through a Map with JSTL [duplicate]

开发者 https://www.devze.com 2022-12-31 13:12 出处:网络
This question already has answers here: 开发者_运维问答 How to loop through a HashMap in JSP? (3 answers)
This question already has answers here: 开发者_运维问答 How to loop through a HashMap in JSP? (3 answers) Closed 7 years ago.

I'm looking to have JSTL loop through a Map<String, String> and output the value of the key and it's value.

For example I have a Map<String, String> which can have any number of entries, i'd like to loop through this map using JSTL and output both the key and it's value.

I know how to access the value using the key, ${myMap['keystring']}, but how do I access the key?


Like this:

<c:forEach var="entry" items="${myMap}">
  Key: <c:out value="${entry.key}"/>
  Value: <c:out value="${entry.value}"/>
</c:forEach>


You can loop through a hash map like this

<%
ArrayList list = new ArrayList();
TreeMap itemList=new TreeMap();
itemList.put("test", "test");
list.add(itemList);
pageContext.setAttribute("itemList", list);                            
%>

  <c:forEach items="${itemList}" var="itemrow">
   <input  type="text"  value="<c:out value='${itemrow.test}'/>"/>
  </c:forEach>               

For more JSTL functionality look here

0

精彩评论

暂无评论...
验证码 换一张
取 消