从JSTL中的Viewable循环遍历地图

时间:2017-12-15 20:23:43

标签: java jsp jersey mapping jstl

所以我试图遍历我从Viewable检索的地图。它使用单个属性或其他任何东西,但不知何故它不允许我遍历列表。有什么建议吗?

如果你有更好的想法传递对象而不是使用Viewable,请随时告诉我!

提前谢谢你们。

控制器:

    @GET
@Path("home")
public Viewable showHome() {
    Map <String, Location> map = init();
    return new Viewable("/WEB-INF/home.jsp", map);
}

private HashMap init() {
    Map <String, Location> map = new HashMap <String, Location>();
    Location location1 = new Location("A");
    Location location2 = new Location("B");
    map.put("1", location1);
    map.put("2", location2);
    return (HashMap) map;
}

home.jsp代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> 
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <c:if test="${it != null}">
        <c:forEach var="location" items="${it}">
             Location: <c:out value="${location.name}" />
        </c:forEach>
    </c:if>
</body>
</html>

0 个答案:

没有答案