1

I am using the following command to display values from 'userList'

<s:iterator value="userList" status="rowStatus">
  <tr class="even"> 
    <td><s:property value="tweet_id" /></td>
    <td><s:property value="message" /></td>
    <td><s:property value="created" /></td>
  </tr>
</s:iterator>

What is the use of status="rowStatus" in this command?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Siddharth
  • 884
  • 3
  • 11
  • 27

2 Answers2

7

From docs.

The iterator tag can export an IteratorStatus object so that one can get information about the status of the iteration, such as:

index: current iteration index, starts on 0 and increments in one on every iteration
count: iterations so far, starts on 1. count is always index + 1
first: true if index == 0
even: true if (index + 1) % 2 == 0
last: true if current iteration is the last iteration
odd: true if (index + 1) % 2 == 1

Example

   <s:iterator status="rowStatus" value='{0, 1}'>
      Index: <s:property value="%{#rowStatus.index}" /> <br />
      Count: <s:property value="%{#rowStatus.count}" /> <br />  
   </s:iterator>

will print

  Index: 0
  Count: 1
  Index: 1
  Count: 2

In your userList

userList count = 1 to userList.size();
userList index = 0 to userList.size() - 1;
Roman C
  • 49,761
  • 33
  • 66
  • 176
Ken de Guzman
  • 2,790
  • 1
  • 19
  • 33
5

In your case, none.

When iterating, the current object is pushed on top of the Value Stack. This means you can access it by simply using its name (along with the many other ways).

You can use this value as you wish for the value attribute.

But if (in a case different from your, but that you will certainly encounter soon) you need to put this value in a form field that will be submitted back to an(other) Action, targeting a List<YourObject> attribute, then you need to use IteratorStatus to mount the correct name attribute. For example:

SourceAction

private List<User> sourceUserList; 

TargetAction

private List<User> updatedUserList; 

JSP

<s:form action="targetAction">
  <s:iterator value="sourceUserList" status="rowStatus">

    <s:hidden name="updatedUserList[%{#rowStatus.index}].id" value="id"/>
    <s:property value="id" />

    <s:textfield name="updatedUserList[%{#rowStatus.index}].name" value="name" />
    <s:textfield name="updatedUserList[%{#rowStatus.index}].age"  value="age"  />

  </s:iterator>
  <s:submit/>
</s:form>

Got it ?

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243