gpt4 book ai didi

java - 如何使用 JSTL 在 jsp 页面中迭代 ArrayList>

转载 作者:行者123 更新时间:2023-11-30 03:46:38 25 4
gpt4 key购买 nike

我正在尝试使用 JSTL 显示数据库中的值,有标签,因为它不可重复使用,我一直在使用另一个场景,按照我之前的问题,Other Statergies to implemment <sql:query> ,我开始使用 servlet 转到表的 View 页面,我无法像该链接中那样使用一维数组,因此我使用了 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)

最佳答案

有几件事你必须改变:

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>
...
...

关于java - 如何使用 JSTL 在 jsp 页面中迭代 ArrayList<HashMap<String, String>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25501441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com