How to iterate ArrayList> in a jsp page u

2020-04-21 01:01发布

问题:

I'm trying to display the values from my database using JSTL there is the tag since it is not re usable i have been using another scenario as per my previous question, Other Statergies to implemment <sql:query>, I started using servlets to go to my view page for the table, I cannot use a single dimensional array as in that link so i used ArrayList>.

My code that fetches the values from the database is given below.

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;
}

My Servlet code is given below

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
}

Code in my page is given below

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

My page on load only returns blank page, what did i do wrong in this, correct me if the code I have done is wrong.

The error in my log is as follows

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)

回答1:

There are few things you have to change:

ResultSet are by-default forward only, since your accessing column Stk_prdid multiple time , it does not returns any value.

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);
        ...
    }
    ....
}

In jsp:

...
<c:forEach var="row" items="${stkList}">
    ...
    <td>${row.prdStk}</td>
    ...
...


回答2:

For jsp code,

<c:forEach items="${listOfMap}" var="maps">
       <c:forEach items="${maps}" var="mapItem">
             ${mapItem.key} ${mapItem.value}
       </c:forEach>
</c:forEach>

In your case,

<c:forEach items="${stkList}" var="maps">
           <c:forEach items="${maps}" var="mapEntry">
              ${mapEntry['prdStk']}
           </c:forEach>
</c:forEach>