<c:forEach> iterating through multiple collections in parallel

Hi there,

I have two arrayLists, one contains a list of ids and the other contains a list of names. The ids and the names in a particular position in their arrayList correspond so that the id in position one corresponds to the name in position one.

I want to use <c:forEach> to iterate through these arraylists so that the names and corresponding Ids are printed together. Is this possible?

For example, I want to use the id as part of a url and the name to be what the user clicks on.

<c:forEach var="k" items="${inbox.id}">

<a href="ProfileServlet?id=${k}">${inbox.name[x]}</a>,

</c:forEach>

Is there a way to perhaps increment a variable, x in ${inbox.name[x]}

Hope Im making myself clear! Thanks!

[898 byte] By [bigadjayea] at [2007-11-27 11:50:35]
# 1

Better, use Hashtable (key, value pairs)

Hashtable table = new Hashtable();

ArrayList names = new ArrayList();

.....

names = nameBO.getNames(.,.);

.....

table.put(id, names); //populate the Hashtable

to fetch:

if (table != null) {

Set set= table.keySet () ;

Iterator iter = set.iterator () ;

while ( iter.hasNext () )

{

String id = (String)iter.next();

ArrayList nameData = (ArrayList)table.get(data);

for(int j = 0; j < nameData.size(); j++)

{

String name = nameData.get[j].toString();

}

}

}

skp71a at 2007-7-29 18:32:26 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Thanks for the reply, actually I simiplified my problem to make it easier to explain. What happens if I want to obtain values from 6 collections where values correspond? A hashmap only takes a key and a value. Therefore would it work?

bigadjayea at 2007-7-29 18:32:26 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

almost anything is possible. Whether it is advisable is another matter.

The varStatus attribute of the c:forEach loop exposes a index/counter for you to use.

<c:forEach var="k" items="${inbox.id}" varStatus="status">

<a href="ProfileServlet?id=${k}">${inbox.name[status.index]}</a>

</c:forEach>

However I think you could use a better datastructure than parallel lists.

I would have an object "Inbox" which had attributes id, name ... and then iterate through a list of them.

public class Inbox{

private int id;

private String name;

// getter/setter methods for id/name

}

You then expose a list of Inbox objects to your jsp page (one list as opposed to 6)

<c:forEach var="inbox" items="${myListOfInboxes}">

<a href="ProfileServlet?id=${inbox.id}">${inbox.name}</a>,

</c:forEach>

And using the c:url tag just for kicks :-)

<c:forEach var="inbox" items="${myListOfInboxes}">

<c:url value="ProfileServlet" var="theURL">

<c:param name="id" value=${inbox.id}">

</curl>

<a href="${theURL}">${inbox.name}</a>,

</c:forEach>

evnafetsa at 2007-7-29 18:32:26 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

[nobr]Thanks for the reply, what you have said makes a lot of sense! :D

I have opted for the option you recommended and I am now accessing an arrayList of inbox beans. However, when I do the c:forEach, for some reason, rather than displaying the contents of each bean it is displaying the same bean multiple times!

<c:forEach var="inbox" items="${homeInboxList}">

<a href="ProfileServlet?action=profile&profileid=${inbox.fromUserId}"><img src="${inbox.imagePath}"/></a>

<a href="InboxServlet?action=message&messageid=${inbox.messageId}"> ${inbox.firstname} ${inbox.lastname}</a> ${inbox.date}<br/>

</c:forEach>

It retrieves te correct number of beans but it seems to repeat the same bean.

Any ideas what I am doing wrong? Maybe something obvious?[/nobr]

bigadjayea at 2007-7-29 18:32:26 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Dont worry! Problem solved.

The problem was in my servlet, i wasn't initiating a new

bean for each row of my result set. I kept using the same bean and consequently I was overwriting the previous rows. New it had to be a silly mistake.

Thanks so much for your help! :D

bigadjayea at 2007-7-29 18:32:26 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...