一尘不染

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

jsp

我正在尝试使用JSTL显示数据库中的值,因为没有可重复使用的标记,所以我一直在使用另一个场景,根据我之前的问题,其他状态来限制<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)

阅读 338

收藏
2020-06-08

共1个答案

一尘不染

您需要更改的几件事:

默认情况下,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>
    ...
...
2020-06-08