• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question related to <c:forEach> & <c:if>

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have 2 questions, first one related to <c:forEach> & the other related to <c:if>

1) String[] arr1={"one", "two", "three"};
String[] arr2={"1","2","3"};
Map map = new HashMap();
map.put("StringArray",arr1);
map.put("NumberArray",arr2);
request.setAttribute("map",map);

<c:forEach var="item1" items="${map}">
${item1}
</c:forEach>


This code does only the half, ie retrieves only the rows of the map.

How can we iterate through the map ?

2) <c:forEach var="item" items="one,two,three" varStatus="instance">
<c:if test="${instance.count} eq 3">
This is third instance
</c:if>


<c:if test="${item} == 'three' ">
this is three
</c:if>


</c:forEach>

The 'If' statements are not working.

Please resolve my queries.
Thanks
 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use nested c:forEach tag.
 
krishna na
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even I tried It. I am getting Don't know how to iterate over "items" in <forEach> for the below code

<c:forEach var="item1" items="${map}">

<c:forEach var="i" items="item1">

${item1}

</c:forEach>

</c:forEach>

I am also looking for the answer related to <c:if> statement also.

Those who have cleared SCWCD & others who are planning to write SCWCD please help.

 
Ranch Hand
Posts: 437
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Krishna. If we execute, first we will get first map entry(key,value) inside <c:forEach>, which is of type java.util.Map.Entry. The string array object is present inside HashMap as value. So, we are not able to iterate here. We will iterate, unless we get string array. Eventhough if we use inner <c:forEach> tag, we are not able to iterate the string arrays.

Try using the following code:



We know, the ${item1} is same as<%= pageContext.getAttribute("item1") %>(inside forEach tag we didn't specify scope value, by default it is page scope). The pageContext.getAttribute("item1") returns java.util.Map.Entry. So, we are casting here. We get the string array through the getValue()...

In the second, there is syntax error, use
and


 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If we execute, first we will get first map entry(key,value) inside <c:forEach>, which is of type java.util.Map.Entry. The string array object is present inside HashMap as value. So, we are not able to iterate here. We will iterate, unless we get string array. Eventhough if we use inner <c:forEach> tag, we are not able to iterate the string arrays.



Isnt the first time the ${item1} refer to String arr1 because thats what first map key "StringArray" refer to?
 
Chinmaya Chowdary
Ranch Hand
Posts: 437
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Harshana. It comes as 'name=value' pair which is of type java.util.Map.Entry. <c:forEach> tag has no idea wether key(StringArray) refers to value(arr1). It simply iterate as name=value pairs. We have to get the value. So, we are using getValue().
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
About using forEach with a Map. Each iteration returns an instance of java.util.Map.Entry. To get a value from that, you can call getValue(), which will in your case will return a String[]. You can then iterate through that array using another forEach loop: (no need for scripting)


reply
    Bookmark Topic Watch Topic
  • New Topic