开发者

JSF: logic based on iteration index

开发者 https://www.devze.com 2023-01-21 02:15 出处:网络
Pardon me for the title, that\'s the best my limited brain can came up this late. So, i have a list of string, something like [abc, def, ghi].

Pardon me for the title, that's the best my limited brain can came up this late.

So, i have a list of string, something like [abc, def, ghi].

The question: in JSF, how do I iterate the list and create a string that look like this "abc, def, ghi" (notice the commas)?

For those who have the urge to tell me that I'd better use a Java method to concatenate the 开发者_运维百科string, hear this: every member of the list should be rendered as a separate commandLink.

If plain JSF it would look like:

<h:commandLink>abc</h:commandLink>, <h:commandLink>def</h:commandLink>, <h:commandLink>ghi</h:commandLink>


Assuming that #{bean.items} returns List<String> or String[], in JSF 1.x you can use JSTL c:forEach with varStatus. It gives you a handle to LoopTagStatus which has a isLast() method.

<c:forEach items="#{bean.items}" var="item" varStatus="loop">
    <h:commandLink value="#{item}" /><c:if test="#{!loop.last}">, </c:if>
</c:forEach>

In Facelets as shipped with JSF 2.x, same functionality is available by ui:repeat.

<ui:repeat value="#{bean.items}" var="item" varStatus="loop">
    <h:commandLink value="#{item}" />#{!loop.last ? ', ' : ''}
</ui:repeat>
0

精彩评论

暂无评论...
验证码 换一张
取 消