老师请问map集合怎么通过这种方法取出key和value值?

来源:2-10 JSTL遍历集合

qq_旧安_0

2020-04-30 16:22:35

老师请问map集合怎么通过这种方法取出key和value值?

写回答

1回答

好帮手慕小尤

2020-04-30

同学你好,同学实现思路是一致的,同学在Servlet中为集合添加数据,然后在jsp中循环显示。代码如下所示:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HashMap<String, String> map = new HashMap<>();
		map.put("1", "test1");
		map.put("2", "test2");
		map.put("3", "test3");
		request.setAttribute("map", map);
		request.getRequestDispatcher("/test.jsp").forward(request, response);
	}
<c:forEach items="${requestScope.map }" var="m" varStatus="idx">
	<h2>${idx.index+1 }---key:${m.key }---value:${m.value }</h2>
</c:forEach>

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

0
hq_旧安_0
h 理解了,谢谢老师!
h020-04-30
共1条回复

0 学习 · 9666 问题

查看课程