从数据库中检索表:javax.el.PropertyNotFoundException

Posted

技术标签:

【中文标题】从数据库中检索表:javax.el.PropertyNotFoundException【英文标题】:Retrieving a table from database: javax.el.PropertyNotFoundException 【发布时间】:2013-11-26 10:45:43 【问题描述】:

我想从数据库中检索整个表并将其显示到我的 jsp 页面中,但出现错误

我的 StaffBean。 java是

package com.staff.bean;
import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;


 public class StaffBean 

    private String date;
    private int workload;
    private int hourId;
    private int daysId;
    private int staffId;
    private String StaffName;
    private String ActiveORInactive;
    private String Stafftype;
    private String subcode;
    private boolean valid = false;

public String getdate() 
   // DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
 //   Date date = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); // your template here
    java.util.Date dateStr = null;
    try 
        dateStr = formatter.parse(date);
     catch (ParseException e) 
        // TODO Auto-generated catch block
        e.printStackTrace();
    
    java.sql.Date dateDB = new java.sql.Date(dateStr.getTime());
    return formatter.format(dateDB);

public void setdate(String date)
    this.date=date;

public int gethourId() 
    return hourId;

public void sethourId(int hourId) 
    this.hourId = hourId;

public int getdaysId() 
    return daysId;

public void setdaysId(int daysId) 
    this.daysId = daysId;

public int getworkload() 
    return hourId;

public void setworkload(int workload) 
    this.workload = workload;

    public int getstaffId() 
        return staffId;
    
    public void setstaffId(int staffId) 
        this.staffId = staffId;
    
    public String getStaffName() 
        return StaffName;
    
    public void setStaffName(String StaffName) 
        this.StaffName = StaffName;
    
    public String ActiveORInactive() 
        return ActiveORInactive;
    
    public void setActiveORInactive(String ActiveORInactive) 
        this.ActiveORInactive = ActiveORInactive;
    

    public String getStafftype() 
        return Stafftype;
    
    public void setStafftype(String Stafftype) 
        this.Stafftype = Stafftype;
    
    public String getsubcode() 
        return Stafftype;
    
    public void setsubcode(String subcode) 
        this.subcode = subcode;
    

    public boolean isValid() 
        return valid;
    
    public void setValid(boolean valid) 
        this.valid = valid;
    



我的 GetAllDetailDAO.java 是

 package com.staff.DAO;
 import java.sql.Connection;
 import java.sql.ResultSet;
  import java.sql.SQLException;
 import java.sql.Statement;
 import java.util.ArrayList;
 import java.util.List;

  import com.staff.bean.StaffBean;
  import com.staff.DB.ConnectionProvider;
  public class GetAllDetailDAO 

    Connection con;
    Statement stmt;
    private int noOfRecords;

    public List<StaffBean> viewAllStaff(int offSet, int noOfRecords)

        List<StaffBean> list = new ArrayList<StaffBean>();
        StaffBean _staffBean;
        try 
            con = ConnectionProvider.getConnection();
            stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("select SQL_CALC_FOUND_ROWS * from  tblstaffdetails limit "+offSet+","+noOfRecords);
                    //+offSet+","+noOfRecords);
            //SQL_CALC_FOUND_ROWS
            while(rs.next())
                _staffBean = new StaffBean();
                _staffBean.setStaffName(rs.getString("StaffName"));
                _staffBean.setstaffId(rs.getInt("staffId"));
                _staffBean.setActiveORInactive(rs.getString("ActiveORInactive"));
                _staffBean.setStafftype(rs.getString("Stafftype"));
                list.add(_staffBean);
            
            rs.close();          
            rs = stmt.executeQuery("SELECT FOUND_ROWS()");
            if(rs.next())
             this.noOfRecords = rs.getInt(1);

         catch (SQLException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        


        return list;

    
    public int getNoOfRecords() 
      return noOfRecords;
    

连接提供者 包 com.staff.DB;

  import java.sql.*;

public class ConnectionProvider 
    static Connection con;
    static String url;

    public static Connection getConnection() throws SQLException
        try 
              Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/StaffAllocation","root", "success");
        catch(ClassNotFoundException cnf)
            cnf.printStackTrace();
        

        return con;

    


而我的 GetAllDetailServlet.java 是

  package com.staff.servlet;
  import java.io.IOException;
  import java.util.List;
  import java.sql.*;

  import javax.servlet.RequestDispatcher;
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;

  import com.staff.bean.StaffBean;
  import com.staff.DAO.GetAllDetailDAO;
  import com.staff.DB.*;

  @WebServlet("/GetAllDetailServlet")
    public class GetAllDetailServlet extends HttpServlet




/**
 * Servlet implementation class GetAllDetailsServlet
 */


    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetAllDetailServlet() 
        super();
        // TODO Auto-generated constructor stub
    

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
        // TODO Auto-generated method stub

        int page = 1;
        int recordsPerPage = 4;
        if(request.getParameter("page") != null)
            page = Integer.parseInt(request.getParameter("page"));
            GetAllDetailDAO allDetailDAO = new GetAllDetailDAO();
            List<StaffBean> list  = allDetailDAO.viewAllStaff((page-1)*recordsPerPage, recordsPerPage);
        int noOfRecords = allDetailDAO.getNoOfRecords();
        int noOfPages = (int) Math.ceil(noOfRecords * 1.0 / recordsPerPage);
            request.setAttribute("staffList", list);
        request.setAttribute("noOfPages", noOfPages);
        request.setAttribute("currentPage", page);
            RequestDispatcher view = request.getRequestDispatcher("jsp/displayAllDetail.jsp");
            view.forward(request, response);

        
        

我的 DisplayAllDetail.jsp 是

  <html>
  <head>
   <link rel="stylesheet" type="text/css" href="css/style.css"/>
   <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <title> :: All Details</title>
  </head>
  <body>
  <fieldset>
   <table border="1" cellpadding="4" cellspacing="4" align="center">
<tr>
<th>staffId</th>
    <th>StaffName</th>
    <th>ActiveORInactive</th>
    <th>Stafftype</th>
    <c:forEach var="_staffBean" items="$staffList">
        <tr>
            <td>$_staffBean.staffId</td>
            <td>$_staffBean.StaffName</td>
            <td>$_staffBean.ActiveORInactive</td>
            <td>$_staffBean.Stafftype</td>        
        </tr>
    </c:forEach>
</tr>
    </table>
    <c:if test="$currentPage != 1">
<td><a href="getAll.do?method=get&page=$currentPage - 1">Prev</a></td>
    </c:if>
    <table border="1" cellpadding="5" cellspacing="5" align="center">
<tr>
    <c:forEach begin="1" end="$noOfPages " var="i">
        <c:choose>
            <c:when test="$currentPage eq i ">
                <td>$i</td>
            </c:when>
            <c:otherwise>
                <td><a href="getAll.do?method=get&page=$i">$i</a></td>
            </c:otherwise>
        </c:choose>
    </c:forEach>
</tr>
</table>
<c:if test="$currentPage lt noOfPages ">
<td><a href="getAll.do?method=get&page=$currentPage+1 ">Next</a></td>
</c:if>
</fieldset>
</body>
</html>

我的错误是

  HTTP Status 500 - An exception occurred processing JSP page /jsp/displayAllDetail.jsp at line 22
  type Exception report

  message An exception occurred processing JSP page /jsp/displayAllDetail.jsp at line 22

  description The server encountered an internal error that prevented it from fulfilling this request.

  exception 

  org.apache.jasper.JasperException: An exception occurred processing JSP page /jsp/displayAllDetail.jsp at line 22

19:         <c:forEach var="_staffBean" items="$staffList">
20:             <tr>
21:                 <td>$_staffBean.staffId</td>
22:                 <td>$_staffBean.StaffName</td>
23:                 <td>$_staffBean.ActiveORInactive</td>
24:                 <td>$_staffBean.Stafftype</td>        
25:             </tr>

堆栈跟踪: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) com.staff.servlet.GetAllDetailServlet.doGet(GetAllDetailServlet.java:56) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

