Use <C:Foreach> with Hashmap

Use c:forEach with HashMap

Yes, this is perfectly acceptable.

When you use <c:forEach> to iterate over a Map, each item in the iteration is an instance of Map.Entry. So given your example:

<c:forEach var="type" items="${types}">
Key is ${type.key}
Value is ${type.value}
</c:forEach>

How to iterate HashMap using JSTL forEach loop?

Try this,

suppose my MAP is :-

Map<String, String> countryList = new HashMap<String, String>();
countryList.put("United States", "Washington DC");
countryList.put("India", "Delhi");
countryList.put("Germany", "Berlin");
countryList.put("France", "Paris");
countryList.put("Italy", "Rome");

request.setAttribute("capitalList", countryList);

So in JSP ,

<c:forEach var="country" items="${capitalList}">
Country: ${country.key} - Capital: ${country.value}
</c:forEach>

Hope this helps !

How can we iterate over an HashMap in JSTL?

This is the HaspMap creation in the java method:

Map<String, String> countryList = new HashMap<String, String>();
countryList.put("United States", "Washington DC");
countryList.put("India", "Delhi");
countryList.put("Germany", "Berlin");
countryList.put("France", "Paris");
countryList.put("Italy", "Rome");

The first argument in the HaspMap is the Key and second argument is the value. Now you need to access that in the jsp just with the key and the value as:

<c:forEach var="country" items="${capitalList}">
Country: ${country.key} - Capital: ${country.value}
</c:forEach>

Thats it. And in your case too, you can loop in jsp with key and value. Hope this helps.

How to iterate hashmap containing Object and array object, in jsp using jstl

<c:forEach var="qrCode" items="${qrCodeMap}">
For Page: ${qrCode.key} , QR Codes :<br/>
<c:forEach var="result" items="${qrCode.value}">
${result.attr}<br/>
</c:forEach>
</c:forEach>

Where attr is the attribute you want to show from Result.

Iterate over elements of List and Map using JSTL c:forEach tag

Suppose ${list} points to a List<Object>, then the following

<c:forEach items="${list}" var="item">
${item}<br>
</c:forEach>

does basically the same as as following in "normal Java":

for (Object item : list) {
System.out.println(item);
}

If your ${list} is a List<Person> where Person is a Javabean having name and email properties represented by getName() and getEmail() getter methods, then the following

<c:forEach items="${list}" var="person">
${person.name}<br>
${person.email}<br>
</c:forEach>

does basically the same as as following in "normal Java":

for (Person person : list) {
System.out.println(person.getName());
System.out.println(person.getEmail());
}

If you have a List<Map<K, V>> instead, then the following

<c:forEach items="${list}" var="map">
<c:forEach items="${map}" var="entry">
${entry.key}<br>
${entry.value}<br>
</c:forEach>
</c:forEach>

does basically the same as as following in "normal Java":

for (Map<K, V> map : list) {
for (Entry<K, V> entry : map.entrySet()) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
}

The key and value are here not special methods or so. They are actually getter methods of Map.Entry object (click at the blue Map.Entry link to see the API doc). In EL (Expression Language) you can use the . dot operator to access getter methods using "property name" (the getter method name without the get prefix), all just according the Javabean specification.

See also:

  • Places where JavaBeans are used?
  • Show JDBC ResultSet in HTML in JSP page using MVC and DAO pattern
  • javax.el.PropertyNotFoundException: Property 'foo' not found on type com.example.Bean


Related Topics



Leave a reply



Submit