在表中显示SQL行

时间:2016-01-04 16:35:57

标签: spring spring-mvc html-table

如何在JSP表中显示SQL数据库中的所有行?

为了一行代表表中的一个'td'标签。下面的代码仅显示我数据库的最后一行。

CartController.java

@RequestMapping(value="/cart.html", method = RequestMethod.GET)
public ModelAndView cartPage(Model model) throws SQLException, ClassNotFoundException {

    PreparedStatement pst;
    ResultSet rs;
    Connection con;

    con = db.getConnect(); 

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

    while (rs.next()) {
        model.addAttribute("ID", rs.getString(1));
        model.addAttribute("picture", rs.getString(2));
        model.addAttribute("name", rs.getString(3));
        model.addAttribute("company", rs.getString(4));
        model.addAttribute("type", rs.getString(5));
        model.addAttribute("price", rs.getString(6));
    }

    ModelAndView cart = new ModelAndView("Cart");

    return cart; 

}

这是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>
        <tr>
        <td>${ID}</td>
        <td><img src="${picture}"/></td>
        <td>${name}</td>
        <td>${company}</td>
        <td>${type}</td>
        <td>${price}</td>
        <td></td>
        </tr>

    </table>
</div>

错误:

type Exception report

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

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 74

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


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.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.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)


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:237)
org.apache.jsp.WEB_002dINF.Cart_jsp._jspService(Cart_jsp.java:190)
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.

1 个答案:

答案 0 :(得分:4)

因为你正在覆盖购物车信息,显然最后一排赢了赌注。您应该在控制器中保留List Cart个并将其推送到您的视图中。然后在视图中,迭代它们并将它们放在每一行中。

假设您有Cart这样的类:

public class Cart {
    private String ID;
    private String picture;
    private String name;
    private String company;
    private String type;
    private String price;

    // getters and setters
}

更改您的控制器以创建List Cart个并将每个Cart添加到列表中:

...
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);
...

在视图中:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<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>