Java JSP for-each 标签中的逗号分隔值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18784501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Comma separated values within JSP for-each tag
提问by blackpanther
I am trying to get a JSTL <c:forEach>
tag to work so that it would print a list of names as follows:
我试图让一个 JSTL<c:forEach>
标签工作,以便它打印一个名称列表,如下所示:
Best, Milo, Kane
My code is as follows:
我的代码如下:
<c:forEach items="${persons}" var="person">
${person.name},
</c:forEach>
However, on the last person/name, a comma is inserted at the end, e.g.
但是,在最后一个人/姓名上,在末尾插入一个逗号,例如
Best, Milo, Kane,
How can I avoid the last comma in the loop?
如何避免循环中的最后一个逗号?
采纳答案by Reimeus
You could use LoopTagStatus#isLast
你可以用 LoopTagStatus#isLast
<c:forEach items="${persons}" var="person" varStatus="loop">
${person.name}
<c:if test="${!loop.last}">,</c:if>
</c:forEach>
A simpler solution is to use a conditional operatorwithin EL
instead of the if
tag
一个简单的办法是使用条件运算符中EL
,而不是的if
标签
${!loop.last ? ',' : ''}