0
我想弄清楚如何遍歷ConcurrentHashMap,它將字符串保存爲鍵和具有getter方法的對象。訪問包裝在JSP中的對象的getter方法使用JSP映射
以下是對象作爲值存儲在地圖中的類。
public class ServiceStats {
public int unprocessedItems;
public int processedItems;
/**
* @return the unprocessedItems
*/
public int getUnprocessedItems() {
return unprocessedItems;
}
/**
* @param unprocessedItems the unprocessedItems to set
*/
public void setUnprocessedItems(int unprocessedItems) {
this.unprocessedItems = unprocessedItems;
}
/**
* @return the processedItems
*/
public int getProcessedItems() {
return processedItems;
}
/**
* @param processedItems the processedItems to set
*/
public void setProcessedItems(int processedItems) {
this.processedItems = processedItems;
}
}
Servlet是這樣將數據發送到JSP。
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
ConcurrentHashMap<String, ServiceStats> serviceStatsMap = stats.getServiceStats();
request.setAttribute("stats", serviceStatsMap);
request.getRequestDispatcher("WEB-INF/jsp/stats.jsp").forward(request, response);
}
迭代應該是這樣的:
<c:forEach var="entry" items="${stats}">
${entry.key}, ${entry.value.getvaluefromgettermethod}
</c:forEach>
如何訪問對象,它是地圖的價值,然後訪問該對象內的getter方法?
我是否必須像這樣在JSP中導入我的類,或者這是不必要的?
<%@ page import="com.company.ServiceStats"%>
您認爲這裏發生了什麼'$ {entry.key}'? –
@AleksandrM如果String值來自用戶輸入字段,會發生什麼是潛在的[跨站點腳本(XSS)](https://en.wikipedia.org/wiki/Cross-site_scripting)漏洞。 – Andreas
@ Amit.rk3謝謝!它正在工作。沒想到這很容易:) – lkallas