简体   繁体   English

如何在JSTL / JSP中的循环内连接字符串?

[英]How can I concatenate a string within a loop in JSTL/JSP?

<c:forEach items="${myParams.items}" var="currentItem" varStatus="stat">
  <c:set var="myVar" value="<c:out var="myVar" />" />
</c:forEach>

I want to concatenate the values of currentItem.myVar and output it at the end of the loop, problem is I can't figure out how to do this... 我想连接currentItem.myVar的值并在循环结束时输出它,问题是我无法弄清楚如何做到这一点......

(Preferably not using Java) (最好不要使用Java)

Perhaps this will work? 也许这会奏效吗?

<c:forEach items="${myParams.items}" var="currentItem" varStatus="stat">
  <c:set var="myVar" value="${stat.first ? '' : myVar} ${currentItem}" />
</c:forEach>

You're using JSTL 2.0 right? 你正在使用JSTL 2.0吗? You don't need to put <c:out/> around all variables. 您不需要在所有变量周围放置<c:out/> Have you tried something like this? 你尝试过这样的事吗?

<c:forEach items="${myParams.items}" var="currentItem" varStatus="stat">
  <c:set var="myVar" value="${myVar}${currentItem}" />
</c:forEach>

Edit : Beaten by the above 编辑 :被上述击败

是JSTL的join() ,你搜索的是什么?

<c:set var="myVar" value="${fn:join(myParams.items, ' ')}" />

define a String variable using the JSP tags 使用JSP标记定义String变量

<%!
String test = new String();
%>

then refer to that variable in your loop as 然后在循环中引用该变量

<c:forEach items="${myParams.items}" var="currentItem" varStatus="stat">
test+= whaterver_value
</c:forEach>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM