File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes c:forEach Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "c:forEach" Watch "c:forEach" New topic


Mel Calvin

Joined: Sep 11, 2007
Posts: 9
Consider the following code:

ArrayList list1 = new ArrayList();
ArrayList list2 = new ArrayList();
ArrayList list3 = new ArrayList();
list1.add(0, "list1-row1");
list1.add(1, "list1-row2");
list1.add(2, "list1-row3");
list2.add(0, "list2-row1");
list3.add(2, "list3-row3"):

HashMap map = new HashMap();
map.put("List1", list1);
map.put("List2", list2);
map.put("List3", list3);
request.setAtttribute("MAP", map);
forwarding to the jsp-page...

If I in a jsp-page wanted to print out the first rows the three arrays, how could that be achieved? I.e. retrieve only one element per iteration per array. I just can't get this to work..

[ September 17, 2007: Message edited by: Bear Bibeault ]
Amol Nayak
Ranch Hand

Joined: Oct 26, 2006
Posts: 218
If we have three Lists bound to request scope with names list1, list2 and list3 and assuming that all three lists have same size we can do this.

If you really want to use HashMap and this logic to be generic then i would recommend you to write a tag and implement the logic in the tag class.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63858

It's not clear what you are trying to achieve. What do you mean by "retrieve only one element per iteration per array"?

By nature, only one element will be retrieved per iteration so I'm not sure what's "special" about what you are trying to do?

[Asking smart questions] [About Bear] [Books by Bear]
Mel Calvin

Joined: Sep 11, 2007
Posts: 9
I have a hashmap of arrays, if I iterate over the map and prints the elements, it will print the arrays as a whole. I want to print the elements of the arrays which is not that easy, however, I have come oup with a solution:

This code works..
I agree. Here's the link:
subject: c:forEach
jQuery in Action, 3rd edition