JSp

jstl foreach省略最后一条记录中的元素

发布于 2021-02-01 16:23:11

尝试使用此jstl公式化json字符串,我如何使该段不在最后一个记录的末尾添加逗号?最后注意逗号

<c:forEach items="${fileList}" var="current">
    { id:1001,data:["<c:out value="${current.fileName}" />" , "<c:out value="${current.path}" />" , "<c:out value="${current.size}" />" , "<c:out value="${current.type}" />"] },
</c:forEach>
关注者
0
被浏览
146
1 个回答
  • 面试哥
    面试哥 2021-02-01
    为面试而生,有面试问题,就找面试哥。

    只需使用LoopTagStatus#isLast()

    <c:forEach items="${fileList}" var="current" varStatus="loop">
        { id: 1001,
          data: [
            "<c:out value="${current.fileName}" />",
            "<c:out value="${current.path}" />",
            "<c:out value="${current.size}" />",
            "<c:out value="${current.type}" />"
          ]
        }<c:if test="${!loop.last}">,</c:if>
    </c:forEach>
    

    您还可以在EL中使用条件运算符,而不是<c:if>

        ${!loop.last ? ',' : ''}
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看