[英]Getting Hashmap in jsp
public class DBConnection {
public HashMap<Object, List<Dashboard>> getStoreResult() {
ArrayList<Dashboard> dashRec=new ArrayList<Dashboard>();
try{
Class.forName("");
Connection con=DriverManager.getConnection("");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("");
HashMap<Object, List<Dashboard>> map = new HashMap<>();
while (rs.next()) {
}
return map;
小服务程序代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HashMap<Object, List<Dashboard>> map = new DBConnection().getStoreResult();
request.setAttribute("map",map);
RequestDispatcher rd=request.getRequestDispatcher("Dashboard.jsp");
rd.forward(request, response);
JSP 代码:
%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import ="java.util.*"%>
<%@ page import="com.org.myprj.Dashboard" %>
<% Map<Object, List<Dashboard>> map = (HashMap<>)request.getAttribute("map");%>
I have created Hashmap with key as object and value as Arraylist.Till servlet,it works fine.I want it in jsp page.How to get this Hashmap in jsp page?
使用 JSTL 库并迭代 map,如下所示:
<c:forEach var="vmap" items="${map}">
Key is ${vmap.key} and Value is ${vmap.value}<br>
</c:forEach>
如果不能使用 JSTL 库,可以访问并迭代如下:
<% Map<Object, List<Dashboard>> map = (Map<Object, List<Dashboard>>)request.getAttribute("map");%>
然后像往常在 Java 中一样迭代map
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.