使用变量作为映射键

Pao*_*olo 2 jsp key el hashmap

我有类似的东西

<c:set var="x" value="a" />
Run Code Online (Sandbox Code Playgroud)

假设我有这样的地图

Map<String, String> map = new HashMap<>();
map.put("a", "1");
map.put("b", "2");
request.setAttribute("map", map);
Run Code Online (Sandbox Code Playgroud)

我想${x}在EL表达式中使用变量作为映射键,就像这样

${map.x}
Run Code Online (Sandbox Code Playgroud)

(这是错的)以便打印

1
Run Code Online (Sandbox Code Playgroud)

(或2)取决于的价值 ${x}

Bal*_*usC 6

您需要使用括号表示法[]才能在a上使用动态键Map.

${map[x]}
Run Code Online (Sandbox Code Playgroud)

也可以看看: