![](/img/trans.png)
[英]How to loop the key and value(which is in UserDefinedClass) of Hashmap in ModelAndView to JSP using JSTL
[英]How to display HashMap Key in jsp/jstl
我是JSP / JSTL的新手。
我已根據請求設置了HashMap,如下所示
HashMap <String, Vector> hmUsers = new HashMap<String, Vector>();
HashMap hmUsers = eQSessionListener.getLoggedinUsers();
request.setAttribute("currentLoggedInUsersMap", hmUsers);
我在My jsp中警告HashMap如下
<script> alert("<c:out value = '${currentLoggedInUsersMap}' />"); </script>
到目前為止,所有這些都符合我的期望。
但是,如果我嘗試獲取此HashMap的密鑰,則不會發出任何警報。
<script> alert("<c:out value = '${currentLoggedInUsersMap.key}' />"); </script>
有什么我錯的嗎?
提前致謝。
這是您在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>
它就像在JAVA中使用的Map.Entry,如下所示,以獲取鍵值。
for (Map.Entry<String, String> entry : currentLoggedInUsersMap.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
}
閱讀有關如何在JSP中循環HashMap的詳細描述?
您可以將hashmap迭代為:
<c:forEach items="${currentLoggedInUsersMap}" var="usersMap" varStatus="usersMapStatus">
//Key
Key: ${usersMap.key}
//Iterate over values ,assuming vector of strings
<c:forEach items="${usersMap.value}" var="currentLoggedInUserValue" varStatus="valueStatus">
Value: ${currentLoggedInUserValue}
</c:forEach>
</c:forEach>
這里$ {usersMap.key}是地圖中存在的鍵列表,$ {usersMap.value}是你必須在內循環中再次迭代的向量,如上所示。
確保導入:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.