Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struts2 increment a previous set <s:set /> value

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.

like image 897
Emaborsa Avatar asked Mar 26 '13 15:03

Emaborsa


2 Answers

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}"/>
like image 95
Roman C Avatar answered Oct 31 '22 07:10

Roman C


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

like image 1
Ziyad Beg Avatar answered Oct 31 '22 06:10

Ziyad Beg