• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Chet Hawkins
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Pie
Posts: 20750
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic