• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSTL forEach and iterating through a list  RSS feed

 
Dave Newman
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Have racked my brains, and I'm obviously missing something here. I have a list that I wish to iterate through. using while() works fine, and all 65 elements in the list are iterated through

The foreach() JSTL tag does not iterate.


Code below:


<table BORDER>

<%

Query q = em.createNamedQuery ("Site.findAll");
List<Site> siteList = q.getResultList();

out.print(siteList.size());

Iterator<Site> siteIterator = siteList.iterator();

// This bit works fine

while (siteIterator.hasNext()){
out.print((Site)siteIterator.next() + "<BR>");
}


%>

<c:forEach items="${siteList}" var="mySite">
<tr>
<!-- This bit is never reached -->
<td><c:out value="${mySite.name}"/></td>
</tr>
</c:forEach>
</table>
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recently discussed here.

The JSTL and EL has no visibility to scriptlet variables. They are not meant to work together. You need to create a scoped variable.

(And remove all scriptlets from your JSP pages.)
 
Dave Newman
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Recently discussed here.

The JSTL and EL has no visibility to scriptlet variables. They are not meant to work together. You need to create a scoped variable.

(And remove all scriptlets from your JSP pages.)


Google still has some work to do... I searched for ages and didn't find that thread!

It probably shows how long it's been since I went near JSP (as a rule I like to avoid it!)

The scriptlets are there purely so I can play around with JSTL. I had intended to eventually put the list setup code in some kind of facade class or use case controller, or something like that, or is there some kind of JSTL/JSP/Whatever recommended framework for that?

wait a mo... is it a backing class like with Tapestry?

Anyway, thanks - At least I know where I've gone wrong!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Newman wrote:The scriptlets are there purely so I can play around with JSTL.

Cool. I was hoping that was the case.

I had intended to eventually put the list setup code in some kind of facade class or use case controller

That's the right track. You may find this article that I wrote for the JR Journal a while back an interesting read.

or something like that, or is there some kind of JSTL/JSP/Whatever recommended framework for that?

There are plenty. I'm not a big fan of them (too big, too complicated) so I wrote a simple front controller implementation (FrontMan, see links below) that I use.

You can do some research and decide where your own needs lie (see Struts, SpringMVC, etc -- but I'd avoid JSF like the plague).

Anyway, thanks - At least I know where I've gone wrong!

My pleasure!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!