如何使用 JSTL 在 jsp 页面中迭代 ArrayList<HashMap<String, String>>
Posted
技术标签:
【中文标题】如何使用 JSTL 在 jsp 页面中迭代 ArrayList<HashMap<String, String>>【英文标题】:How to iterate ArrayList<HashMap<String, String>> in a jsp page using JSTL 【发布时间】:2014-10-19 12:29:17 【问题描述】:我正在尝试使用 JSTL 显示我的数据库中的值有标签,因为它不可重用我一直在使用另一个场景按照我之前的问题,Other Statergies to implemment <sql:query>,我开始使用 servlet 去我的查看表格的页面,我不能像在那个链接中那样使用一维数组,所以我使用了 ArrayList>。
下面给出了我从数据库中获取值的代码。
public ArrayList getAllStockDetails()
System.out.println("Function");
HashMap<String, String> stkMap = new HashMap<String, String>();
ArrayList<HashMap<String, String>> stkList= new ArrayList<>();
try
Dbconnection dcon = new Dbconnection();
dcon.connect();
String query = "Select * from tbstk";
Statement stmt = dcon.con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next())
//System.out.println(rs.getString("SCat_id") + rs.getString("SCat_des"));
stkMap.put("idStk", rs.getString("Stk_prdid"));
stkMap.put("prdStk", rs.getString("Stk_prdid"));
stkMap.put("lvlStk", rs.getString("Stk_lvlid"));
stkMap.put("inStk", rs.getString("Stk_instk"));
stkMap.put("thldStk", rs.getString("Stk_thld"));
stkMap.put("expStk", rs.getString("Stk_prdid"));
stkList.add(stkMap);
return stkList;
catch (SQLException ex)
Logger.getLogger(Op_Stock.class.getName()).log(Level.SEVERE, null, ex);
return null;
我的 Servlet 代码如下
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
String userPath = request.getServletPath();
switch (userPath)
case "/ViewStock":
Op_Stock ops= new Op_Stock();
ArrayList<HashMap<String, String>> stkList= new ArrayList<>();
stkList=ops.getAllStockDetails();
request.setAttribute("stkList", stkList);
userPath = "/Stock";
break;
//Other cases
String url = "/WEB-INF/view" + userPath + ".jsp";
**request.getRequestDispatcher(url).forward(request, response);**//<--Error Here
下面是我页面中的代码
<table class="table table-bordered table-striped table-hover">
<thead>
<th><strong>Product Name</strong></th>
<th><strong>Level</strong></th>
<th><strong>In Stock</strong></th>
<th><strong>Threshold</strong></th>
<th><strong>Expiry</strong></th>
<th><strong>Operations</strong></th>
</thead>
<jsp:useBean id="gen" scope="request" class="Common.General"/>
<jsp:useBean id="prd" scope="request" class="Product.Product"/>
<tbody>
<c:forEach var="row" items="$stkList.rows">
<tr>
<td><c:out value="$row.prdStk"/></td>
<td><c:out value="$row.lvlStk"/></td>
<td><c:out value="$row.inStk"/></td>
<td><c:out value="$row.thldStk"/></td>
<td><c:out value="$row.expStk"/></td>
<td>
<button type="button" class="btn btn-default btn-sm btn-danger" onclick="deletedata('$row.idStk');">
<span class="glyphicon glyphicon-trash"></span>
</button>
<button type="button" class="btn btn-default btn-sm btn-primary" onclick="editdata('$row.idStk')">
<span class="glyphicon glyphicon-edit"></span>
</button>
<button class="btn btn-info btn-sm" data-toggle="modal" data-target="#myModal">
<span class="glyphicon glyphicon-eye-open"></span>
</button>
</td>
</tr>
</c:forEach>
</tbody>
<tfoot>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tfoot>
</table>
我的页面加载只返回空白页面,我做错了什么,如果我做的代码有误,请纠正我。
我的日志中的错误如下
Stacktrace:] with root cause
java.lang.NumberFormatException: For input string: "rows"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at javax.el.ListELResolver.coerce(ListELResolver.java:157)
at javax.el.ListELResolver.getValue(ListELResolver.java:70)
at org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:110)
at org.apache.el.parser.AstValue.getValue(AstValue.java:167)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:187)
at org.apache.jasper.el.JspValueExpression.getValue(JspValueExpression.java:115)
at org.apache.jsp.WEB_002dINF.view.Stock_jsp._jspx_meth_c_005fforEach_005f0(Stock_jsp.java:344)
at org.apache.jsp.WEB_002dINF.view.Stock_jsp._jspService(Stock_jsp.java:191)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:405)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:349)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:721)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:466)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:391)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:318)
at Stock.Con_Stock.processRequest(Con_Stock.java:58)
at Stock.Con_Stock.doGet(Con_Stock.java:74)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:655)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:277)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2381)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2370)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
【问题讨论】:
你可以试试$row['prdStk']
等吗?
@Manesh 你有什么例外
我在日志中有一个异常为 java.lang.NumberFormatException: For input string: "rows"
@Manesh 在哪里?你能突出显示确切的行并发布完整的堆栈跟踪吗?
【参考方案1】:
对于jsp代码,
<c:forEach items="$listOfMap" var="maps">
<c:forEach items="$maps" var="mapItem">
$mapItem.key $mapItem.value
</c:forEach>
</c:forEach>
在你的情况下,
<c:forEach items="$stkList" var="maps">
<c:forEach items="$maps" var="mapEntry">
$mapEntry['prdStk']
</c:forEach>
</c:forEach>
【讨论】:
差不多是老生常谈了,如何根据key设置显示,比如prdstk应该在Product Name栏下。 据您所知,Aravinds 解决方案有效,而且它不那么复杂,所以看看它。感谢您的帮助 @Manesh 代码简化,对你有帮助吗【参考方案2】:你需要改变的东西很少:
ResultSet 默认只转发,因为您多次访问列Stk_prdid
,它不会返回任何值。
public ArrayList getAllStockDetails()
...
Statement stmt = dcon.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
....
while (rs.next())
HashMap<String, String> stkMap = new HashMap<String, String>();//<-- move here
stkList.add(stkMap);
...
....
在jsp中:
...
<c:forEach var="row" items="$stkList">
...
<td>$row.prdStk</td>
...
...
【讨论】:
以上是关于如何使用 JSTL 在 jsp 页面中迭代 ArrayList<HashMap<String, String>>的主要内容,如果未能解决你的问题,请参考以下文章