struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Laurie Harper <lau...@holoweb.net>
Subject Re: <logic:iterate> how to test the 1st and last iteration ?
Date Fri, 27 Apr 2007 21:00:46 GMT
Affan Qureshi wrote:
>> piloupy GOTTAPIL wrote:
>>> Hi,
>>>
>>> What I want to do is quite simple. I'd like to know how to test in a
>>> <logic:iterate> when I'm at the first or last iteration.
>>>
>>> My present problem is to parse a collection of String and display the
>>> results like this :
>>>
>>> ### CODE : begin ###
>>> value1, value2, value3
>>> ### CODE : end ###
>>>
>>> And as you can see, for the last iteration, I mustn't displey the
>>> comma character.
>>>
>>> My present code is :
>>>
>>> ### CODE : begin ###
>>> <logic:iterate id="e" name="myCollection">
>>>   <bean:write name="e" property="name" />,
>>> </logic:iterate>
>>> ### CODE : end ###
>>>
>>> Thanks in advance :)
>>>
> 
> 
> Declare a counter variable in the loop. You can get the size of the 
> collection in a page variable with the <bean:size > tag and use that to 
> compare the current iteration. And then dont display the comma at the last 
> iteration. 

This is trivial to do with JSTL; something like this should work:

   <c:forEach items='myCollection' var='e' varStatus='status'>
     <c:out value='e'>
     <c:if test='${not status.last}'>
       <c:out value=','/>
     </c:if>
   </c:forEach>

Strut 1's logic:iterate tag only provides a simple integer status 
variable, which is why you have to jump through a few extra hoops using 
that. Just one of the reasons JSTL is preferred over the logic: 
collection :-)

L.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message