siva
siva

Reputation: 244

How to display a Map object on a JSP from a Spring controller

I have to retrieve a Map from the controller and display the details as a table on a JSP. The map returned from the controller is a Map <String, List>. How do I do this?

Upvotes: 4

Views: 6492

Answers (1)

Hari Ram
Hari Ram

Reputation: 305

This is as simple as you do in java. I recommend you install JSTL jar file and use taglibs in your file. Then use the <c: forEach> tag to iterate through the map and print them.

In you case,

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<table>
   <c:forEach items="${map}" var="mapElement">
      <tr>
           <td>${mapElement.key}</td>
           <c:forEach items="${mapElement.value}" var="listElement" >
              <td>${listElement}</td>
           </c:forEach>
      </tr>
   </c:forEach>
<table>

Upvotes: 6

Related Questions