aspose file tools*
The moose likes JSP and the fly likes forEach in Java class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "forEach in Java class" Watch "forEach in Java class" New topic
Author

forEach in Java class

Dan Parsons
Ranch Hand

Joined: May 30, 2007
Posts: 70
I have a JSTL forEach example that I would like to implement in a Java class instead of JSP.
Example:


Is the below attempt the right way?




David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Does it work? Seems like that'd be the absolute quickest way of finding out, considering you've already written a first attempt.

It's not entirely clear what you're trying to do, though; right now it looks like you're setting each movie to a string, then doing nothing with it.

Note that if you're using Java 1.5+ you may want to consider using the loop syntax introduced there.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

There's no need to use an iterator when looping over a List. For example iterating over a List<Item> :




[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Keith Flo
Ranch Hand

Joined: Nov 29, 2005
Posts: 128

Dan,

No .. it doesn't look correct ... a couple questions ...

1. What does MovieListing.getList() return? a java.util.ArrayList of Strings, a java.util.ArrayList of MovieBeans or something else?
2. It looks like your JSP code places an ArrayList of MovieBean into scope and the toString() method of MovieBean prints what you want when you iterate using forEach. Is this what you want?
3. What do you want the Java code to do?


kf
SCJP 5.0 (preparing for SCWCD)
Dan Parsons
Ranch Hand

Joined: May 30, 2007
Posts: 70
Keith Flo wrote:Dan,

No .. it doesn't look correct ... a couple questions ...

1. What does MovieListing.getList() return? a java.util.ArrayList of Strings, a java.util.ArrayList of MovieBeans or something else?
2. It looks like your JSP code places an ArrayList of MovieBean into scope and the toString() method of MovieBean prints what you want when you iterate using forEach. Is this what you want?
3. What do you want the Java code to do?


Its not working and seems to print all the records per array.
Its suppose to print a Java java.util.ArrayList of MovieBeans.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

You have not posted your updated code since my post. What's the status of things now?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: forEach in Java class
 
Similar Threads
Answer this if you know EL
JSTL printing issue
Jstl error
Doubt in Attribute with JSTL.Thanks!
howto display data from database into html