javax.el.PropertyNotFoundException:

时间:2016-01-28 11:58:02

标签: java jsp servlets

我正在学习jsp,servlets等等。我有一个例外,我只是无法找出错误。我知道El不能找到错误的字段,但我的JavaBean很好。传递给jsp页面的列表也很好。 已经更改了jsp和servlet,异常继续。

希望some1可以帮助我。

继承守则和StackTrace:

堆栈跟踪:

    SEVERE: Servlet.service() for servlet jsp threw exception
    javax.el.PropertyNotFoundException: Property 'nome' not found on type      java.lang.String
    at javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:268)
    at javax.el.BeanELResolver$BeanProperties.access$300(BeanELResolver.java:221)
    at javax.el.BeanELResolver.property(BeanELResolver.java:355)
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:95)
    at org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:110)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:169)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
    at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:943)
    at org.apache.jsp.admin.buscar_jsp._jspx_meth_c_005fforEach_005f0(buscar_jsp.java:253)
    at org.apache.jsp.admin.buscar_jsp._jspx_meth_c_005fif_005f0(buscar_jsp.java:215)
    at org.apache.jsp.admin.buscar_jsp._jspService(buscar_jsp.java:162)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:719)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:465)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:390)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:317)
    at control.ControleProduto.buscar(ControleProduto.java:140)
    at control.ControleProduto.execute(ControleProduto.java:65)
    at control.ControleProduto.doPost(ControleProduto.java:46)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

Excessa no buscar An exception occurred processing JSP page /admin/buscar.jsp at line 33

30:             </tr>
31:             <c:forEach items="lista" var="prod">
32:                     <tr>
33:                         <td>${prod.nome }</td> 
34:                         <td>${prod.estoque }</td>
35:                         <td><fmt:formatNumber value="${prod.preco}" type="currency"/> </td>
36:                         <td> <fmt:formatDate value="${prod.validade}"  pattern="dd/MM/yyyy" /> </td>


Stacktrace:
org.apache.jasper.JasperException: An exception occurred processing JSP page /admin/buscar.jsp at line 33

30:             </tr>
31:             <c:forEach items="lista" var="prod">
32:                     <tr>
33:                         <td>${prod.nome }</td> 
34:                         <td>${prod.estoque }</td>
35:                         <td><fmt:formatNumber value="${prod.preco}" type="currency"/> </td>
36:                         <td> <fmt:formatDate value="${prod.validade}"  pattern="dd/MM/yyyy" /> </td>


Stacktrace:
    at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:574)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:719)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:465)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:390)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:317)
    at control.ControleProduto.buscar(ControleProduto.java:140)
    at control.ControleProduto.execute(ControleProduto.java:65)
    at control.ControleProduto.doPost(ControleProduto.java:46)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javax.el.PropertyNotFoundException: Property 'nome' not found on type java.lang.String
    at javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:268)
    at javax.el.BeanELResolver$BeanProperties.access$300(BeanELResolver.java:221)
    at javax.el.BeanELResolver.property(BeanELResolver.java:355)
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:95)
    at org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:110)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:169)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
    at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:943)
    at org.apache.jsp.admin.buscar_jsp._jspx_meth_c_005fforEach_005f0(buscar_jsp.java:253)
    at org.apache.jsp.admin.buscar_jsp._jspx_meth_c_005fif_005f0(buscar_jsp.java:215)
    at org.apache.jsp.admin.buscar_jsp._jspService(buscar_jsp.java:162)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
    ... 38 more

继承豆子:

package entity;

import java.util.Date;
import java.util.GregorianCalendar;

public class Produto {

    private Integer idProduto;
    private String nome;
    private Integer estoque;
    private Double preco;
    private Date validade;

    /*
     * Produto tem uma Categoria
     */
    private Categoria categoria;

    public Produto(Integer idProduto, String nome, Integer estoque,
            Double preco, Date validade) {
        super();
        this.idProduto = idProduto;
        this.nome = nome;
        this.estoque = estoque;
        this.preco = preco;
        this.validade = validade;
    }

    public Produto() {
        super();
    }

    public Categoria getCategoria() {
        return categoria;
    }
    public void setCategoria(Categoria categoria) {
        this.categoria = categoria;
    }
    public Integer getIdProduto() {
        return idProduto;
    }
    public void setIdProduto(Integer idProduto) {
        this.idProduto = idProduto;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public Integer getEstoque() {
        return estoque;
    }
    public void setEstoque(Integer estoque) {
        this.estoque = estoque;
    }
    public Double getPreco() {
        return preco;
    }
    public void setPreco(Double preco) {
        this.preco = preco;
    }
    public Date getValidade() {
        return validade;
    }
    public void setValidade(Date validade) {
        this.validade = validade;
    }

    public static Date converterData(String data){
        String[] novaData = data.split("/");
        GregorianCalendar cal = new GregorianCalendar(
                new Integer(novaData[2]),
                new Integer(novaData[1]) - 1,
                new Integer(novaData[0]));
        return cal.getTime();
    }


    @Override
    public String toString() {
        return "Produto [idProduto=" + idProduto + ", nome=" + nome
                + ", estoque=" + estoque + ", preco=" + preco + ", validade="
                + validade + "]";
    }


}

JSP页面:

Topo.jsp

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

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Sistema de Controle de Produtos</title>
<!--  CSS Styles -->
<link rel="stylesheet" type="text/css"
     href="${pageContext.request.contextPath}/bootstrap/css/bootstrap.min.css">
<!--  Tabela com cores diferentes -->
<link rel="stylesheet" type="text/css"
     href="${pageContext.request.contextPath}/css/tabelacomcores.css">


</head>
<body>

