Paolo
Paolo

Reputation: 2472

Use variable as a map key

I have something like this

<c:set var="x" value="a" />

Supposing I have a map like this

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

I want to use variable ${x} as map key in an EL expression, like this

${map.x}

(which is wrong) in order to print

1

(or 2) depending on the value of ${x}

Upvotes: 2

Views: 6226

Answers (1)

BalusC
BalusC

Reputation: 1108742

You need to use the brace notation [] in order to use dynamic keys on a Map.

${map[x]}

See also:

Upvotes: 6

Related Questions