File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Question related to <c:forEach> & <c:if> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Question related to <c:forEach> & <c:if>" Watch "Question related to <c:forEach> & <c:if>" New topic
Author

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

krishna na
Greenhorn

Joined: Mar 17, 2009
Posts: 14
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

Joined: Jul 10, 2008
Posts: 368
You can use nested c:forEach tag.


Thanks,
Shailesh
krishna na
Greenhorn

Joined: Mar 17, 2009
Posts: 14
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

Joined: Apr 21, 2008
Posts: 432
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

Joined: Jun 11, 2007
Posts: 327

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

Joined: Apr 21, 2008
Posts: 432
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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)



[My Blog]
All roads lead to JavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question related to <c:forEach> & <c:if>
 
Similar Threads
Garbage collection
How to set and get values from HashMap using JSTL
Adding elements of two arrays
(String[] args) vs. (String args[])
Standard Tags, Design Pattern Qs