• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

EL List traversing

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On a JSP page, How can I traverse a List of Objects say of class A and pull the values of there instance variables...

e.g

class A{
private String name;

// getter and setter's for name...

}
Thanks...
 
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use JSTL c:forEach tag to iterate over the List.

<c:forEach items="listOfObjects" var="currObject>
${listOfObjects.name}
${listOfObjects.secondProperty}
${listOfObjects.otherProperty}
</c:forEach>

OR
If you want access for a given index only then ::
${listOfObjects[0].name}
${listOfObjects[0].secondProperty}
${listOfObjects[0].otherProperty}

Regards,
Shivani
 
Vivek Kinra
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Shivani... Your second option will not work because the return type from List is Object and then we have to cast that object to its class to access the "name" property.

I haven't tried second one but I think it will have the casting problem too..

Thanks...
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks Shivani... Your second option will not work because the return type from List is Object and then we have to cast that object to its class to access the "name" property


Actually no, you don't have to. EL will do that for you. If you've put Person objects in a List, you don't have to cast to Person when getting the value back.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
vivek, you can see the same by executing the following code

 
Vivek Kinra
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Salil, Satou....

It work's great...My mistake was I was trying to traverse the list by directly using the List instead of setting it in request like


<c:forEach var="object_name" items='${a}'>
<br>
<c ut value='${object_name.name}'>
</c ut>
</c:forEach>

 
That's a very big dog. I think I want to go home now and hug this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic