使用变量作为键访问EL中的映射值 [英] Access map value in EL using a variable as key
问题描述
我在EL中有一个Map
作为${map}
,我试图使用一个键来获取它的值,该键本身也是一个EL变量${key}
,其值是"1000"
.
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"
.
使用${map["1000"]}
可以,但是${map["$key"]}
无效.我在做什么错?如何使用变量作为键来获取Map
值?
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?
推荐答案
$
不是变量名的开头,它表示表达式的开头.您应该使用${map[key]}
来访问地图map
中的属性key
.
$
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
.
您可以在带有GET
参数的页面上尝试使用以下查询字符串,例如?whatEver=something
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]}"/>
这将输出:
whatEver: something
请参阅: https://stackoverflow.com/tags/el/info 并滚动到括号符号"部分.
See: https://stackoverflow.com/tags/el/info and scroll to the section "Brace notation".
这篇关于使用变量作为键访问EL中的映射值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!