views:

650

answers:

3
<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...

(Preferably not using Java)

+1  A: 

define a String variable using the JSP tags

"<%!

String test = new String(); %>"

then refer to that variable in your loop as

" test+= whaterver_value "

Using Java isn't the way to go with this, I think I need to use JSTL
codeninja
almost -1 for new String(). How about ""?
Thilo
Thilo yeah, I understand for new String(). My mistake!
*Scriptlets*... yuck!
BalusC
+2  A: 

Perhaps this will work?

<c:forEach items="${myParams.items}" var="currentItem" varStatus="stat">
  <c:set var="myVar" value="${stat.first ? '' : myVar} ${currentItem}" />
</c:forEach>
harto
this is ok, except the first value is null
codeninja
OK, I added a check to see if it's the first time through the loop. Does that work?
harto
yup perfecto =] ty
codeninja
+2  A: 

You're using JSTL 2.0 right? You don't need to put around all variables. 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

Ben J
this is ok, except the first value is null =/
codeninja
first value null: Just do another c:set outside of the loop to initialize myVar.
Thilo
Then perhaps wrap the set tag in an if? <c:if test="${not empty currentItem}"/> ... </c:if>?
Ben J
EL doesn't evaluate `null` values as a `"null"` string. The problem lies somewhere else. It is apparently incorrectly been set as a `"null"` string.
BalusC