0

I have the following HashMap which I want to iterate over and print the values. How can I do that in Spring?

HashMap<Integer, HashMap<String, String>> hm = new HashMap<Integer, HashMap<String, String>>();
Shrikant Kakani
  • 1,511
  • 2
  • 17
  • 37
  • This is not Spring nor any other web MVC framework (JSF, GWT, Vaadin or other) work, it is plain JSTL + Expression Language. – Luiggi Mendoza Apr 28 '14 at 22:32

2 Answers2

1

You can use JSTL to iterate over HashMap of HashMap.

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

Try like this:

   <c:forEach var="entry" items="${hm}">
          Key: <c:out value="${entry.key}"/>
          Value: <c:out value="${entry.value}"/>
          <c:set var="hm1" value="${Value}">
          <c:forEach var="entry" items="${hm1}"/>
              Key1: <c:out value="${entry1.key}"/>
              Value1: <c:out value="${entry1.value}"/>  
          </c:forEach>
   </c:forEach>
Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82
0

in spring controller class, add the hm object to ModelAndView then return as below.

Class yourControllerClass{

public ModelAndView handleRequest(..){

ModelAndView mav = new ModelAndView("yourViewName");

//your hash map iterations    

mav.put("hm",hm);   
//here first attribute is used to iterate the value in JSP

return mav;

}

in JSP follow Shreyos Answer as in above.

Ezhilan Mahalingam
  • 2,838
  • 3
  • 16
  • 16