How can I concatenate a string within a loop in JSTL/JSP?
Solution 1
Perhaps this will work?
<c:forEach items="${myParams.items}" var="currentItem" varStatus="stat">
<c:set var="myVar" value="${stat.first ? '' : myVar} ${currentItem}" />
</c:forEach>
Solution 2
You're using JSTL 2.0 right? You don't need to put <c:out/>
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
Solution 3
Is JSTL's join()
, what you searched for?
<c:set var="myVar" value="${fn:join(myParams.items, ' ')}" />
qodeninja
I write qode mostly for myself... out of curiosity for solving problems, understanding how things work or making (sometimes unnecessarily) complex systems to only simplify them later (once I discover alternative strategies). For whatever reason, I like torturing myself with Regular Expressions, SED, Bash and JavaScript (Node), but have found a growing (painful) love with Python. Having said that, I enjoy scripting languages a lot more than compiled languages, and I've coded in almost all of the major modern ones except Ruby. I'm a secret Turing Machine/Computer Grammars/Regular Expressions nerd, and have written my own mini compilers and toy languages. I'm constantly writing command dispatchers that I later write scripting languages for; it's an addiction. There's plenty room for me to grow and learn still; and I appreciate the wisdom of grey beards and lady wizards even if I don't always follow their sage advice. FOSS is hella cool; cool projects are cool. Find me online if you have ideas. I'm a really bad programmer but I'll write a line or two for the betterization of the peoples. Edit: I recently discoved that VI is really just SED with wings. Still not using VI. Nano or bust.
Updated on January 28, 2020Comments
-
qodeninja over 4 years
<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)
-
qodeninja about 14 yearsUsing Java isn't the way to go with this, I think I need to use JSTL
-
hansvb about 14 yearsalmost -1 for new String(). How about ""?
-
qodeninja about 14 yearsthis is ok, except the first value is null =/
-
qodeninja about 14 yearsthis is ok, except the first value is null
-
hansvb about 14 yearsfirst value null: Just do another c:set outside of the loop to initialize myVar.
-
Ben J about 14 yearsThen perhaps wrap the set tag in an if? <c:if test="${not empty currentItem}"/> ... </c:if>?
-
harto about 14 yearsOK, I added a check to see if it's the first time through the loop. Does that work?
-
Kapil D about 14 yearsThilo yeah, I understand for new String(). My mistake!
-
BalusC about 14 yearsEL doesn't evaluate
null
values as a"null"
string. The problem lies somewhere else. It is apparently incorrectly been set as a"null"
string. -
Jess about 11 yearsYou can use this same concept to concat a string with a delimiter like this:
value="${ids}${stat.first ? '' : ','}${currentItem.id}"
-
ArunDhwaj IIITH about 8 yearsThanks Ben and @Subin, Its saved a lot of mine time.