在JSP和javascript中连接包含foreach LOOP中变量和字符串值的字符串

时间:2015-03-01 10:56:29

标签: javascript jsp jstl

我想实现这个目标:

  ['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

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

1 个答案:

答案 0 :(得分:0)

使用<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>