This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EL (${intKey}) is not working with Map<Integer,String> ?

 
Sagar Vyas
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

This is my first post in Coderanch and very excited to learn lots of new things.

I have one doubt related to EL (expression language).

I have created a map as below.

Map<Integer,String> map = new LinkedHashMap<Integer,String>();
map.put(2, "my value");

when I tried to access an element in my jsp as below I wont be able to get it.

${map[2]}

Then, I have found somewhere that ,it is taking as Long so it wont work.

but here my question is then how do I access integer key with EL ?

What is a way to access it ?

Looking for some help.

Thanks in advance,
Sagar Vyas
 
Bill Clar
Ranch Hand
Posts: 163
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you setting the map attribute in the request object (HttpServletRequest)?

Are you using the name "map"?
 
Stefan Evans
Bartender
Posts: 1778
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that EL interprets your literal number as a Long.

So it would translate into java code loosely as:


Because Integer with a value of 2 is NOT equal to a Long with a value of 2 (different classes are never equal) the value fails to be retrieved from the map.

I don't know of any way in EL to not interpret literal numbers as Long.

 
Sagar Vyas
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill Clar wrote:How are you setting the map attribute in the request object (HttpServletRequest)?

Are you using the name "map"?



Hi Bill,

Thanks for prompt reply,

It is not a case that I am using map, I kept name "map" just for example.

Thanks,
Sagar Vyas
 
Sagar Vyas
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:
Because Integer with a value of 2 is NOT equal to a Long with a value of 2 (different classes are never equal) the value fails to be retrieved from the map.

I don't know of any way in EL to not interpret literal numbers as Long.



Hi Stefan,

Thanks for explanations,

That means is there not any way to access map value having integer key ? in EL ?

Thanks,
Sagar Vyas
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic