Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate over elements of List and Map using JSTL <c:forEach> tag

If I have a JSF backing bean return an object of type ArrayList, I should be able to use <c:foreach> to iterate over the elements in the list. Each element contains a map and although the question of how to access the map content through JSTL has been answered here, if I pass an array of such maps, I can't find how to iterate over them and still access the map content using JSTL. There's documentation which refers to simple iterators but not to those whose items are themselves maps.

If anyone can give me a simple example of how a java List is iterated over in JSP I'd be massively appreciative. Mark

like image 357
volvox Avatar asked Jan 27 '10 16:01

volvox


People also ask

How do I iterate a map in JSTL?

You can use the same technique to loop over a HashMap in JSP which we have used earlier to loop over a list in JSP. The JSTL foreach tag has special support for looping over Map, it provides you both key and value by using var attribute. In the case of HashMap, object exported using var contains Map. Entry object.

Which tag is used to iterate over a list of items in JSP?

JSTL foreach tag allows you to iterate or loop Array List, HashSet or any other collection without using Java code.

Which tag is used to iterate over the elements in a collection or an array?

The forEach tag allows you to iterate over a collection of objects. You specify the collection using the items attribute, and the current item is available through a variable named by the var attribute. A large number of collection types are supported by forEach, including all implementations of java.


2 Answers

Mark, this is already answered in your previous topic. But OK, here it is again:

Suppose ${list} points to a List<Object>, then the following

<c:forEach items="${list}" var="item">     ${item}<br> </c:forEach> 

does basically the same as as following in "normal Java":

for (Object item : list) {     System.out.println(item); } 

If you have a List<Map<K, V>> instead, then the following

<c:forEach items="${list}" var="map">     <c:forEach items="${map}" var="entry">         ${entry.key}<br>         ${entry.value}<br>     </c:forEach> </c:forEach> 

does basically the same as as following in "normal Java":

for (Map<K, V> map : list) {     for (Entry<K, V> entry : map.entrySet()) {         System.out.println(entry.getKey());         System.out.println(entry.getValue());     } } 

The key and value are here not special methods or so. They are actually getter methods of Map.Entry object (click at the blue Map.Entry link to see the API doc). In EL (Expression Language) you can use the . dot operator to access getter methods using "property name" (the getter method name without the get prefix), all just according the Javabean specification.

That said, you really need to cleanup the "answers" in your previous topic as they adds noise to the question. Also read the comments I posted in your "answers".

like image 74
BalusC Avatar answered Sep 17 '22 15:09

BalusC


try this

<c:forEach items="${list}" var="map">     <tr>         <c:forEach items="${map}" var="entry">              <td>${entry.value}</td>          </c:forEach>     </tr> </c:forEach> 
like image 33
Prateek RG Avatar answered Sep 19 '22 15:09

Prateek RG