在JSP中使用数组

在JSP中使用数组,jsp,jstl,Jsp,Jstl,我想把两个对象合并成一个变量。这些物体看起来是这样的 {name: "James", type: "author"}, {name: "Amelia", type: "author"} 可以通过我的JSP中的${global.content.credits.by}访问它们。我要做的是列出每个对象的两个名称,使它们看起来像:James,Amy(列表中最后一项没有尾随逗号)当调用${authorNames}变量时 我尝试了以下方法: <c:forEach items="${global.co

我想把两个对象合并成一个变量。这些物体看起来是这样的

{name: "James", type: "author"}, {name: "Amelia", type: "author"}
可以通过我的JSP中的
${global.content.credits.by}
访问它们。我要做的是列出每个对象的两个名称,使它们看起来像:
James,Amy
(列表中最后一项没有尾随逗号)当调用
${authorNames}
变量时

我尝试了以下方法:

<c:forEach items="${global.content.credits.by}" var="author" varStatus="loop">
   <c:set var="authorNames" value="${author.name}" />
</c:forEach>

但是,我能够显示所有作者名称的唯一方法是在循环内,在循环外,${authorNames}变量在每次迭代中都会被覆盖


JSP中是否有某种数组推送方法,我可以用来组合两个名称,然后在循环中的最后一个名称之外的每个名称之间添加一个逗号。

您可以将名称附加到
authorNames
,而不是覆盖它

<c:forEach items="${global.content.credits.by}" var="author" varStatus="loop">
   <c:if test="${loop.index == 0}>
     <c:set var="authorNames" value="${author.name}" />
   </c:if>
   <c:if test="${loop.index != 0}>
     <c:set var="authorNames" value="${authorNames},${author.name}" />
   </c:if>
</c:forEach>