Access map value in EL using a variable as key
Asked Answered
H

5

17

I have a Map in EL as ${map} and I am trying to get the value of it using a key which is by itself also an EL variable ${key} with the value "1000".

Using ${map["1000"]} works, but ${map["$key"]} does not work. What am I doing wrong and how can I get the Map value using a variable as key?

Hasan answered 20/9, 2012 at 17:41 Comment(1)
$ is not the start of a variable name, it indicates the start of an expression.Superstition
S
22

$ is not the start of a variable name, it indicates the start of an expression. You should use ${map[key]} to access the property key in map map.

You can try it on a page with a GET parameter, using the following query string for example ?whatEver=something

<c:set var="myParam" value="whatEver"/>
whatEver: <c:out value="${param[myParam]}"/>

This will output:

whatEver: something

See: https://stackoverflow.com/tags/el/info and scroll to the section "Brace notation".

Superstition answered 20/9, 2012 at 20:31 Comment(0)
D
5

I have faced this issue before. This typically happens when the key is not a String. The fix is to cast the key to a String before using the key to get a value from the map

Something like this:

<c:set var="keyString">${someKeyThatIsNotString}</c:set>

<c:out value="${map[keyString]}"/>

Hope that helps

Dubitable answered 24/5, 2016 at 7:4 Comment(0)
E
2

You can put the key-value in a map on Java side and access the same using JSTL on JSP page as below:

Prior java 1.7:

Map<String, String> map = new HashMap<String, String>();
map.put("key","value");

Java 1.7 and above:

Map<String, String> map = new HashMap<>();
map.put("key","value");

JSP Snippet:

<c:out value="${map['key']}"/>
Electrum answered 23/7, 2014 at 15:22 Comment(1)
In my case value is not a string but an Address object containing string fields like city, street and pincode. How do I display the city field of the address?Argentous
S
0

My five cents. Now I am working with EL 3.0 (jakarta impl) and I can access map value using three ways:

 1. ${map.someKey}
 2. ${map['someKey']}
 3. ${map[someVar]} //if someVar == 'someKey'
Syce answered 22/9, 2020 at 10:35 Comment(3)
It doesn't work because your someKey is actually not a variable like your row. This question asks how to use a variable as map key and your answer doesn't answer it. Jasper has already answered it (see the currently top voted answer).Plemmons
Your answer is implying that syntax is different in EL 3.0. But this is not true. It's still exactly the same as it was in EL 1.0. See Jasper's answer.Plemmons
@Plemmons Unfortunately, I can't agree with you. My answer is NOT implying that syntax is different in EL 3.0, it just states that in v. 3.0 there are three ways (there can be versions 1,2, 4,5 etc) and for convenience I listed them. And what about there are other answers, I know, I have seen them. This is my vision of the matter.Syce
L
-4

I think that you should access your map something like:

${map.key}

and check some tutorials about jstl like 1 and 2 (a little bit outdated, but still functional)

Lorou answered 20/9, 2012 at 18:3 Comment(1)
Not ${map.$key}, just ${map.key}Lorou

© 2022 - 2024 — McMap. All rights reserved.