Torres
Torres

Reputation: 5400

Iterate 2 Lists in parallel in a JSP

I need to iterate through 2 list in paralell in a JSP. I have list1 and list2, and I need to iterate both at same time, or at least access to the list2 with the same index list1 is using, example:

Ideally something like this:

logic:iterate name="list1" id="object1" scope="session" indexId="index"
bean:define id="object2" name="list2[index]"

Thanks in advance

Upvotes: 1

Views: 3143

Answers (2)

Torres
Torres

Reputation: 5400

I´ve solved the problem with this structure:

<logic:iterate name="list1" id="object1" scope="session" indexId="index">

    <logic:iterate name="list2" id="objAux" scope="session" offset="index" length="1">
        <bean:define id="object2" name="objAux"/>
    </logic:iterate>

...
</logic:iterate>

Upvotes: 3

Boris Pavlović
Boris Pavlović

Reputation: 64632

What about merging these two lists in Java and then iterating resulting list in JSP?

public List<Pair<String, String>> merge(List<String> one, List<String> two)
{
  List<Pair<String, String> result = new ArrayList<Pair<String, String>>();
  for (int i = 0, i < Math.max(one.size(), two.size()); i++)
  {
    result.add(new Pair(one.size < i ? one.get(i) : null, two.size() < i ? two.get(i) : null));
  }
  return result;
}

Upvotes: 0

Related Questions