Specify the key in square brackets and return the corresponding value as the expression value. For example, expression $ {map ['key']} returns the value associated with the "key" key in the map referenced by the Map identifier.
When the expression value in the items attribute of forEach is java. util. Map, the type of the variable named IN var is java. util. Map. Entry. If var = entry, use the expression $ {entry. key} to get the key name. Use $ {entry. value} to get the value of each entry. This is because java. util. Map. Entry objects have the getKey and getValue methods. The expression language complies with the naming conventions of JavaBean.
<%
Map <String, String> map2 = new HashMap ();
Map2.put ("a", "hello world ");
Map2.put ("B", "this is map ");
Request. setAttribute ("map2", map2 );
%>
<Br>
Key-Value Pair traversal <br>
<C: forEach var = "item" items = "$ {map2}">
$ {Item. key }>$ {item. value} <br>
</C: forEach>
Key traversal <br>
<C: forEach var = "item" items = "$ {map2}">
$ {Item. key} <br>
</C: forEach>
Value traversal <br>
<C: forEach var = "item" items = "$ {map2}">
$ {Item. value} <br>
</C: forEach>
<Body>
<Br>
<%
List <String> list = new ArrayList <String> ();
List. add ("first ");
List. add ("second ");
List <String> list2 = new ArrayList <String> ();
List2.add ("aaaaaa ");
List2.add ("bbbbbb ");
Map <String, List <String> map = new HashMap ();
Map. put ("a", list );
Map. put ("B", list2 );
Request. setAttribute ("map", map );
%>
Obtain the list value through the key and traverse the list <br>
<C: forEach var = "item" items = "$ {map ['a']}">
$ {Item} <br>
</C: forEach> <br>
<C: forEach var = "item" items = "$ {map ['B']}">
$ {Item} <br>
</C: forEach> <br>
The value in the map is the list, and each item in the list is directly traversed. <br>
<C: forEach var = "item" items = "$ {map}">
<C: forEach items = "$ {item. value}" var = "it">
$ {It} <br>
</C: forEach>
</C: forEach>