• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can we use Map in items attribute of forEach

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I use user defined bean to store properties and put the beans in

java.util.Map newsMap=new java.util.HashMap();

when retrieving in forEach tag in items attribute I specified

<c:forEach var="news" items="${sessionScope.newsMap}">

input type="checkbox" value="${news.newsId}"/>

</c:forEach>

here, news is the NewsBean , a user defined bean and newsId is the bean proeprty of long type and newsMap is a map object that stores NewsBean objects with keys as Long.

I got the error something like newsId symbol can not be identified

Is it possible to use Map in EL expressions like in items attribute of forEach tag?

regards,

Ramesh Kangamuthu
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Absolutely. But you're missing an important concept.

When you iterate over a Map, what do you get? The values? No, you get the Map.Entry elements.

So how would you retrieve the values given that the iteration items are instances of Map.Entry?
[ April 09, 2008: Message edited by: Bear Bibeault ]
 
Ramesh kangamuthu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bear,

Thank you for valuable reply. I am not familiar with Map.Entry elements. I need to iterate a Map consists of keys as Long Objects and values as NewsBean Objects, it was stored as Session attribute. In the var attribute "news" of forEach tag,I tried to retrieve the NewsBean Objects and their properties by putting "." after the beans in the following manner.

<c:forEach var="news" items="${sessionScope.newsMap}">

<input type="checkbox" value="${news.newsId}"/>

</c:forEach>

It shows error for the property specified after putting "."

In the above statement newsId is a Long property of the bean NewsBean.

Could you give me an example for my requirement?

Regards,

Ramesh Kangamuthu
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ramesh,
Bear is tying to say that when you iterate over a Map you do not get
"news" objects. You get a Map.Entry object with a pair key/value.

[Edit: detailed example removed. neilson, your desire to help is commendable, but when someone is trying to help a poster work through an example, it's not helpful to just blurt out the answer. People learn best when they work through examples on their own. We try to avoid writing code for people, but rather help them to write their own.]
[ April 09, 2008: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ramesh kangamuthu:
I am not familiar with Map.Entry elements.

If you are going to be working with Maps, this is an important concept to get under your belt. Please look up Map.Entry in the Javadoc and study its methods.


Beacuse newsMap is a Map, the items set into the var will not be the Map values, but Map.Entry instances (as neilson helpfully pointed out).

Given the layout of Map.Entry (you did go look at the Javadoc, right?) you can easily access the key or the value of the entry within the body of the forEach loop.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
P.S. Please be sure to use UBB code tags when posting code to the forums. Unformatted code is extremely hard to read. Please read this for more information.
 
reply
    Bookmark Topic Watch Topic
  • New Topic