needreebas
needreebas

Reputation: 61

How to display a row number using Struts <logic:iterate>

I have an ArrayList and I'mam using Struts 1 <logic:iterate> to iterate and display records in the table. Since the list size is bigger now I need to the display the row number in the first column. How to achieve this?

Tried option:1

 <% long count = 1;%>
 <logic:iterate id="iter" name="empForm" property="empList"  >
 <tr>  <td> <%= count++ %>  <td>  </tr>

Working but scriptlet should not be used.

Tried option:2

<logic:iterate id="iter" name="empForm" property="empList" indexId="index" >

 <tr>  <td> <bean:write name="index"/> <td>  </tr>

Problem: working index starts like 0,1,2 that needs to be as 1,2,3

How to show as 1,2,3 instead of 0,1,2?

Upvotes: 1

Views: 9856

Answers (2)

Roman C
Roman C

Reputation: 1

Instead of

<bean:write name="index"/>

you can use

<c:out value="${index + 1}"/>

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 692191

You're using logic:iterate, which is explicitely documented as "should not be used anymore", by a framework (Struts 1) that is itself deprecated and officially abandoned for several years. Stop using Struts 1, really.

And, in the meantime, at least learn the JSP EL and the JSTL. They exist for more that 10 years:

<c:forEach var="employee" varStatus="loopStatus" values="${empForm.empList}">
  <tr>
    <td>${loopStatus.index + 1}<td>
    <td><c:out value="${employee.name}"/></td>
  </tr>
</c:forEach>

Upvotes: 1

Related Questions