Reputation: 20356
I'm new to using JSP and need to figure out how to do this. I'd appreciate any pointers on how to do this ?
I need to display images in this table-like structure. To simplify the problem,
A B C
D E F
G H I
where each of these elements are a part of the Set names in action class.
Set<String> names = new HashSet<String>(0);
names.add("A");
names.add("B");
names.add("C");
names.add("D");
names.add("E");
names.add("F");
names.add("G");
names.add("H");
names.add("I");
Its fairly trivial to do it in java, however, I am having a hard time to figure out, how do I ask the iterator to point to next element manually.
<s:iterator value="names">
<s:property/>
I'd now like to point iterator to point to next or run a nested iterator loop here.
</s:iterator>
Upvotes: 1
Views: 224
Reputation: 4103
You can easily accomplish it with JSTL:
<table>
<tr>
<c:forEach items="names" var="name" varStatus="i">
<c:if test="${!i.first && !i.last && i.index % 3 == 0}">
</tr>
<tr>
</c:if>
<td><c:out value="${name}" /></td>
</c:forEach>
</tr>
</table>
Doing so, a new line (</tr><tr>
) will be added every 3 elements.
(not tested)
Upvotes: 1
Reputation: 6905
As CoolBeans said, you can use JSTL forEach loop.
If you'd like to see an example of that (alongside other good JSTL examples), check out JSTL Examples.
"Iterating over data structures" has some info and examples on what you're trying to do.
Upvotes: 0