Struts 逻辑:迭代

问题描述 投票:0回答:2

我正在使用 struts

logic:iterate
,我需要更改它写出的最后一行的样式。

我在迭代标签上有一个

bean:size
indexId
,但我无法找出逻辑组合来表示是否
bean:size = indexId
,因为大小是集合的大小,而最大
indexId
始终是因为从 0 开始,所以大小变为 -1。

java struts
2个回答
2
投票

我上次使用 Struts 是在今年早些时候,所以让我用最专业的知识来解释一下,

无法使用

logic:iterate
确定集合的长度(请参阅解释 here)。您需要执行以下操作:

假设您的收藏位于

request.setAttribute("collections", allMyCollections);

可以使用

EL
(表达式语言)来确定大小,并使用
c:if
来确定它们是否相等,即这样的效果:

<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>

否则,使用

<bean:size />
获取集合的大小,将其设置为变量,然后您将使用 scriplet 获取存储的集合大小,并使用
<logic:equal>
标签查看最后一个索引是否位于
collections.size() -1
(但这很麻烦)。

希望这有帮助。

PS 代码很粗糙......


0
投票

这可能来得晚了,但我认为这更干净一点......

<logic:lessThan name="currentIndex" value="${myCollectionSize - 1}">
    Some style...
</logic:lessThan>
<logic:equal name="currentIndex" value="${myCollectionSize - 1}">
    Some style on the last element...
</logic:equal>

或在 CSS 中:

someElement:last-of-type {}

.someClass:last-child {}

希望有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.