MadhuB
MadhuB

Reputation: 97

struts 2 for loop

I want to loop a code some no of times like this

<s:iterator value="#session.count">
        <TD WIDTH='10%' BGCOLOR='#000080'>&nbsp;</TD>
    </s:iterator>

based on value 'count', stored in the session, those many times I need to add tags as above but it is not looping as expected.

what I want to know is is for arrays or collection objects. but how to loop the code N no of time using Struts 2 tags in JSP.

Upvotes: 0

Views: 13695

Answers (3)

Dave Newton
Dave Newton

Reputation: 160170

For a simple for-loop, use the begin and end attributes:

<s:iterator begin="0" end="%{#session.count}">
  <td width='10%' bgcolor='#000080'>&nbsp;</td>
</s:iterator>

I'd also use CSS a bit better.

See the iterator tag docs.


Now you've mentioned you're using a very old version of S2. In that case, use JSTL's c:forEach tag, there's no reason to use an S2 tag. It has the same begin/end semantics, and still makes much for sense than creating a useless list.

Upvotes: 6

Uchenna Nwanyanwu
Uchenna Nwanyanwu

Reputation: 3204

You can see iterator examples here and here.

Upvotes: 0

Jaiwo99
Jaiwo99

Reputation: 10017

seems it is very easy.

use this:

<s:subset source="your_source" count="#session.count">
    <s:iterator>
        <td>test</td>
    </s:iterator>
</s:subset>

Upvotes: 0

Related Questions