File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Iterating a List Using JSTL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Iterating a List Using JSTL " Watch "Iterating a List Using JSTL " New topic
Author

Iterating a List Using JSTL

paul nisset
Ranch Hand

Joined: May 13, 2009
Posts: 171
Hi,

I'm having a problem using JSTL to output a list in my jsp page.
Hopefully somebody can clarify what is going on.

It only seems to work if I access each field as an array element by position ie myobject[0] rather than by filename ( ie myobject.myField )

The code below works if I remove ${lesson.title} but everything I've read seems like using ${lesson.title} without falling over .

I should note the result set only has one row but I would think iterating in JSTL would work the same , regardless of whether you have one row or a hundred.

The error is :


The jsp codes is :


The list is is being generated like below and is getting populated


Thanks ,
Paul
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

If lesson is a list, then it can't have a title property. Where do you think this title property should come from?

P.S. What's with the scriptlets mixed in with the JSTL?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
paul nisset
Ranch Hand

Joined: May 13, 2009
Posts: 171
Hi,

Thanks for getting back to me.
I'm iterating a list called "lessonlist" and each object in the list is a Lesson object

"title" is property in the Lesson class.

<c:forEach items="${lessonlist}" var="lesson">

The scriptlet is just for debugging. It is sending out count of the list it is iterating.

-Paul
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

Hint: if title is a property of the list entries, then it's not a property of the list itself, is it?
paul nisset
Ranch Hand

Joined: May 13, 2009
Posts: 171

As far as I can tell, I'm not addresing 'title' as a property of the list . I'm addressing it as as a property of each object/entry in the list.
In the following code , ${lesson[0]} correctly displays the value of the 'title' property of lesson but ${lesson.title} produces an error.
The problem is I still don't understand why.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

paul nisset wrote:
As far as I can tell, I'm not addresing 'title' as a property of the list .



Contradiction. You say you are not. And then you do. If lesson is the list, then ${lesson.title} attempts to find the title property of the list.

I'm addressing it as as a property of each object/entry in the list.
See above. You are not.

If title is to be a property of an entry, and ${lesson[0]} is an entry, then how would you reference the title property of the entry?

${lesson[0]} correctly displays the value of the 'title' property
That may be what it appears to be doing, but it's not. It's emitting whatever the entry evaluates to as a string.
paul nisset
Ranch Hand

Joined: May 13, 2009
Posts: 171
OK . I'm obviously missing something because , in the following line ,my understanding is that ${lessonlist} is the list I am iterating
and JSTL is making each entry in the list available as a variable called "lesson" .



If this is not the case, how would I iterate the list so that I could address each entry in the list as an object and access it's properties?
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
Bear: You're responding as though lessonlist is a list of List objects, but according to the snippet of Java code provided in the first post, it seems that lessonlist is a List of Lesson objects. So it seems to me that the lesson var specified in the <c:forEach> loop should refer to an individual Lesson object instance.

Am I missing something?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

You may be right. Maybe I got thrown off by the formatting (lack thereof) and no indentation, as well as the [0] notation applied to lesson.

paul nisset
Ranch Hand

Joined: May 13, 2009
Posts: 171
The [0] notation is basis of the original question I posed .

When I use :

I get the value I expect .

When I use :



I get the NumberFormatException .

I don't understand why .

Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
In your Java code, you did the following:

All we know for sure is that lessonlist is a List of something. I was assuming it was a list of Lesson objects, but actually you don't show us the code the creates this. What does your lessonHome.FindByCourse(id) actually return? Are you sure it is a list of Lesson objects? Can it be returning a list of arrays of Lesson objects? Or does it return a single Lesson object? Let's have a look at that.

By the way, why did you set the lessonlist variable on both the Request and the Session?
paul nisset
Ranch Hand

Joined: May 13, 2009
Posts: 171
Thanks for taking a look.

This is the method that is returning the list.
sessionFactory is a Hibernate SessionFactory that is wired in via Spring.



I set the lessonlist variable on both the Request and the Session because I was unsure which variable the foreach loop items property was looking at.

Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
paul nisset wrote:Thanks for taking a look.

This is the method that is returning the list.
sessionFactory is a Hibernate SessionFactory that is wired in via Spring.




Well, nothing looks obviously wrong. I would start debugging with a simple case to see if that works (creating a sample list yourself rather than using the one returned by Hibernate) and work up from there.
paul nisset wrote:
I set the lessonlist variable on both the Request and the Session because I was unsure which variable the foreach loop items property was looking at.

If will find the first one as it goes through the scopes looking for it: page, request, session and finally application. If you set one on both request and session scope, the request scoped object will hide the one on the session scope.

paul nisset
Ranch Hand

Joined: May 13, 2009
Posts: 171
Thanks for your input.

The scoping precedence rules are good to know.
paul nisset
Ranch Hand

Joined: May 13, 2009
Posts: 171
It is something to do with the way Hibernate produces the list .

When I create list manually ( as below ) ,it works and ${lesson.title} shows up the way I would expect it to for the three entries.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

paul nisset wrote:It is something to do with the way Hibernate produces the list .


Yes, I believe you are right. So it's time to put in some debugging code to print out the list, or some useful properties like what class it is and what its contents are and so on. Put that in your Java code because (as you're finding out) a JSP is a pretty poor platform for testing and debugging.
paul nisset
Ranch Hand

Joined: May 13, 2009
Posts: 171
Just an FYI for anyone following this. It was indeed a Hibernate issue.

I was using a Hibernate Native SQL query but I was not casting the result to an Entity (via addEntity() )before adding it to a list (see below).

So, Hibernate was returning a list but not a list of what I expected.


Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
Glad you got it sorted out. Perhaps you could mark your topic as Resolved?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Iterating a List Using JSTL