File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes c:forEach fails along with Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "c:forEach fails along with "${var}" syntax" Watch "c:forEach fails along with "${var}" syntax" New topic

c:forEach fails along with "${var}" syntax

Chet Hawkins

Joined: Jun 15, 2011
Posts: 20
Both blue lines produce valid output. The list has 10 elements and is an ArrayList.
The forEach loop never executes and the "${stlist.size()}" call fails to produce output.

This is a JSP - some table data elements are just debugging framers.

<%@page import="context.context"%>
<%@page import="database.statisticlist"%>
<%@page import="database.statistic"%>
<%@taglib prefix="c" uri="" %>
context ctxt = (context) session.getAttribute("context");
statisticlist stlist = new statisticlist( ctxt, null, map ); // dont worry map is properly initialized
<td>Count = ${stlist.size()}</td>
<td>Count = <%=stlist.size()%></td>
<td><%=stlist.get(1).getName() %>
<c:forEach var="statistic" items="${stlist}" varStatus="status">
<td>End Table</td>

Why is the forEach loop failing and I guess the greater issue is what is wrong with my "${var}" syntax?

I have another jsp file previous to this one with the same pattern. It works great.
that HTML:

<select name="selectCharacter">
<option>New Character</option>
<c:forEach items="${characterlist}" var="option">

Thanks for any help.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63840

The variable stlist is a scripting variable created in a scriptlet, and it can only be used in scriptlets. For use with the JSTL and EL you need to use a scoped variable. Mixing scriptlets and the JSTL/EL in the same page is sure recipe for disaster. Give up on scriptlets now. They have been discredited for near a decade and their use is simply irresponsible in 2011.

[Asking smart questions] [About Bear] [Books by Bear]
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

What's wrong with your ${var} syntax? What's wrong is that you think it's Java. It isn't. Forget about Java syntax for the EL. Example: you have

First of all you don't need a cast. (This isn't Java, EL isn't a strongly-typed language.)
And second of all you don't call methods (this isn't Java), you use properties. So you should have:
Chet Hawkins

Joined: Jun 15, 2011
Posts: 20
Yes, I am getting it. TY. I was able to dig into scoped variables, convert my classes to beans (I suppose), rewire their constructors into building methods, and erase all my insane erroneous syntax, casting and like that I was using in a mad effort to get anything in the forEach to work. It now works.

Many thanks. On to the next major debacle.
I agree. Here's the link:
subject: c:forEach fails along with "${var}" syntax
It's not a secret anymore!