Tone Tone - 5 months ago 19
Java Question

Struts logic:iterate

I'm using struts

logic:iterate
and I need to change the style on the last row that it writes out.

I have a
bean:size
, and
indexId
on the iterate tag, but I can't figure out a logic combination to say if
bean:size = indexId
because size is the size of the collection, and the max
indexId
is always going to size -1 because of starting at 0.

Answer

My last time I did Struts was early this year, so let me explain with best knowledge,

There is no way of determining the length of the collections by using logic:iterate (See explanation here). What you will have to do is the following:

Assuming your collections is placed under request.setAttribute("collections", allMyCollections);

You can use EL (Expression Language) to determine the size and determine if they are equal by using c:if, i.e. in this effect:

<logic:iterate name="collections" id="curElement">
    <c:if test="${curElement.indexId == ${fn:length(collections) - 1}}">
        <!-- It is pretty messy ...but you get the idea -->
        <!-- We are the last element...whoohoo!!! -->
    </c:if>
</logic:iterate>

Otherwise, use <bean:size /> to get the size of the collections, set it to a variable and you'll use scriplets to get the stored collections size and use <logic:equal> tags to see if the last index is at collections.size() -1 (but that's cumbersome).

Hope this helps.

PS The code is rough....