JSTL forEach tag: problems with enumeration, and with understanding how it should work

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"
            <c:forEach var="myObject" items="#{SessionBean.objects}">
                <h:outputText value="#{myObject}" /> <br />

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)

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.


Asked by: Vanessa330 | Posted: 21-01-2022

Answer 1

This article explains the difference between the unified EL and the EL. Here is a snippet

Evaluation of EL

Evaluation of EL is categorized as immediate evaluation and deferred evaluation. Immediate evaluation means a JSP page evaluates the expression when the page is rendered. With immediate evaluation, all values are always read-only. JSP EL expressions take the form of ${imExpr}. JSP expressions are evaluated immediately.

Deferred evaluation means that the technology using the unified EL takes over the responsibility of evaluating the expression from the JSP engine and evaluates the expression at the appropriate time during the page lifecycle. The EL takes control from the JSP container to evaluate the expression at the appropriate time. JSF EL expressions take the form of #{defExpr}. JSF expressions work in this way.

The following example shows a JSF inputText tag, which represents a text field component into which a user enters a value. The inputText tag's value attribute references an expression that points to the name property of the book bean.

<h:inputText id="name" value="#{student.name}"/>

For an initial request of the page containing this tag, the JSF implementation evaluates the #{student.name} expression during the "render response" phase of the lifecycle. During this phase, the expression merely accesses the value of quantity from the book bean, as is done in immediate evaluation.

For a postback, the implementation evaluates the expression during the "apply request values," "process validations," and "update model" phases, during which the value is retrieved from the request, validated, and propagated to the book bean.

I am wondering if the problem is from the fact that an instance of SessionBean was not created?

Try this:

<jsp:useBean class="packagename.SessionBean" id="sb"/>
<c:forEach var="myObject" items="${sb.objects}">
            <h:outputText value="${myObject}" /> <br />

[Update] I wonder if this article might help then. It explains how the two EL's work together.

Answered by: Julia893 | Posted: 22-02-2022

Similar questions

java - Understanding the need for a DI framework

This might be a naive question. I'm currently learning the Spring framework and dependency injection. While the basic principle of DI is rather easy to grasp, it's not immediately obvious why you need an elaborate framework to implement it. Consider the following: public abstract class Saw { public abstract void cut(String wood); } public class HandSaw extends Saw { public void cut(S...

Understanding C++ compilers from a Java / C# perspective

I'm a moderately experienced Java / C# programmer, and I've recently started learning C++. The problem is, I'm having trouble understanding how to structure the various header and code files. This seems mostly due to my lack of understanding as to how the compiler links everything together. I've tried reading some textbooks, but my preconceptions are heavily colored by my Java and C# knowledge. For example, I'm having a ha...

foreach - Understanding for each loop in Java

This question already has answers here:

java - Understanding Goetz's article on Thread safety of HttpSession

Referring to Brian Goetz's article Are all stateful Web applications broken? for IBM developerWorks, I want to refer to this piece of code HttpSession session = request.getSession(true); ShoppingCart cart = (ShoppingCart)session.getAttribute("shoppingCart"); if (cart == null) { cart = new ShoppingCart(...);...

java - Understanding kernel assigned local addresses on a udp socket

i'm developing a java application using the jstun library (hxxp://jstun.javawi.de/), and i need to compare my public ip with the one chosen by the kernel (wildcard address - hxxp://java.sun.com/j2se/1.5.0/docs/api/java/net/DatagramSocket.html#DatagramSocket() ) when i create a udp socket. what i don't understand is, if my local ip on my natted network is in the form of 192.168.1.x, why do i get an ip such as 10.x.x...

java - Understanding Classes

I'm new to programming and have some questions about classes. I'll use Karel as an example: public class Karel extends robot{ .... } then I extend Karel: public class SuperKarel extends Karel{ .... } but then I want to organize some groups of methods: public class KarelJumps extends SuperKarel { .... } public class Kar...

understanding the Java try/catch

I'm currently writing an app for school that is a mini search engine. On execution, it indexes the contents of the text files included as args. I haven't used the try and catch methods before, and we were just given this code as an include in our program: Scanner inputFile = null; try { inputFile = new Scanner(new File("dog.txt")); } catch (FileNotFoundException fe) { Syste...

bytecode - Understanding Java Byte Code

Often I am stuck with a java class file with no source and I am trying to understand the problem I have at hand. Note a decompiler is useful but not sufficient in all situation... I have two question What tools are available to view java byte code (preferably available from the linux command line ) What are good references to get familiar with java byte code syntax

java - Understanding context.xml in tomcat 6

I created a mainly empty dynamic web project in eclipse. It has no servlets no jsp files The web.xml is &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/...

java - Understanding trees in ANTLR

I'm trying to use Antlr for some text IDE-like functions -- specifically parsing a file to identify the points for code folding, and for applying syntax highlighting. First question - is Antlr suitable for this requirement, or is it overkill? This could be achieved using regex and/or a hand-rolled parser ... but it seems that Antlr is out there to do this work for me. I've had a look through the ....

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)