VijayD
VijayD

Reputation: 820

How to iterate a map in JSP?

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>

Upvotes: 3

Views: 14683

Answers (2)

Boaz
Boaz

Reputation: 4669

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>

Upvotes: 11

Subbu
Subbu

Reputation: 308

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>

Upvotes: 1

Related Questions