9

I'm working on JSPs with Struts2, I have to iterate on two lists, and change the background code of each <tr/> printed.

My JSP snippet:

<s:set var="counter" value="0" scope="page" />
<s:iterator value="listaContoCapitale" status="i">
    <s:iterator value="utilizzi" status="j">

    <s:if test="#counter == 0 || #counter % 2 == 0">
        <s:set var="trclass" value="'rigaSfondo1'" scope="page" />
    </s:if>
    <s:else>
        <s:set var="trclass" value="''" scope="page" />
    </s:else> 
    <tr class="${trclass}">
        ....tds
    </tr>
    </s:iterator>
    <s:set var="counter" value="here i have to change its value (increment it by1)" />
</s:iterator>

I need to increment my counter every step on the inner loop. Is there a way to increment my counter value by a simple struts tag? I know I could use Java scriptlet but I rather keep the JSP clear if possible.

2
  • In addition to Roman's answer, consider using a ternary to shorten the class decision logic Mar 26, 2013 at 17:05
  • What do you mean with ternary?
    – Emaborsa
    Mar 27, 2013 at 8:02

2 Answers 2

17

You aren't required to create a reference variable just use #i.index or #i.count inside the iterator. It's already incremented by the iterator tag itself.

Note that "count" is 1-based, "index" is 0-based.

Always check the docs.

If you still need your own counter

<s:set var="counter" value="0"/>

increment

<s:set var="counter" value="%{#counter+1}"/>
6
  • Need a separate counter because it needs to be based on the inner loop and not reset when the outer loop bumps up. Mar 26, 2013 at 17:04
  • I can't use the basic counter of an iterator, if you check my code you'll see that neither the outer loop nor the inner loop would give me a right counter that alternates from odd to even, in order to set the background color (trclass variable); in any case it doesn't matter if it starts with 0 or 1.... Before posting my question i tried your solution (<s:set var="counter" value="%{#counter+1}"/>) but it gave me an error by translating the JSP page. Tomorrow i'll try it again...
    – Emaborsa
    Mar 26, 2013 at 19:31
  • to check that odd or even you could use j.even or j.odd it's boolean values. The last statement should work, isn't it?
    – Roman C
    Mar 26, 2013 at 19:42
  • 2
    Works like a charm, use <s:property value="#counter"/> to display it.
    – Roman C
    Mar 26, 2013 at 19:57
  • <s:set var="counter" value="%{#counter+1}"/> works, but only if i remove the scope from de first definition.
    – Emaborsa
    Mar 27, 2013 at 8:29
1

You Can Use this easy Way to Do this struts2 increment

<s:set var="count" value="1"/>
<s:iterator value="yourlisthere">
<div><s:property value="#count" /><s:property/></div>
<s:set var="counter" value="%{#count+1}"/>
<s:set var="count" value="%{#counter}"/>
</s:iterator>

jsp struts2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.