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

后端 未结 4 2147
生来不讨喜
生来不讨喜 2020-12-15 03:36

  \" />


        
相关标签:
4条回答
  • 2020-12-15 04:11

    Is JSTL's join(), what you searched for?

    <c:set var="myVar" value="${fn:join(myParams.items, ' ')}" />
    
    0 讨论(0)
  • 2020-12-15 04:22

    Perhaps this will work?

    <c:forEach items="${myParams.items}" var="currentItem" varStatus="stat">
      <c:set var="myVar" value="${stat.first ? '' : myVar} ${currentItem}" />
    </c:forEach>
    
    0 讨论(0)
  • 2020-12-15 04:22

    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

    0 讨论(0)
  • 2020-12-15 04:22

    define a String variable using the JSP tags

    <%!
    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>
    
    0 讨论(0)
提交回复
热议问题