2016-09-23 164 views
-1

我搜索了每種可能的解決方案,但仍未找到解決此問題的結果。它說,在我的jsp的第4行中,存在一個錯誤,即在該範圍內未找到該bean。javax.servlet.ServletException:java.lang.InstantiationException:在範圍內未找到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

使用JSTL

在你的JSP擺脫的Java代碼,並使用JSTL

在你servlet代碼創建一個ArrayList Record對象(每個記錄都有一個DTO)。 Bean類將擁有合適的setter/getters。

例如

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

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