本文介绍了如何迭代ArrayList< HashMap< String,String>>在使用JSTL的jsp页面中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JSTL显示数据库中的值,因为没有可重复使用的标记,所以我一直在使用另一个方案,按照我的上一个问题,,我开始使用servlet转到表的查看页面,我不能使用单个像在该链接中的三维数组,所以我使用了ArrayList>.

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

我的Servlet代码在下面给出

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.

我的日志中的错误如下

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)

推荐答案

您需要更改的内容很少:

There are few things you have to change:

ResultSet默认情况下仅转发,因为您多次访问列Stk_prdid,所以它不返回任何值.

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

在jsp中:

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

这篇关于如何迭代ArrayList&lt; HashMap&lt; String,String&gt;&gt;在使用JSTL的jsp页面中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 02:52