gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 06:18:26 24 4
gpt4 key购买 nike

有人能告诉我为什么这段代码不起作用吗?我希望我的数据库中的所有行都显示在 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);

购物车.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>

最佳答案

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

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

应该是

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

如原因所述,您已将项目定义为字符串:

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

注意,这个类是一个字符串而不是购物车。

关于java - MethodNotFoundException - 在 JSP 表中显示来自 SQL 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34600084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com