3

How to iterate map with <Integer, List<ResponseInfo>> in JSP using <c:forEach> JSTL tag and then iterating that list using another for loop?

Let me know if you want to see the code.

From the controller I'm returning
return new ModelAndView("reviewAudit","responseForm",responseForm);

where responseForm contains a map:
private Map<String, List<ResponseInfo>> resInfoMap;


Here is my JSP code:

<div class="panel-body">
    <div class="panel-group" id="accordion">
    <c:forEach items="${responseForm.resInfoMap}" var="responselist">

        <div class="panel panel-primary">
            <div class="panel-heading">
                <h4 class="panel-title">
                    <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" class="">Topic1</a>
                </h4>
            </div>
            <div id="collapseOne" class="panel-collapse collapse in" aria-expanded="true">
                <div class="panel-body">
                    <table>
                        <c:forEach items="${responselist}" var="response1">
                        <tr>
                            <td>
                                <p>
                                    <span style="font-size: 13px; font-weight: bold;">Q:</span>
                                    ${response1.auditQuestion}
                                </p>
                            </td>
                            <td>
                                <p>
                                    <span style="font-size: 13px; font-weight: bold;">Ans:</span>
                                    ${response1.auditResponse}
                                </p>
                                <p>
                                    <span style="font-size: 13px; font-weight: bold;">Comment:</span>
                                    ${response1.auditComment}
                                </p>
                            </td>
                        </tr>
                        </c:forEach><!-- list iteration -->
                    </table>
                </div>
                </div>
        </div>

    </c:forEach> <!-- map iteration -->         
    </div>       <!-- <div class="panel-group" id="accordion"> -->
</div>
informatik01
  • 16,038
  • 10
  • 74
  • 104
VijayD
  • 826
  • 1
  • 11
  • 33
  • 7
    We'd **love** to see the code. – Konstantin Yovkov Mar 27 '15 at 12:05
  • @kicko added code in the question itself. P.S. Iteration is giving exception as `javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>` – VijayD Mar 27 '15 at 12:10
  • Where do you get responselist from? what is it's type? – Boaz Mar 27 '15 at 12:14
  • 1
    Related questions: 1) [How to loop through a HashMap in JSP?](https://stackoverflow.com/questions/1835683/how-to-loop-through-a-hashmap-in-jsp), 2) [Use with HashMap](https://stackoverflow.com/questions/2210907/use-cforeach-with-hashmap) – informatik01 May 22 '18 at 15:20

2 Answers2

11

When iterating over a map using <c:forEach> you're, in fact, iterating over a collection of entries, these entries have both "key" and "value" fields.

Try the following:

<c:forEach var="entry" items="${map}">
   key is ${entry.key}
   <c:forEach var="info" items="${entry.value}">
        info is ${info}
   </c:forEach>
</c:forEach>
informatik01
  • 16,038
  • 10
  • 74
  • 104
Boaz
  • 4,549
  • 2
  • 27
  • 40
  • do I have to write like "key is ${entry.key}" or var key=${entry.key}? – VijayD Mar 27 '15 at 12:14
  • 3
    "key is ${entry.key}" is to show how to access the key, you don't need to do that if you don't want to do that. the inner c:forEach define the entry.value (which is the List) as it's "items" so it iterate over it. – Boaz Mar 27 '15 at 12:17
  • how can I change this line with every loop `Topic1` to change href and "Topic 1 values"? – VijayD Mar 27 '15 at 12:28
  • I'm not sure what you're trying to do. I assume you want to change this element at every iteration of the inner for loop (i.e., when iterating over List), the first thing you should do it make sure that this element is within the scope of the inner loop (otherwise you won't have access to the "info" variable at each iteration). then use your understanding of the ResponseInfo class to pull the relevant information (I assume the href and topic) from the "info" variable. – Boaz Mar 27 '15 at 12:33
  • `Topic <%System.out.print(count++); %>` wrote this silly stuff where var is StringBuffer and count is int type. but instead of attaching that count to "topic" and var to href value, its coming on server log, is there any way to attach the java variables to Strings in JSP? – VijayD Mar 27 '15 at 12:47
  • system.out.print prints the value to the console. you want to print it to the page. <%System.out.print(var);%> should be <%=var%> – Boaz Mar 27 '15 at 12:48
1

We are not able to see you first code. Try this:

<c:forEach var="map" items="${responseForm}"> <!--your map variable name-->
      <c:forEach var="list" items="${map.value}">

      </c:forEach>
</c:forEach>
Subbu
  • 308
  • 2
  • 4
  • 12