Pergunta

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>>();
Foi útil?

Solução

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>

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top