下午好,我正在尝试在表格列中显示以“,”
分隔的项目列表我目前的格式是信用卡后面会显示逗号,但如果没有电子转帐付款,最后一张卡后会留下逗号。我附上了问题的截图..
这是我的代码:
<%if (creditCards.size() > 0) {%>
<% for (CreditCardPaymentVO card : creditCards) {%>
<%= card.getType()%>
,
<%}%>
<%}%>
<% if (eft != null) {%>
<%= "EFT"%>
<%} %>
任何想法如何让逗号在最后一张信用卡后不显示? 谢谢你的时间和精力!
答案 0 :(得分:3)
Scriptlet不再酷了。试试JSTL:
<c:forEach var="creditCard" items="${creditCards}" varStatus="status">
<c:out value="${creditCard.type}"/>
<c:if test="${!status.last || eft}">, </c:if>
</c:forEach>
<c:if test="${eft}">EFT</c:if>
基于JSTL forEach separator的示例。
答案 1 :(得分:1)
我建议使用JSTL,如下所示:
<c:forEach items="${creditCards}" var="creditCard" varStatus="index">
<c:out value="${creditCard.type}" />
<c:if test="${not index.last}">,</c:if>
</c:forEach>
答案 2 :(得分:0)
我不熟悉JSTL,但在其他编程语言中,我通常会为数组找到'join'方法。像JS这样的东西:
["VISA", "Mastercard", "Amex"].join(', ');
=> "VISA, Mastercard, Amex"
如果这种语言中没有这样的方法,我道歉。但它被标记为Javascript。