Contenante 字符串,其中包含 c:forEach 循环中的变量值和字符串 JSP

Contenante string which contains value of variable and strings in c:forEach loop in JSP

我想实现这个:

  ['string1', -33.890542, 151.274856],
  ['string 2', -33.923036, 151.259052],
  ['string 3', -34.028249, 151.157507],
  ['string 4', -33.80010128657071, 151.28747820854187],
  ['string 5', -33.950198, 151.259302]

为了实现上述输出,我有以下代码

<c:forEach items="${coordListEvent}" var="cor" varStatus="stat">
     <c:set var="chaine" value="     ['',${chaine}${cor.longitude},${cor.latitude}]," />
      </c:forEach>

但是,我得到了结果

['',['',['',['',['',['',['',['',['',['',10.1803501,36.8162982],10.1803501,36.8162982],10.1803501,36.8162982],10.1803501,36.8162982],10.1803501,36.8162982],10.1803501,36.8162982],10.1803501,36.8162982],10.1803501,36.8162982],10.1803501,36.8

如何修改我的代码以获得预期的输出?

使用 <c:choose> JSTL 标记根据循环计数器分支到 if-else 结构。

<c:forEach items="${coordListEvent}" var="cor" varStatus="stat">
    <c:choose>
        <!-- IF -->
        <c:when test="${stat.count == 1}">
            <c:set var="chaine" value="[${cor.longitude},${cor.latitude}]" />
        </c:when>
        <!-- ELSE -->
        <c:otherwise>
            <c:set var="chaine" value="${chaine},[${cor.longitude},${cor.latitude}]" />
        </c:otherwise>
    </c:choose>
</c:forEach>