Java 如何在 JSP 中循环遍历 HashMap?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1835683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to loop through a HashMap in JSP?
提问by blub
How can I loop through a HashMap
in JSP?
如何循环遍历HashMap
JSP 中的 a?
<%
HashMap<String, String> countries = MainUtils.getCountries(l);
%>
<select name="country">
<%
// Here I need to loop through countries.
%>
</select>
回答by Jim Garrison
Depending on what you want to accomplish within the loop, iterate over one of these instead:
根据您想要在循环中完成的内容,请迭代其中之一:
countries.keySet()
countries.entrySet()
countries.values()
countries.keySet()
countries.entrySet()
countries.values()
回答by BalusC
Just the same way as you would do in normal Java code.
就像您在普通 Java 代码中所做的那样。
for (Map.Entry<String, String> entry : countries.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
// ...
}
However, scriptlets(raw Java code in JSP files, those <% %>
things) are considered a poor practice. I recommend to install JSTL(just drop the JAR file in /WEB-INF/lib
and declare the needed taglibsin top of JSP). It has a <c:forEach>
tag which can iterate over among others Map
s. Every iteration will give you a Map.Entry
back which in turn has getKey()
and getValue()
methods.
但是,scriptlet(JSP 文件中的原始 Java 代码,那些<% %>
东西)被认为是一种糟糕的做法。我建议安装JSTL(只需将 JAR 文件放入/WEB-INF/lib
并在 JSP 顶部声明所需的标签库)。它有一个<c:forEach>
可以迭代的标签Map
。每次迭代都会给你一个Map.Entry
返回,它反过来有getKey()
和getValue()
方法。
Here's a basic example:
这是一个基本示例:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:forEach items="${map}" var="entry">
Key = ${entry.key}, value = ${entry.value}<br>
</c:forEach>
Thus your particular issue can be solved as follows:
因此,您的特定问题可以按如下方式解决:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<select name="country">
<c:forEach items="${countries}" var="country">
<option value="${country.key}">${country.value}</option>
</c:forEach>
</select>
You need a Servlet
or a ServletContextListener
to place the ${countries}
in the desired scope. If this list is supposed to be request-based, then use the Servlet
's doGet()
:
您需要 aServlet
或 aServletContextListener
将 放置${countries}
在所需的范围内。如果这个列表应该是基于请求的,那么使用Servlet
's doGet()
:
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
Map<String, String> countries = MainUtils.getCountries();
request.setAttribute("countries", countries);
request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);
}
Or if this list is supposed to be an application-wide constant, then use ServletContextListener
's contextInitialized()
so that it will be loaded only once and kept in memory:
或者,如果这个列表应该是一个应用程序范围的常量,那么使用ServletContextListener
'scontextInitialized()
这样它只会被加载一次并保存在内存中:
public void contextInitialized(ServletContextEvent event) {
Map<String, String> countries = MainUtils.getCountries();
event.getServletContext().setAttribute("countries", countries);
}
In both cases the countries
will be available in ELby ${countries}
.
在这两种情况下,countries
将在EL通过${countries}
。
Hope this helps.
希望这可以帮助。
See also:
也可以看看:
回答by tk_
Below code works for me
下面的代码对我有用
first I defined the partnerTypesMap
like below in the server side,
首先我partnerTypesMap
在服务器端定义如下,
Map<String, String> partnerTypes = new HashMap<>();
after adding values to it I added the object to model
,
添加值后,我将对象添加到model
,
model.addAttribute("partnerTypesMap", partnerTypes);
When rendering the page I use below foreach
to print them one by one.
在渲染页面时,我使用下面的方式foreach
将它们一一打印。
<c:forEach items="${partnerTypesMap}" var="partnerTypesMap">
<form:option value="${partnerTypesMap['value']}">${partnerTypesMap['key']}</form:option>
</c:forEach>