Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using EL to loop through a List and display all its properties

 
Matthew Busse
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
  •  
    Matthew Busse
    Ranch Hand
    Posts: 52
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Sheriff
    Posts: 21298
    32
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 52
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 1777
    10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 52
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 65105
    89
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ItDoesntWorkIsUseless

    What is happening?
     
    Matthew Busse
    Ranch Hand
    Posts: 52
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.

    Bear Bibeault wrote:ItDoesntWorkIsUseless


    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
    Pie
    Posts: 65105
    89
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 14691
    16
    Eclipse IDE Ubuntu VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Just in case, check the HTML source from the browser. Do you have the following ?

    Or anything else ?
     
    Stefan Evans
    Bartender
    Posts: 1777
    10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic