Help with <c:forEach> Tag (it is driving me insane)
I keep gettingthis error (Don't know how to iterate over supplied "items" in <forEach>) whenever i run my jsp page.
JSP Code:
<c:set var="page_action" value="${sessionScope.httpData.find_Fix}" />
<c:set var="LoadedUpdates" value="${sessionScope.hotFix.fixDetails}" />
<c:if test="${page_action != null}" >
<div class="hot-fix">
<table>
<c:forEach items="${LoadedUpdates}" var="updates" varStatus="status">
<tr>
<td> ${status.count} </td><td><c:out value="${updates}" /></td>
</tr>
</c:forEach>
</table>
</div>
</c:if>
JAVA Code (Backend Bean):
import com.sun.java.util.collections.ArrayList;
publicclass HotFixLoader{
ArrayList fixdetails =new ArrayList();
public HotFixLoader(){
for (int i = 0; i < 10; i++){
fixdetails.add("<a href=\"#\">xxxxxx</a>, xx/xx/xxxx, xyxyxyxyxy");
}
}
public ArrayList getFixDetails(){
return fixdetails;
}
}[code]
[/code]
[2083 byte] By [
henrywaxa] at [2007-10-2 15:44:37]

It seems that LoadedUpdates is null or does not implement Collection interface.
Try to trace out a value of LoadedUpdates by:
<c:out value="${LoadedUpdates}"/>
Probably the better way to impl your logic is something like this:
<jsp:useBean id="hotFix" class="HotFixLoader" scope="session"/>
...
<c:forEach items="${hotFix.fixDetails}" var="updates" varStatus="status">
...
</c:forEach>
Good luck!
GeneralI have tried that and i am still getting the same error.i am totally lost for words
Your code looks fine.
JSTL should know how to iterate over an arrayList.
So probably you are not passing it an array list. Lets see what you ARE passing it:
Try commenting out/deleting the loop so that the page loads at least, and then:
<c:out value="${LoadedUpdates.class}"/>
<c:out value="${sessionScope.hotFix}"/>
<c:out value="${sessionScope.hotFix.class}"/>
<c:out value="${sessionScope.hotFix.fixDetails}"/>
<c:out value="${sessionScope.hotFix.fixDetails.class}"/>
Are you sure this is the forEach tag it is complaining about?
Hi evnafets,
When I print out the details you suggested, I get the following:
class com.sun.java.util.collections.ArrayList
uk.co.everest.knowledge.base.micro.HotFixLoader@84c class uk.co.everest.knowledge.base.micro.HotFixLoader
[<a href="#">xxxxxx</a>, xx/xx/xxxx, xyxyxyxyxy, <a href="#">xxxxxx</a>,
xx/xx/xxxx, xyxyxyxyxy, <a href="#">xxxxxx</a>, xx/xx/xxxx, xyxyxyxyxy,
<a href="#">xxxxxx</a>, xx/xx/xxxx, xyxyxyxyxy, <a href="#">xxxxxx</a>,
x/xx/xxxx, xyxyxyxyxy, <a href="#">xxxxxx</a>, xx/xx/xxxx, xyxyxyxyxy,
<a href="#">xxxxxx</a>, xx/xx/xxxx, xyxyxyxyxy, <a href="#">xxxxxx</a>,
x/xx/xxxx, xyxyxyxyxy, <a href="#">xxxxxx</a>, xx/xx/xxxx, xyxyxyxyxy,
<a href="#">xxxxxx</a>, xx/xx/xxxx, xyxyxyxyxy]
class com.sun.java.util.collections.ArrayList
After taking out the commas in the output text of my backing bean I get the following
class com.sun.java.util.collections.ArrayList
uk.co.everest.knowledge.base.micro.HotFixLoader@14ac class uk.co.everest.knowledge.base.micro.HotFixLoader
[<a href="#">xxxxxx</a> xx/xx/xxxx xyxyxyxyxy, <a href="#">xxxxxx</a> xx/xx/xxxx xyxyxyxyxy, <a href="#">xxxxxx</a> xx/xx/xxxx xyxyxyxyxy,
<a href="#">xxxxxx</a> xx/xx/xxxx xyxyxyxyxy, <a href="#">xxxxxx</a> xx/xx/xxxx xyxyxyxyxy, <a href="#">xxxxxx</a> xx/xx/xxxx xyxyxyxyxy,
<a href="#">xxxxxx</a> xx/xx/xxxx xyxyxyxyxy, <a href="#">xxxxxx</a> xx/xx/xxxx xyxyxyxyxy, <a href="#">xxxxxx</a> xx/xx/xxxx xyxyxyxyxy,
<a href="#">xxxxxx</a> xx/xx/xxxx xyxyxyxyxy]
class com.sun.java.util.collections.ArrayList
Then I assumed that it was my commas that is causing the problems but I was wrong, because I am still getting the same errors when I invoke the forEach loop.
Somebody please help
Kind regards and best wishes.
Hi guys,
If anyone of you is having problems with <c:forEach> looping round an arraylist, I have successfully worked around the stupid bug using c:forTokens.
In essence, when you call an instance of arrayList object in your jsp using <jspBean> or <c:set>, the contents of the array list will be enclosed in a normal array indexer "[" and delimited by "," .
Thereby making all the contents of your arrayList a single index of a normal array.
In short to retrieve all the contents of you arrayList you'll have to use the <c:forTokens> tag and then specify the delimeter as ","
An example of how i fixed my problem is below.
<c:forTokens delims="," items="${sessionScope.hotFix.fixDetails}" var="currentUpdate" varStatus ="status" >
<str:replace replace="[" with="" var="formattedUpdateString" >${currentUpdate}</str:replace>
<str:replace replace="]" with="" var="formattedUpdateString" >${formattedUpdateString}</str:replace>
<tr>
<td> ${status.count} </td><td><a href="#"><c:out value="${formattedUpdateString}" /></a></td>
</tr>
</c:forTokens>
Don't forget to download the string taglib from jakarta's website if you want to use the str replace function
url: http://jakarta.apache.org/taglibs/doc/string-doc/intro.html
