Processing java. util. Map and nested List in JSTL EL

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.