根本原因

    javax.el.PropertyNotFoundException: Property 'StaffName' not found on type   com.staff.bean.StaffBean
javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:237)
javax.el.BeanELResolver$BeanProperties.access$400(BeanELResolver.java:214)
javax.el.BeanELResolver.property(BeanELResolver.java:325)
javax.el.BeanELResolver.getValue(BeanELResolver.java:85)
org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
org.apache.el.parser.AstValue.getValue(AstValue.java:183)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:1026)
org.apache.jsp.jsp.displayAllDetail_jsp._jspx_meth_c_005fforEach_005f0(displayAllDetail_jsp.java:157)
org.apache.jsp.jsp.displayAllDetail_jsp._jspService(displayAllDetail_jsp.java:99)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
com.staff.servlet.GetAllDetailServlet.doGet(GetAllDetailServlet.java:56)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

注意 Apache Tomcat/7.0.42 日志中提供了根本原因的完整堆栈跟踪。

【问题讨论】:

不要在标题中寻求帮助 - 在这里提出问题的每个人都希望得到答案。 在 getdate() 中,您将日期字符串解析为日期,然后再次将其格式化回字符串????我真的不认为你想那样做! NickJ,请帮我解决我的错误。我想检索一个表格并显示它,你能帮忙吗? 请用您所做的代码更改更新您的问题。我看不出还有什么问题。 我没有做太多的改变。唯一的问题是表格没有显示出来。 【参考方案1】:

尝试声明

    private String staffName;
    private String activeORInactive;
    private String stafftype;

而不是大写字母。

建议你关注Java Code convertions

【讨论】:

但是我已经创建了以大写字母开头的列的名称。如果我将它们编辑成更小的大小写会有什么变化? 你是如何映射你的数据库的?你在使用 OpenJpa,Hibernate 吗?如何初始化 ConnectionProvider? 对不起.. 我现在将我的连接提供商包括在我的问题中 我假设您使用的是 Hibernate。 JavaObject 属性名和列名彼此无关。只有你正在使用的映射(主要是 Hibernate 的 porpuse,让你忘记数据库名称,只使用面向对象的编程)在你的 java 对象中,你应该遵循约定,js 不会有问题 如果你是编程的初学者,并且你正在使用 Hibernate,我会劝阻你。 Hibernate 只适合高级程序员。我建议你改为学习 JDBC。【参考方案2】:

在 Blanca Hdez 的正确答案的基础上,更改 JSP,使 staffName 的大小写正确:

<td>$_staffBean.StaffName</td>

应该是:

<td>$_staffBean.staffName</td>

注意“staffName”的小写“s”——非常重要!

【讨论】:

你是否也听从了布兰卡·赫德兹的建议? 是的。我试着转换成小写字母。请检查我的jsp页面。错误在于说明 c:forEach var="_staffBean" items="$staffList" 应该赋予var什么值?必须用 _staffBean 替换什么

以上是关于从数据库中检索表:javax.el.PropertyNotFoundException的主要内容,如果未能解决你的问题,请参考以下文章

从数据库中检索表:javax.el.PropertyNotFoundException

如何从多个聚合表中检索数据?

我无法从 Laravel 6 中的相关表(模型)中检索数据

如何使用 PHP 表单从多个表中检索数据?

从 SQLite 表 iOS 中检索值

Django 从三个表中检索数据