我在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请参阅: stackoverflow/tags/el/info 并滚动到括号符号"部分.
See: stackoverflow/tags/el/info and scroll to the section "Brace notation".
更多推荐
使用变量作为键访问EL中的映射值
发布评论