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

104,307

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, ' ')}" />
Share:
104,307
qodeninja
Author by

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, 2020

Comments

  • qodeninja
    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
    qodeninja about 14 years
    Using Java isn't the way to go with this, I think I need to use JSTL
  • hansvb
    hansvb about 14 years
    almost -1 for new String(). How about ""?
  • qodeninja
    qodeninja about 14 years
    this is ok, except the first value is null =/
  • qodeninja
    qodeninja about 14 years
    this is ok, except the first value is null
  • hansvb
    hansvb about 14 years
    first value null: Just do another c:set outside of the loop to initialize myVar.
  • Ben J
    Ben J about 14 years
    Then perhaps wrap the set tag in an if? <c:if test="${not empty currentItem}"/> ... </c:if>?
  • harto
    harto about 14 years
    OK, I added a check to see if it's the first time through the loop. Does that work?
  • Kapil D
    Kapil D about 14 years
    Thilo yeah, I understand for new String(). My mistake!
  • BalusC
    BalusC about 14 years
    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.
  • Jess
    Jess about 11 years
    You can use this same concept to concat a string with a delimiter like this: value="${ids}${stat.first ? '' : ','}${currentItem.id}"
  • ArunDhwaj IIITH
    ArunDhwaj IIITH about 8 years
    Thanks Ben and @Subin, Its saved a lot of mine time.