    <!-- Como criar submenus? -->
    <div class="container" >
        <div class="col-xs-12">
            <ul class="nav nav-pills">
                <li><a href="index.jsp">Cadastrar</a></li>
                <li><a href="buscar.jsp">Consultar</a></li>
                <li><a href="#">Sair</a>            

            </ul>
        </div> <!--  Menu -->

rodape.jsp

    </div> 

</div> <!--  container -->  

</body>
</html>

buscar.jsp

<jsp:include page="topo.jsp"></jsp:include>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<!-- Format library -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>


<h3 class="page-header">Buscar produtos</h3>

<form method="post" action="buscar.html">
    <div class="form-group">
        Nome :
        <input type="text" name="nome" value="${nome}" class="form-control">
    </div>
    <input type="submit" value="Buscar" class="btn btn-primary">
</form>

    ${msg }

    <c:if test="${fn:length(lista)>0}">
        <table class="table table-bordered">
            <tr>
                <th>Nome</th>
                <th>Estoque</th>
                <th>Preco</th>
                <th>Validade</th>
                <th>Categoria</th>
                <th></th>
            </tr>
            <c:forEach items="lista" var="prod">
                    <tr>
                        <td>${prod.nome }</td> 
                        <td>${prod.estoque }</td>
                        <td><fmt:formatNumber value="${prod.preco}" type="currency"/> </td>
                        <td> <fmt:formatDate value="${prod.validade}"  pattern="dd/MM/yyyy" /> </td>
                        <td>${prod.categoria.nomeCategoria }</td>
                        <td>
                            <a href="editar.html?id=${prod.idProduto}" class="btn btn-warning">Editar</a>
                            <a href="excluir.html?id=${prod.idProduto}" class="btn btn-danger"
                                onclick="return confirm('Deseja excluir realmente o produto?')">Excluir</a>
                        </td>
                </tr>
            </c:forEach>
        </table>
    </c:if> 

    <jsp:include page="rodape.jsp"></jsp:include>

Dao.java

package persistence;

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

public class Dao {

    Connection con;
    PreparedStatement stmt;
    ResultSet rs;

    private final String URL = "jdbc:mysql://localhost:3306/aula";
    private final String USER = "root";
    private final String PASS = "something";

    protected void open()throws Exception{
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection(URL, USER, PASS);
    }

    protected void close()throws Exception{
        if(rs != null)
            rs.close();

        if(stmt != null)
            stmt.close();

        if(con != null)
            con.close();
    }

}

ProdutoDao.java

package persistence;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import entity.Categoria;
import entity.Produto;

public class ProdutoDao extends Dao {

    public void cadastrar(Produto prod)throws Exception{
        open();

        stmt = con.prepareStatement("insert into produto (nome,estoque,preco,validade,idCategoria)"
                + " values(?,?,?,?,?)");

        stmt.setString(1, prod.getNome());
        stmt.setInt(2, prod.getEstoque());
        stmt.setDouble(3, prod.getPreco());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        stmt.setString(4, sdf.format(prod.getValidade()));
        stmt.setInt(5, prod.getCategoria().getIdCategoria());

        stmt.execute();

        close();
    }

    public List<Produto> buscar(String nome)throws Exception{
        open();

        stmt = con.prepareStatement("select * from produto inner join categoria "
                + " on produto.idcategoria = categoria.idcategoria where nome like ?");
        stmt.setString(1, nome + "%");

        rs = stmt.executeQuery();
        List<Produto> lista = new ArrayList<Produto>();

        while(rs.next()){
            Produto prod = new Produto(rs.getInt("idproduto"), rs.getString("nome"), 
                    rs.getInt("estoque"), rs.getDouble("preco"), rs.getDate("validade"));
            Categoria cat = new Categoria(rs.getInt("idcategoria"), 
                    rs.getString("nomecategoria"));
            prod.setCategoria(cat);
            lista.add(prod);
        }
        close();
        return lista;
    }


    /**
     * Busca pela ID do produto
     * @param id
     * @return 
     * @throws Exception
     */
    public Produto buscar(int id) throws Exception{
        open();

        stmt = con.prepareStatement("select * from produto inner join categoria "
                + " on idcategoria = idcategoria where idproduto = ?");
        stmt.setInt(1, id);
        Produto prod = null;
        rs = stmt.executeQuery();
        if(rs.next()){
            prod = new Produto(rs.getInt("idproduto"), rs.getString("nome"), 
                    rs.getInt("estoque"), rs.getDouble("preco"), rs.getDate("validade"));
            Categoria cat = new Categoria(rs.getInt("idcategoria"), 
                    rs.getString("nomecategoria"));
            prod.setCategoria(cat);
        }
        close();
        return prod;
    }

    public void excluir(int id)throws Exception{
        open();
        stmt = con.prepareStatement("delete from produto where idproduto = ?");
        stmt.setInt(1, id);
        stmt.execute();
        close();
    }


    public void editar(Produto prod)throws Exception{
        open();
        stmt = con.prepareStatement("update produto set nome = ?, estoque = ?, preco = ?, "
                + " validade = ?, categoria_idcategoria = ? where idproduto = ?");
        stmt.setString(1, prod.getNome());
        stmt.setInt(2, prod.getEstoque());
        stmt.setDouble(3, prod.getPreco());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        stmt.setString(4, sdf.format(prod.getValidade()));
        stmt.setInt(5, prod.getCategoria().getIdCategoria());
        stmt.setInt(6, prod.getIdProduto());

        stmt.execute();
        close();

    }
}

1 个答案:

答案 0 :(得分:0)

对不起,我发现了。

c:forEach items =必须是参数&#34; $ {lista}&#34;而不只是&#34; lista&#34;。

解决。不管怎样,谢谢。