| Author |
Using EL to loop through a List and display all its properties
|
Matthew Busse
Ranch Hand
Joined: Sep 29, 2010
Posts: 52
|
|
Hello rancher geniuses,
I'm trying to use a JSP to display information gathered by hibernate. The model uses hibernate to generate a List of objects, and each object has several properties that I want to display. I'm able to successfully pass the List to the controller, but I can't figure out how to get the view to loop through the list of objects, and display all the properties in a table.
None of the books I have explain this, and I can't find any answers online (I've been looking for the last hour or so).
Any help would be greatly appreciated!!
Thanks!
Matt
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56529
|
|
Please try to be complete when posting:
Is scoped variable userFeedback the list? If so, the in your forEach loop, feedbackEntry will be the item from the list for the current iteration.
So what's the problem?
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Matthew Busse
Ranch Hand
Joined: Sep 29, 2010
Posts: 52
|
|
Bear Bibeault wrote:Please try to be complete when posting:
Is scoped variable userFeedback the list? If so, the in your forEach loop, feedbackEntry will be the item from the list for the current iteration.
So what's the problem?
Yes, userFeedback is the List, it's was set as an attribute on the request by the controller.
The code doesn't work. I put comments in the code snippet saying that the ${feedbackEntry.name} and {$feedbackEntry.id} do not display anything, it's just blank. Sorry if I didn't make the problem clear.
I can access the data using ${userFeedback[0].name}, so I know the view is receiving the List of objects with the correct information. I just can't get the forEach tag to loop through the list and display the properties of each object.
Thanks,
Matt
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
Matthew Busse wrote:I just can't get the forEach tag to loop through the list and display the properties of each object.
That's because you're looking at the tag. Look at the element... what is its scope? (This is what happens when you think in terms of tags and not in terms of elements.)
|
 |
Matthew Busse
Ranch Hand
Joined: Sep 29, 2010
Posts: 52
|
|
Paul Clapham wrote:
That's because you're looking at the tag. Look at the element... what is its scope? (This is what happens when you think in terms of tags and not in terms of elements.)
I see. I'm guessing the element is the "feedbackEntry" that is being created in the forEach statement? It's scope is only inside the forEach statement?
I still don't understand. The Head First book doesn't explain the scope of the elements in the forEach statement.
I appreciate the fact that you're trying to make me think through this myself, but could you give me a little more help please? I'm been trying to find something else about this online, to no avail...
Thanks!
Matt
|
 |
Stefan Evans
Bartender
Joined: Jul 06, 2005
Posts: 1008
|
|
Ok, I'm having a moment now. I can't believe I missed this when I looked at the problem earlier
I would suggest nesting the content you want repeated within the <c:forEach> tags.
ie:
|
 |
Matthew Busse
Ranch Hand
Joined: Sep 29, 2010
Posts: 52
|
|
Stefan Evans wrote:Ok, I'm having a  moment now. I can't believe I missed this when I looked at the problem earlier
I would suggest nesting the content you want repeated within the <c:forEach> tags.
Thanks for your input. I actually noticed I had the ending </c:forEach> tag in the wrong place soon after I left the first post, but moving it to the end still didn't work. It now looks like this:
So, I'm still stuck.
Thanks!
Matt
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56529
|
|
ItDoesntWorkIsUseless
What is happening?
|
 |
Matthew Busse
Ranch Hand
Joined: Sep 29, 2010
Posts: 52
|
|
Bear Bibeault wrote:
What is happening?
Nothing is happening. The ${userFeedback[0].name} tag works, so I'm pretty sure the List of objects is successfully getting to the view. After that, it's just blank. Zip, nada.
Does that mean I can't use the forEach tag with objects in this way? I know how to use ArrayLists with forEach tags, so I thought about creating an ArrayList of ArrayLists of the object properties in the model, and then passing that up to the view, is that what I need to do in this case?
Thanks!
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56529
|
|
No, it means "it doesn't work" or "it's blank", doesn't help us help you.
What's blank? The entire page? The table? The TD elements?
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14685
|
|
Just in case, check the HTML source from the browser. Do you have the following ?
Or anything else ?
|
[My Blog]
All roads lead to JavaRanch
|
 |
Stefan Evans
Bartender
Joined: Jul 06, 2005
Posts: 1008
|
|
|
And if the <c:forEach> tag is showing up in your html source, then add <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> to your page.
|
 |
 |
|
|
subject: Using EL to loop through a List and display all its properties
|
|
|