• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Iterating over nested collections

 
Gary Blomquist
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic