aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Is for each on a map with EL possible? 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 » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Is for each on a map with EL possible?" Watch "Is for each on a map with EL possible?" New topic
Author

Is for each on a map with EL possible?

Jonny Andersson
Ranch Hand

Joined: Sep 07, 2004
Posts: 87
I am trying to learn the EL for the J2EE SCWCD-exam and have trouble when I try to loop over a map with a for each that use EL. Here is an example of what I try to accomplish, is it possible to do this?

I have a servlet that creates a map like this one:


It contains movie objects that has a name property like this:


And I the forward to a JSP page to print the list of movies using a for each like this:



But it does not work. I get some error that say that a TreeMap$entry not have an attribute name ...

You can see what I try to do, is it possible or do you have a better, working, example?

And just to understand, is not



a reference to a movie objekt? One thing that complicates here is of course that I have a map. A map have keys and objects, am I iterating over the keys or the objects? With a list would I know that it is the objects, but I am not quite sure with a map. In normal Java would I have to iterate over the keys and then get the objects from the map using that keys.


Truly agile Java application developer with Extreme Programming and high quality craftsmanship
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Using the forEach loop for a Map will return an instance of Map.Entry for each iteration. If you look at the Map.Entry class, you'll see that it has a key and a value. So in your example, you should do the following :


[My Blog]
All roads lead to JavaRanch
Jonny Andersson
Ranch Hand

Joined: Sep 07, 2004
Posts: 87
That example from you looks so reasonable so that I already know that it must be correct ... I will try it later when I have time to look at it again. A lot of thanks for your help!
 
Don't get me started about those stupid light bulbs.
 
subject: Is for each on a map with EL possible?
 
Similar Threads
JSTL pg 437 HFSJ
JSTL problem
problem with c:set tag
jsp:include inside c:forEach
Using JSTL example in K&B Book