Re: Still need help with layered Map iteration in JSP
I guess the first thing to make sure you understand is that, when iterating over a Map, the object exposed by JSTL via the var attribute is of type Map.Entry. It's equivalent to doing a map.entrySet().iterator(). As for getting the size of an array, Collection, or Map, use the bean:size tag. Quoting Erik Weber [EMAIL PROTECTED]: OK, I'll take ANY WAY TO DO THIS AT ALL. I have a c:forEach over a Map. The value for each key in the Map can be an array of objects, or I can make it a List. I don't care at this point. All I want to do is get the length of this array or List, before I start iterating over *it*, during the current iteration of the Map. I am told that I access the array or List like this: 1) Given this Map iteration: c:forEach items=myMap var=currentMapEntryWhichIsBothAKeyAndAValueTogether 2) access the value of the current Map entry (which will be either an array or a List) like this: ${currentMapEntryWhichIsBothAKeyAndAValueTogether.value} But I have tried all of these, none of them work: c:when test=${currentMapEntryWhichIsBothAKeyAndAValueTogether.value.length 0}!-- where the value of the Map entry is an array -- c:when test=${currentMapEntryWhichIsBothAKeyAndAValueTogether.value.size 0}!-- where the value of the Map entry is a List -- c-rt:when test=${fn:length(currentMapEntryWhichIsBothAKeyAndAValueTogether.value) 0}!-- where the value of the Map entry is either an array or a List -- Not that they would have worked, because I was merely guessing in every case. This is the type of thing that makes me sorry I ever got involved with tag libraries. I have a Map of Lists. I didn't invent this. This is a common structure to use in Java. So someone please show me a single document that tells you how to work with JSTL tags (or any other tags) to do such a simple thing as to do a nested iteration of a Map of Lists or a Map of arrays! Here is the Java equivalent of what I need to do: //Map, stored as request attribute, has String keys, MyClass[] values Map map = getMapWhichIsARequestAttribute(); Iterator i = map.keySet().iterator(); while (i.hasNext()) { String key = (String) i.next(); MyClass[] values = (MyClass[]) map.get(key); int size = values.length; // *HOW DO I GET THIS VALUE USING TAGS?* if (size 0) { // *HOW DO I IMPLEMENT THIS TEST USING TAGS?* if (size 5) { //do setup for a big array for (int x = 0; x size; x++) { //do output for each item in array } } else { //do setup for a small array for (int x = 0; x size; x++) { //do output for each item in array } } } else { //no records found for this key } } Failing that, I would appreciate it if someone could show me how to do this using Struts tags. If I had used scriptlets, I would have been done hours ago. Thanks, Erik Erik Weber wrote: I have a c:forEach where items refers to a Map, and var refers to the current Map entry. c:forEach items=myMap var=currentEntry The value for each Map entry is an array. How do I switch on the length of this array? I tried this: c:when test=${currentEntry.value.length 5} But I get a syntax error; it says I supplied the . operator with an index value of type java.lang.String to be applied to an array, but that the value cannot be converted to an integer. Is there some sort of way I can use items.length or items.size or something similar? More importantly, WHERE IS THIS DOCUMENTED? I have searched the Internet for decent documents on JSTL and cannot find a simple complete guide to tag usage -- something similar to what Struts has for its tags. For example, where is a document that explains what you can do with var, varStatus, items, etc. on a c:forEach tag, and covers all the tags? The Sun web services tutorial does not do this, it only gives examples. Thank you, Erik Erik Weber wrote: Thanks again. Erik Kris Schneider wrote: c:forEach supports a varStatus attribute. The value of that atrribute is a String that names an instance of javax.servlet.jsp.jstl.core.LoopTagStatus. The LoopTagStatus instance has nested visibility so that it's only available within the enclosing c:forEach tag. LoopTagStatus exposes a number of properties, but the one you're probably interested in is index: .. c:forEach var=bean varStatus=status items=${entry.value} %-- ${status.index} is the current index --% ... /c:forEach .. Quoting Erik Weber [EMAIL PROTECTED]: How can I refer to the index of the current iteration with c:forEach (analogous to the indexId attribute to logic:iterate)? Thanks, Erik Kris Schneider wrote: %@ taglib
Re: Still need help with layered Map iteration in JSP
Kris, once again you have helped me get my work done. bean:size did the trick. I was looking over and over again for a JSTL tag or expression to do this; I probably never would have thought of bean:size. To complete the post for any other reader who is as suicidal as I am after trying to figure this out: c:forEach items=myMap var=currentEntry!-- where Map has String keys and array or List values -- bean-el:size collection=${currentEntry.value} id=size/!-- page scope variable size (of type java.lang.Integer) will now contain the size of the array or list associated with the current Map key -- . . . c:when test=${size 5}!-- how to test how big the current array or List is -- . . . c:forEach items=currentEntry.value var=currentItemInArrayOrList varStatus=iteratorStatus!--- how to iterate the current array or List -- . . . c:out value=${currentItemInArrayOrList.someProperty}/!-- how to print some property of the current item in the current array or List -- . . . c:when test=${iteratorStatus.index % 2 == 0}!-- how to tell if you are on an even-numbered row in your iteration -- and so on . . . Thank you, Erik P.S. I would love to know how you learned all this (I suppose I must buy a JSTL book). Erik Kris Schneider wrote: I guess the first thing to make sure you understand is that, when iterating over a Map, the object exposed by JSTL via the var attribute is of type Map.Entry. It's equivalent to doing a map.entrySet().iterator(). As for getting the size of an array, Collection, or Map, use the bean:size tag. Quoting Erik Weber [EMAIL PROTECTED]: OK, I'll take ANY WAY TO DO THIS AT ALL. I have a c:forEach over a Map. The value for each key in the Map can be an array of objects, or I can make it a List. I don't care at this point. All I want to do is get the length of this array or List, before I start iterating over *it*, during the current iteration of the Map. I am told that I access the array or List like this: 1) Given this Map iteration: c:forEach items=myMap var=currentMapEntryWhichIsBothAKeyAndAValueTogether 2) access the value of the current Map entry (which will be either an array or a List) like this: ${currentMapEntryWhichIsBothAKeyAndAValueTogether.value} But I have tried all of these, none of them work: c:when test=${currentMapEntryWhichIsBothAKeyAndAValueTogether.value.length 0}!-- where the value of the Map entry is an array -- c:when test=${currentMapEntryWhichIsBothAKeyAndAValueTogether.value.size 0}!-- where the value of the Map entry is a List -- c-rt:when test=${fn:length(currentMapEntryWhichIsBothAKeyAndAValueTogether.value) 0}!-- where the value of the Map entry is either an array or a List -- Not that they would have worked, because I was merely guessing in every case. This is the type of thing that makes me sorry I ever got involved with tag libraries. I have a Map of Lists. I didn't invent this. This is a common structure to use in Java. So someone please show me a single document that tells you how to work with JSTL tags (or any other tags) to do such a simple thing as to do a nested iteration of a Map of Lists or a Map of arrays! Here is the Java equivalent of what I need to do: //Map, stored as request attribute, has String keys, MyClass[] values Map map = getMapWhichIsARequestAttribute(); Iterator i = map.keySet().iterator(); while (i.hasNext()) { String key = (String) i.next(); MyClass[] values = (MyClass[]) map.get(key); int size = values.length; // *HOW DO I GET THIS VALUE USING TAGS?* if (size 0) { // *HOW DO I IMPLEMENT THIS TEST USING TAGS?* if (size 5) { //do setup for a big array for (int x = 0; x size; x++) { //do output for each item in array } } else { //do setup for a small array for (int x = 0; x size; x++) { //do output for each item in array } } } else { //no records found for this key } } Failing that, I would appreciate it if someone could show me how to do this using Struts tags. If I had used scriptlets, I would have been done hours ago. Thanks, Erik Erik Weber wrote: I have a c:forEach where items refers to a Map, and var refers to the current Map entry. c:forEach items=myMap var=currentEntry The value for each Map entry is an array. How do I switch on the length of this array? I tried this: c:when test=${currentEntry.value.length 5} But I get a syntax error; it says I supplied the . operator with an index value of type java.lang.String to be applied to an array, but that the value cannot be converted to an integer. Is there some sort of way I can use items.length or items.size or something similar? More importantly, WHERE IS THIS DOCUMENTED? I have searched the Internet for decent documents on JSTL and cannot find a simple complete guide to tag usage -- something similar to what
Re: Still need help with layered Map iteration in JSP
It's certainly not a bad idea to grab a JSTL book, but the spec itself is quite readable and full of good information: http://jcp.org/aboutJava/communityprocess/final/jsr052/ The only JSTL book I've even partially read is JSTL in Action by Shawn Bayern and I have no reservations recommending it. There are also plenty of web resources available: http://www.javaworld.com/javaworld/jw-02-2003/jw-0228-jstl.html http://www.onjava.com/pub/a/onjava/2002/08/14/jstl1.html http://www.onjava.com/pub/a/onjava/2002/09/11/jstl2.html http://www.onjava.com/pub/a/onjava/2002/03/13/jsp.html http://www.onjava.com/pub/a/pub/a/onjava/2002/05/08/jstl.html http://java.sun.com/developer/technicalArticles/javaserverpages/faster/ http://java.sun.com/developer/technicalArticles/Intl/MultilingualJSP/ etc. Quoting Erik Weber [EMAIL PROTECTED]: Kris, once again you have helped me get my work done. bean:size did the trick. I was looking over and over again for a JSTL tag or expression to do this; I probably never would have thought of bean:size. To complete the post for any other reader who is as suicidal as I am after trying to figure this out: c:forEach items=myMap var=currentEntry!-- where Map has String keys and array or List values -- bean-el:size collection=${currentEntry.value} id=size/!-- page scope variable size (of type java.lang.Integer) will now contain the size of the array or list associated with the current Map key -- . . . c:when test=${size 5}!-- how to test how big the current array or List is -- . . . c:forEach items=currentEntry.value var=currentItemInArrayOrList varStatus=iteratorStatus!--- how to iterate the current array or List -- . . . c:out value=${currentItemInArrayOrList.someProperty}/!-- how to print some property of the current item in the current array or List -- . . . c:when test=${iteratorStatus.index % 2 == 0}!-- how to tell if you are on an even-numbered row in your iteration -- and so on . . . Thank you, Erik P.S. I would love to know how you learned all this (I suppose I must buy a JSTL book). Erik -- Kris Schneider mailto:[EMAIL PROTECTED] D.O.Tech http://www.dotech.com/ - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]