Win a copy of Getting started with Java programming language this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

c:forEach not iterating over object fields  RSS feed

 
kri shan
Ranch Hand
Posts: 1486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

employee list has List of Employee. Inner forEach loop is not printing the employee first name, employee last name and city in each iteration.







 
Tim Moores
Saloon Keeper
Posts: 3403
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're assuming that c:forEach can iterate over JavaBean-style object attributes - not so. Read http://www.ibm.com/developerworks/java/library/j-jstl0318/ to learn what it actually can iterate over.
 
kri shan
Ranch Hand
Posts: 1486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can i access the bean property within forEach ?


 
Tim Moores
Saloon Keeper
Posts: 3403
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you tried?
 
kri shan
Ranch Hand
Posts: 1486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No start tag for  </c:forEach> line 7
 
kri shan
Ranch Hand
Posts: 1486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tried this way too and it prints the employee object (not printing first name, last name and city values ).


 
Stefan Evans
Bartender
Posts: 1831
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There isn't really a nice way to dynamically get all the properties of a bean.  The main issue is getting them in "order". 
How does it know you want first name, then last name?  Or last name then first name?  And should one be bolded but not the other?


Two suggestions:
#1: Specify the properties you want displayed specifically and format them appropriately is often the easiest:


#2:
Got this answer from reading http://stackoverflow.com/questions/14454769/creating-array-using-jstl-or-el




#3:
Make a custom tag for printing out your object.


 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!