print Map Object results with JSTL/JSP

Asked

Viewed 69 times

0

Good evening everyone, I would like to know how to print the values of the Map Return Object with jstl? I tried to do with foreach, but the values does not return anything. Follow my implementation:

index.jsp

<c:forEach items="${sedes}" var="sedesMap">
    <p>${sedesMap.value.sedeId}</p>
</c:forEach>

Values returned from Map:

[
    {
        23=Sede [
            sedeId=9, 
            sedeBairro=Tupuiu,
            ...
        ]
    }, 
    {
        53=Sede [
           sedeId=26,
           sedeBairro=São Bernardo do Campos,
           ...
       ]
    },
    {
       53=Sede [
           sedeId=30,
           sedeBairro=São José do Campos,
           ...
       ]
    }
]

1 answer

0

Are you sure your map has the values being passed to jsp? Not seeing your Servlet code makes it harder.

Theoretically it would be like this in Servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    HashMap<String, String> myHash = new HashMap<>();
    myHash.put("a","b");
    myHash.put("c","d");

    request.setAttribute("themap", myHash);

    request.getRequestDispatcher("test.jsp").forward(request, response);
}

and with jstl in jsp:

<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>THE MAP</h1>
<c:forEach var="type" items="${themap}">
   K = ${type.key}
   V = ${type.value}
   <br />
</c:forEach>
</body>
</html>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.