File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Iterating over nested collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Iterating over nested collections" Watch "Iterating over nested collections" New topic
Author

Iterating over nested collections

Gary Blomquist
Greenhorn

Joined: Dec 07, 2001
Posts: 14
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

Joined: Jan 10, 2002
Posts: 61601
    
  67

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61601
    
  67

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

Joined: Dec 07, 2001
Posts: 14
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Iterating over nested collections