javax.servlet.ServletException:java.lang.InstantiationException:在作用域内找不到bean记录

时间:2016-09-23 06:21:11

标签: java servlets

我搜索了所有可能的解决方案,但仍然没有解决此问题的结果。它说在我的jsp的第4行,有一个错误,在范围内找不到bean。

这是我的jsp代码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<jsp:useBean id="records" type="java.sql.ResultSet" scope="request"></jsp:useBean>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet"
    href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script
    src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<title>Gasoline eStore</title>
</head>
<body>
    <h1>Gasoline eStore</h1>
        <table class="table" align="center" border="1" cellpadding="2" cellspacing="2">
                <tr>
                <th>Name</th>
                <th>Sales Amount</th>
                <th>Sales Gross Commission</th>
                <th>Sales Commission</th>
                <th>Take Home Pay</th>

                </tr>


                <%
                boolean empty = true;
                while (records.next()) { 
                empty = false;
                %>

                    <tr class="success">
                        <td align="center"><%=records.getInt("id") %></td>
                        <td align="center"><%=records.getString("gasType") %></td>
                        <td align="center"><%=records.getDouble("litervalue") %></td>
                        <td align="center"><%=records.getDouble("initialAmount") %></td>
                        <td align="center"><%=records.getDouble("vat") %></td>
                        <td align="center"><%=records.getDouble("totalAmount") %></td>      
                    </tr>   
                <% 
                } %>

            </table>
                <% if(empty){ %>    
                    <div class="form-group has-error">
                        <label class="control-label" for="inputError1">No records found.</label>
                    </div>
                <% } %>

            <form action="index.jsp" method="post"> 
            <p><input class="btn btn-primary" type="submit" value="GO BACK">
        </form>

</body>
</html>

这是我的Servlet

package gas.store.controller;

import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import gas.store.model.GasBean;
import gas.store.utility.DBConnectionUtil;
import java.sql.*;

public class ListRecordServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private Connection connection = null;

    public void init() throws ServletException {
        connection = DBConnectionUtil.getDBConnection();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if(connection != null) {
            ResultSet records = new GasBean().getAllRecordsTable(connection);

            request.setAttribute("records", records);

            request.getRequestDispatcher("listrecords.jsp").forward(request,response);
        } else {
            System.err.println("connection is NULL");
        }
    }

}``

1 个答案:

答案 0 :(得分:1)

使用JSTL

在JSP中删除Java代码并使用JSTL

在servlet代码中创建Record对象的ArrayList(每个记录的DTO)。 Record bean类具有适当的setter / getter。

e.g。

<c:forEach items="${records}" var="rec" varStatus="loop">

   <td align="center">${rec.id}</td>