3

I want to do in jsp side something like this:

<c:forEach items="${configForm.unselectedServers}" var="item">
    String name = "";
    <c:forEach items="$item.configs" var="conf">
        name += ", " + {$conf.name}
        ....

Is it possible to do something like that?

item.configs it's an array of a class (that contains name). That part I know it works, I simply want to know how to put these value in a string.


EDIT:

I'm getting the following error:

Caused by: java.lang.NumberFormatException: For input string: ""
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
        at java.lang.Long.parseLong(Long.java:431)
        at java.lang.Long.<init>(Long.java:678)
        at org.apache.el.lang.ELArithmetic$LongDelegate.coerce(ELArithmetic.java:186)
        at org.apache.el.lang.ELArithmetic.coerce(ELArithmetic.java:357)
        at org.apache.el.lang.ELArithmetic.add(ELArithmetic.java:235)
        at org.apache.el.parser.AstPlus.getValue(AstPlus.java:40)
        at org.apache.el.parser.AstPlus.getValue(AstPlus.java:38)
        at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
        at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:935)
        at org.apache.jsp.config.configDetail_jsp._jspx_meth_c_005fset_005f5(configDetail_jsp.java:509)
        at org.apache.jsp.config.configDetail_jsp._jspx_meth_c_005fforEach_005f1(configDetail_jsp.java:475)
        at org.apache.jsp.config.configDetail_jsp._jspx_meth_c_005fforEach_005f0(configDetail_jsp.java:407)
        at org.apache.jsp.config.configDetail_jsp.access$4(configDetail_jsp.java:384)
        at org.apache.jsp.config.configDetail_jsp$Helper.invoke1(configDetail_jsp.java:615)
        at org.apache.jsp.config.configDetail_jsp$Helper.invoke(configDetail_jsp.java:679)

3 Answers 3

4

How about:

<c:forEach items="${configForm.unselectedServers}" var="item">
  <c:set var="name" value="" />
  <c:forEach items="${item.configs}" var="conf">
    <c:set var="name" value="${name}, ${conf.name}" />
  </c:forEach>
</c:forEach>
Sign up to request clarification or add additional context in comments.

2 Comments

Please see the error that this code gives in my edited question. Do you know why?
Updated the code to use a fn:join instead. I am not sure if you can simply concat Strings with '+'. Also, you may need to remove the empty set on the second line. It may be doing something funny.
2

Better approach is to handle it with JSTL itself as shown below:

<c:forEach items="${configForm.unselectedServers}" var="item">
  <c:set var="name" value="" />
  <c:forEach items="$item.configs" var="conf">
    <c:set var="name" value='${name + "," + conf.name}'/>
    <%--Do something here--%>
  </c:forEach>
</c:forEach>

But if the question is just for your awareness you, can do something like this with scriptlet:

    <c:forEach items="${configForm.unselectedServers}" var="item">
      <c:set var="name" value="" />
      <c:forEach items="$item.configs" var="conf">
        <c:set var="name" value='${name + "," + conf.name}'/>
        <%
          String name = pageContext.getAttribute("name");
          //do something here
        %>
      </c:forEach>
    </c:forEach>

2 Comments

Please see the error that this code gives in my edited question. Do you know why?
` <c:forEach items="$item.configs" var="conf">` missing {} in item.configs
2

Maybe this helps.

<c:forEach items="${configForm.unselectedServers}" var="item">
   <c:set var="name" value=""/>
   <c:forEach items="${item.configs}" var="conf">
      // here u can use yr variable like this
      <c:set var="name" value="${conf.first_name}"/>
   </c:forEach>
</c:forEach>

2 Comments

Please see the error that this code gives in my edited question. Do you know why?
` <c:forEach items="$item.configs" var="conf">` missing {} in item.configs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.