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?
util. HashMap. get() method of HashMap class is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the map contains no such mapping for the key.
Key value maps (KVMs) are ideal for this. A KVM is a custom collection of encrypted key/value String pairs. The following lists three broad use cases for storing data in KVMs: User session data: Data that is created and deleted by the runtime only; you cannot view or manage KVM entries outside of the runtime.
put() method of HashMap is used to insert a mapping into a map. This means we can insert a specific key and the value it is mapping to into a particular map. If an existing key is passed then the previous value gets replaced by the new value. If a new pair is passed, then the pair gets inserted as a whole.
$
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With