如何在JSP中循环访问HashMap? [英] How to loop through a HashMap in JSP?
问题描述
如何在JSP中循环 HashMap
?
<%
HashMap< String,String> countries = MainUtils.getCountries(l);
%>
<%
//这里我需要循环遍历国家。
%>
< / select>
就像您在普通Java中一样代码。
for(Map.Entry< String,String> entry:countries.entrySet()){
String key = entry.getKey();
String value = entry.getValue();
// ...
}
然而, scriptlets (JSP文件中的原始Java代码,那些 下面是一个基本的例子: 因此您的特定问题可以按照以下方式解决: 您需要一个 或者如果这个列表应该是一个应用程序范围内的常量,那么使用 在这两种情况下,<%%>
东西)被视为不良练习。我建议安装 JSTL (只需删除JAR文件在 / WEB-INF / lib
中声明所需 taglibs )。它有一个 < c:forEach>
标签,可以迭代其他标签 Map
s。每次迭代都会为您提供 Map.Entry
返回,后者又具有 getKey()
和 getValue()$ c
。
<%@ taglib prefix =curi =http://java.sun.com/jsp/jstl/core%>
< c:forEach items =$ {map}var =entry>
Key = $ {entry.key},value = $ {entry.value}< br>
< / c:forEach>
<%@ taglib prefix =curi =http://java.sun.com/jsp/jstl/core >
< c:forEach items =$ {countries}var =country>
< / c:forEach>
< / select>
Servlet
或一个 ServletContextListener
将 $ {countries}
放置在所需的范围内。如果这个列表应该是基于请求的,那么使用 Servlet
的 doGet()
:
protected void doGet(HttpServletRequest请求,HttpServletResponse响应){
Map< String,String> countries = MainUtils.getCountries();
request.setAttribute(国家,国家);
request.getRequestDispatcher(/ WEB-INF / page.jsp)。forward(request,response);
$ / code> ServletContextListener
的 contextInitialized()
,这样它将只加载一次并保存在内存中:
public void contextInitialized(ServletContextEvent event){
Map< String,String> countries = MainUtils.getCountries();
event.getServletContext()。setAttribute(countries,countries);
$ / code> countries
将通过<$ c在 EL 中提供$ b> $ {countries}
希望这有助于您。 还有:
How can I loop through a HashMap
in JSP?
<%
HashMap<String, String> countries = MainUtils.getCountries(l);
%>
<select name="country">
<%
// Here I need to loop through countries.
%>
</select>
Just the same way as you would do in normal Java code.
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 taglibs in 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.
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()
:
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:
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 EL by ${countries}
.
Hope this helps.
See also:
- Iterate over elements of List and Map using JSTL <c:forEach> tag
- How to iterate over a nested map in <c:forEach>
- How to iterate an ArrayList inside a HashMap using JSTL?
- Using special auto start servlet to initialize on startup and share application data
这篇关于如何在JSP中循环访问HashMap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!