This question already has answers here:
javax.el.PropertyNotFoundException: Property 'foo' not found on type com.example.Bean
                                
                                    (5个答案)
                                
                        
                3年前关闭。
            
        

我正在制作一个页面,您可以在其中更新,编辑,删除,插入数据库中的数据。插入页面运行良好,并且数据已成功插入,但是当我单击“添加”按钮时,我没有重定向到AllGrocery页面,但是Eclipse给了我这个错误。无法确定问题出在哪里。可以有人帮我吗?数据库:GroceryStore,表:杂货店(id,名称,价格)

DataAccess.java

package dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import com.sun.istack.internal.logging.Logger;

import db.DBUtils;
import model.Grocery;

public class DataAccess {

    public void addNew(Grocery g) throws ClassNotFoundException, SQLException{
        try {
        PreparedStatement ps = DBUtils.getPreparedStatement("insert into grocerys values(?,?,?)");
        ps.setInt(1, g.getID());
        ps.setString(2, g.getName());
        ps.setDouble(3, g.getPrice());

        ps.executeUpdate();

        } catch(ClassNotFoundException | SQLException ex){

        }
    }

    public static List<Grocery> getAll(){
        List<Grocery> gs = new LinkedList<>();

        try {
            ResultSet rs = DBUtils.getPreparedStatement("select * from grocerys").executeQuery();

            while (rs.next()){
                Grocery g = new Grocery();
                g.setID(rs.getInt(1));
                g.setName(rs.getString(2));
                g.setPrice(rs.getDouble(3));

                gs.add(g);
            }

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

        return gs;
    }
}


DBUtils.java

package db;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.SQLException;

public class DBUtils {

    public static PreparedStatement getPreparedStatement(String sql) throws ClassNotFoundException, SQLException{

        PreparedStatement ps = null;

        Class.forName("com.mysql.jdbc.Driver");

        String url = "jdbc:mysql://localhost:3306/GroceryStore";
        String user = "root";
        String pass = "adil";

        Connection conn = DriverManager.getConnection(url,user,pass);

        ps = conn.prepareStatement(sql);


        return ps;

    }

    public static void main(String[] args)throws ClassNotFoundException, SQLException {

        getPreparedStatement("select * from grocerys");

    }

}


杂货店

package model;

public class Grocery {

    private int id;
    private String name;
    private Double price;

    public void setID(int id_){
        id = id_;
    }

    public void setName(String name_){
        name = name_;
    }

    public void setPrice(Double price_){
        price = price_;
    }

    public int getID(){
        return id;
    }

    public String getName(){
        return name;
    }

    public Double getPrice(){
        return price;
    }

}


AllGrocery.java

package servlet;

import java.io.IOException;

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 dao.DataAccess;

/**
 * Servlet implementation class AllGrocery
 */
@WebServlet("/AllGrocery")
public class AllGrocery extends HttpServlet {
    private static final long serialVersionUID = 1L;

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

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setAttribute("AllGrocery", DataAccess.getAll());

        RequestDispatcher rd = request.getRequestDispatcher("AllGrocery.jsp");

        rd.forward(request, response);



    }

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

}


        public Double getPrice(){
            return price;
        }

    }


AddNew.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Add Grocery</title>
</head>
<body>

<h1>Add New Grocery</h1>
<form action = "ManagerAddNew.jsp" method = "post">
<table>

<tr>
<th>ID</th>
<td><input type = "number" name = "id" style = "width : 200px;"/></td>
</tr>

<tr>
<th>NAME</th>
<td><input type = "text" name = "name" style = "width : 200px;"/></td>
</tr>


<tr>
<th>PRICE</th>
<td><input type = "number" name = "price" style = "width : 200px;"/></td>
</tr>


<tr>
<th></th>
<td><input type = "submit" name = "submit" value = "ADD"/></td>
</tr>

</table>
</form>

</body>
</html>


AllGrocery.jsp

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

    <%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>

<!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">
<title>Grocerys Available</title>
</head>
<body>

<div syle = "width : 1200px; margin-left:auto;margin-right:auto;">

<table cellpadding = "10">

<tr>

<th>ID</th>
<th>Name</th>
<th>Price</th>

</tr>

<c:forEach items = "${AllGrocery}" var = "p">

<tr>
<td>${p.id}</td>
<td>${p.name}</td>
<td>${p.price}</td>
<td>

<a href = "edit?id = ${p.id}">Edit</a>
<a href = "delete?id = ${p.id}">Delete</a>

</td>
</tr>


</c:forEach>

</table>

</div>

</body>
</html>


ManagerAddNew.jsp

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

    <%@ page import = "model.Grocery" %>
    <%@ page import = "dao.DataAccess" %>

<!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">
<title>Insert title here</title>
</head>
<body>

<%

String name = request.getParameter("name");
int id = Integer.parseInt(request.getParameter("id"));
Double price = Double.parseDouble(request.getParameter("price"));

Grocery g = new Grocery();
g.setID(id);
g.setName(name);
g.setPrice(price);

DataAccess da = new DataAccess();

da.addNew(g);

response.sendRedirect("/SimpleServletProject/AllGrocery");

%>

</body>
</html>


错误:

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

26: <c:forEach items = "${AllGrocery}" var = "p">
27:
28: <tr>
29: <td>${p.id}</td>
30: <td>${p.name}</td>
31: <td>${p.price}</td>
32: <td>


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:575)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    servlet.AllGrocery.doGet(AllGrocery.java:38)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


root cause

javax.el.PropertyNotFoundException: Property 'id' not found on type model.Grocery
    javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:290)
    javax.el.BeanELResolver$BeanProperties.access$300(BeanELResolver.java:243)
    javax.el.BeanELResolver.property(BeanELResolver.java:377)
    javax.el.BeanELResolver.getValue(BeanELResolver.java:97)
    org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
    org.apache.el.parser.AstValue.getValue(AstValue.java:184)
    org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
    org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:950)
    org.apache.jsp.AllGrocery_jsp._jspx_meth_c_005fout_005f0(AllGrocery_jsp.java:215)
    org.apache.jsp.AllGrocery_jsp._jspx_meth_c_005fforEach_005f0(AllGrocery_jsp.java:163)
    org.apache.jsp.AllGrocery_jsp._jspService(AllGrocery_jsp.java:113)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    servlet.AllGrocery.doGet(AllGrocery.java:38)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

最佳答案

如果看到异常,则无法在Grocery对象中找到属性ID。您的ID的getter方法名称应更改为getId(),而不是getID()。它应该是getId(),因为当您camelCase时,您只是将变量的第一个字符更改为大写而不是整个变量。

10-08 13:25