如何在jsp/jstl中显示HashMap Key

Sac*_*hin 3 javascript java jsp jstl

我是JSP/JSTL的新手.

我已根据请求设置了HashMap,如下所示

HashMap <String, Vector> hmUsers = new HashMap<String, Vector>();

HashMap hmUsers = eQSessionListener.getLoggedinUsers();

request.setAttribute("currentLoggedInUsersMap", hmUsers);
Run Code Online (Sandbox Code Playgroud)

我在My jsp中警告HashMap如下

<script> alert("<c:out value = '${currentLoggedInUsersMap}' />"); </script>
Run Code Online (Sandbox Code Playgroud)

到目前为止,所有这些都符合我的期望.

但是,如果我尝试获取此HashMap的密钥,则不会发出任何警报.

<script> alert("<c:out value = '${currentLoggedInUsersMap.key}' />"); </script>
Run Code Online (Sandbox Code Playgroud)

有什么我错的吗?

提前致谢.

Bra*_*raj 6

这是您在JSP中迭代Map所需的内容.有关更多信息,请查看JSTL Core c:forEach Tag.

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

<c:forEach items="${currentLoggedInUsersMap}" var="entry">
    Key = ${entry.key}, value = ${entry.value}<br>
</c:forEach>
Run Code Online (Sandbox Code Playgroud)

它就像在JAVA中使用的Map.Entry,如下所示,以获取键值.

for (Map.Entry<String, String> entry : currentLoggedInUsersMap.entrySet()) {
    String key = entry.getKey();
    String value = entry.getValue();
}
Run Code Online (Sandbox Code Playgroud)

阅读有关如何在JSP中循环HashMap的详细描述