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?
$
is not the start of a variable name, it indicates the start of an expression. You should use${map[key]}
to access the propertykey
in mapmap
.You can try it on a page with a
GET
parameter, using the following query string for example?whatEver=something
This will output:
See: https://stackoverflow.com/tags/el/info and scroll to the section "Brace notation".
You can put the key-value in a map on
Java
side and access the same usingJSTL
onJSP
page as below:Prior java 1.7:
Java 1.7 and above:
JSP Snippet:
I think that you should access your map something like:
and check some tutorials about jstl like 1 and 2 (a little bit outdated, but still functional)
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