Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
krishna na
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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
 
Shailesh Narkhede
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use nested c:forEach tag.
 
krishna na
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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.

 
Chinmaya Chowdary
Ranch Hand
Posts: 432
  • Mark post as helpful
  • send pies
  • 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


 
Harshana Dias
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • 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: 432
  • Mark post as helpful
  • send pies
  • 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().
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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)


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic