Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access map value in EL using a variable as key

Tags:

jsp

el

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?

like image 717
user1643001 Avatar asked Sep 20 '12 17:09

user1643001


People also ask

How do you find the specific value of a key on a map?

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.

What is map key value?

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.

How do you set a value on a map?

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.


1 Answers

$ 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".

like image 107
Jasper de Vries Avatar answered Sep 28 '22 04:09

Jasper de Vries