[reproduced] in Jstl el processing Java.util.Map, and nested list of the situation

Source: Internet
Author: User

(This article has not found the source ~)

In El, the square bracket operator is used to retrieve the elements of an array and a collection. For a collection that implements the Java.util.Map interface, the square bracket operator uses the associated key to find the value stored in the map.

The key is specified in square brackets and the corresponding value is returned as the value of the expression. For example, the expression ${map[' key '} returns the value associated with the "key" key in the map referenced by the map identifier.
When the value of the expression in the Items property of foreach is Java.util.Map, then the type of the variable named in Var is java.util.Map.Entry. At this point var=entry, use the expression ${entry.key} to get the key name. Use the expression ${entry.value} to get the value of each entry. This is because the Java.util.Map.Entry object has Getkey and GetValue methods, and the expression language adheres to the naming convention 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><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);
%>
get the list value by 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>
map value is a list that iterates through each item in the list <br>
<c:foreach var= "Item" items= "${map}" >
<c:foreach items= "${item.value}" var= "it" >
${it}<br>
</c:forEach>
</c:forEach>

[reproduced] in Jstl el processing Java.util.Map, and nested list of the situation

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.