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
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 :