aspose file tools*
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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "c:forEach fails along with "${var}" syntax" Watch "c:forEach fails along with "${var}" syntax" New topic
Author

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

Chet Hawkins
Greenhorn

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="http://java.sun.com/jsp/jstl/core" %>
context ctxt = (context) session.getAttribute("context");
statisticlist stlist = new statisticlist( ctxt, null, map ); // dont worry map is properly initialized
%>
...
<table>
<tr>
<td>Count = ${stlist.size()}</td>
<td>Count = <%=stlist.size()%></td>
<td><%=stlist.get(1).getName() %>
<td>Name</td>
<td>Rank</td>
<td>TP</td>
</tr>
<c:forEach var="statistic" items="${stlist}" varStatus="status">
<tr>
<td>Start</td>
<td>${status.getCurrent().getName()}</td>
<td>${(database.statistic)statistic.getName()}</td>
<td>${(statistic)statistic.getValue()}</td>
<td>${(statistic)statistic.getTP()}</td>
<td>End</td>
</tr>
</c:forEach>
<tr>
<td>End Table</td>
</tr>
</table>

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">
<option>${option.getName()}</option>
</c:forEach>
</select>

Thanks for any help.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

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] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18986
    
    8

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
Greenhorn

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: http://aspose.com/file-tools
 
subject: c:forEach fails along with "${var}" syntax