2016-09-23 9 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> 

である私のサーブレットが

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

を使用サーブレットコードRecordオブジェクト(各レコードのDTO)のArrayListを作成します。 Record beanクラスには、適切なセッター/ゲッターがあります。

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

    <td align="center">${rec.id}</td> 
関連する問題