• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Iterating over nested collections

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am attempting to iterate over a TreeMap of ArrayLists using JSTL. If the ArrayLists contain Strings it works. However, if the ArrayLists contain simple javabeans I get the following error:

javax.servlet.ServletException: Don't know how to iterate over supplied "items" in <forEach>

Here is sample code:

<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.TreeMap" %>

<%
class Node {
private String name;
private boolean checked = false;

public Node(String name) {
this.name = name;
}

public String getName() {
return this.name;
}

public boolean getChecked() {
return this.checked;
}
}
ArrayList list1 = new ArrayList();
list1.add(new Node("1-1"));
list1.add(new Node("1-2"));
list1.add(new Node("1-3"));
ArrayList list2 = new ArrayList();
list2.add(new Node("2-1"));
list2.add(new Node("2-2"));
list2.add(new Node("2-3"));
TreeMap map = new TreeMap();
map.put("list1", list1);
map.put("list2", list2);
pageContext.setAttribute("map", map);
%>

<c:forEach var="mapItem" items="${map}">
<c:forEach var="list" items="${mapItem.value}">
<c:forEach var="listItem" items="${list}">
${listItem.name}<br />
${listItem.checked}<br />
</c:forEach>
</c:forEach>
</c:forEach>

Any idea what wrong?

Thanks,

Gary Blomquist
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your inner-most forEach is attempting to loop through an instance of a Node, which is not a collection.

What are you trying to do with that inner loop?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be more explicit, your outer loop is iterating over the map. Your next loop is looping over the list and giving you the nodes. The third loop is unnecessary.

I think, for what you are trying to do, the code would be more along the lines of:



(Btw, please be sure to use UBB code tags when posting code to preserve the formatting).

Of course, once you do that, more errors are revealed in your sample. Node needs to be declared public, and in a declaration block. But I'm assuming that you're not really declaring things like thia for real and that this is just illustrative sample code, right?
 
Gary Blomquist
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I mistakenly thought "${mapItem.value}" was returning a Collection of values in the map (similar to the values method of HashMap) that needed to be iterated over to get the ArrayLists rather than a single value. I guess thats why they named it .value instead of .values : ) Thanks, for pointing out my error. I moved the Node to a top level class rather than leaving it nested.

And, thanks for the pointer on the UBB codes.
 
Destiny's powerful hand has made the bed of my future. And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic