MethodNotFoundException - 在JSP表中显示SQL中的数据

时间:2016-01-04 21:22:04

标签: java mysql spring jsp

有人可以告诉我为什么这段代码不起作用? 我希望我的数据库中的所有行都会显示在JSP表中(就像在SQL表中一样),但这段代码会导致错误..为什么?

PhoneController的片段:

    con = db.getConnect();

    pst = con.prepareStatement("SELECT * FROM onlineshop.cart");
    rs = pst.executeQuery();


    List<Cart> carts = new ArrayList<>();
    while (rs.next()) {
                Cart cart = new Cart();
                cart.setID(rs.getString(1));
                cart.setPicture(rs.getString(2));
                cart.setName(rs.getString(3));
                cart.setCompany(rs.getString(4));
                cart.setType(rs.getString(5));
                cart.setPrice(rs.getString(6));

                carts.add(cart);
            }
    model.addAttribute("carts", carts);

Cart.jsp:

<div style="padding-right: 40px">
    <table border="1">


        <tr>
            <td>ID</td>
            <td>Product</td>
            <td>Name</td>
            <td>Company</td>
            <td>Type</td>
            <td>Price</td>
            <td>Action</td>
        </tr>
        <c:forEach var="cart" items="carts">
            <tr>
                <td>${cart.getID()}</td>
                <td><img src="${cart.getPicture()}" /></td>
                <td>${cart.getName()}</td>
                <td>${cart.getCompany()}</td>
                <td>${cart.getType()}</td>
                <td>${cart.getPrice()}</td>
                <td></td>
            </tr>
        </c:forEach>

错误:

 An exception occurred processing JSP page /WEB-INF/Cart.jsp at line 72

 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 /WEB-INF/Cart.jsp at line 72

69:             </tr>
70:             <c:forEach var="cart" items="carts">
71:                 <tr>
72:                     <td>${cart.getID()}</td>
73:                     <td><img src="${cart.getPicture()}" /></td>
74:                     <td>${cart.getName()}</td>
75:                     <td>${cart.getCompany()}</td>


Stacktrace:
     org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrap   per.java:574)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputM    odel(InternalResourceView.java:168)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.ja   va:1243)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(Dispa    tcherServlet.java:1027)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServle    t.java:971)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:871)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


 root cause
javax.el.MethodNotFoundException: Method not found: class      java.lang.String.getID()
javax.el.Util.findWrapper(Util.java:351)
javax.el.Util.findMethod(Util.java:213)
javax.el.BeanELResolver.invoke(BeanELResolver.java:156)
org.apache.jasper.el.JasperELResolver.invoke(JasperELResolver.java:147)
org.apache.el.parser.AstValue.getValue(AstValue.java:159)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:943)
org.apache.jsp.WEB_002dINF.Cart_jsp._jspx_meth_c_005fforEach_005f0(Cart_jsp.java:236)
org.apache.jsp.WEB_002dINF.Cart_jsp._jspService(Cart_jsp.java:189)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1243)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:871)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
  org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


note The full stack trace of the root cause is available in the Apache  Tomcat/8.0.29 logs.

的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
  xmlns="http://java.sun.com/xml/ns/javaee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> 
<display-name>OnlineShop</display-name>

<servlet>
    <servlet-name>spring-dispatcher</servlet-name>
    <servlet-  class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>spring-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

1 个答案:

答案 0 :(得分:2)

您需要正确定义购物车。第70行:

70:             <c:forEach var="cart" items="carts">

应该是

70:             <c:forEach var="cart" items="${carts}">

您将项目定义为String,如下所示:

javax.el.MethodNotFoundException: Method not found: class      java.lang.String.getID()

请注意,该类是字符串而不是购物车。