I’ve experienced rather strange behavior of JSTL forEach tag.
I have some bean called SessionBean:
public class SessionBean { private Collection<MyObject> objects; public Collection<MyObject> getObjects() {return objects;} ... }
And a simple JSP page like that:
<%@page contentType='text/html'%> <%@page pageEncoding='UTF-8'%> <%@taglib prefix='f' uri='http://java.sun.com/jsf/core'%> <%@taglib prefix='h' uri='http://java.sun.com/jsf/html'%> <%@taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core' %> <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'> <html> <body> <f:view> <h:form> <c:forEach var='myObject' items='#{SessionBean.objects}'> <h:outputText value='#{myObject}' /> <br /> </c:forEach> </h:form> </f:view> </body>
And, it doesn’t work. Exeption thrown is
javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied 'items' in <forEach> at org.apache.taglibs.standard.tag.common.core.ForEachSupport.toForEachIterator(ForEachSupport.java:255) at org.apache.taglibs.standard.tag.common.core.ForEachSupport.supportedTypeForEachIterator(ForEachSupport.java:219) ....
Why?
And then I change items='#{SessionBean.objects}' to items='${SessionBean.objects}', and there’s no exception. Except for MyObjects aren’t printed.
Then, I make the same change to <h:outputText value='#{myObject}' />, and it’s invalid value for this attribute.
Finally, replacing JSF outputText tag with just ${myObject} works as expected.
Could somebody explain, what happens here on each phase, please?
U: SessionBean is managed by JSF, and surely was created, for it performs some actions in the header.
RESOLUTION: The problem proved to be due to incompatibility between JSTL and JSF in J2EE 1.4. Switching to J2EE 5 made the first variant work just fine.
Thanks!
This article explains the difference between the unified EL and the EL. Here is a snippet
I am wondering if the problem is from the fact that an instance of SessionBean was not created?
Try this:
[Update] I wonder if this article might help then. It explains how the two EL’s work together.