• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Iterating a List Using JSTL

 
Ranch Hand
Posts: 572
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
paul nisset
Ranch Hand
Posts: 572
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 572
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 572
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 572
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 572
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 572
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your input.

The scoping precedence rules are good to know.
 
paul nisset
Ranch Hand
Posts: 572
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 572
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad you got it sorted out. Perhaps you could mark your topic as Resolved?
 
If you have a bad day in October, have a slice of banana cream pie. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic