2016-04-25 222 views
0

我正在嘗試使用可添加到購物車的某些庫存編寫簡單的Web應用程序。我將複製代碼和jsp文件。我希望我能夠解決這個問題。我從數據庫中獲取數據。現在我想顯示什麼是添加到購物車,然後我會嘗試將它保存在分貝。Spring MVC應用程序將庫存添加到購物車jsp JAVA

全堆棧跟蹤。

org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/views/cart.jsp at line 14 

11:  <div style="padding-right: 40px"> 
12:   <table border="1"> 
13:    <tr> 
14:     <c:forEach items="${cpu}" var="c"> 
15:      <td>Name</td> 
16:      <td>${c.name}</td> 
17:     </c:forEach> 


Stacktrace: 
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:574) 
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:461) 
    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:968) 
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:870) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:648) 
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
    org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108) 
root cause 

javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in &lt;forEach&gt; 
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:909) 
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:838) 
    org.apache.jsp.WEB_002dINF.views.cart_jsp._jspService(cart_jsp.java:156) 
    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:968) 
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:870) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:648) 
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
    org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108) 
root cause 

javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in &lt;forEach&gt; 
    org.apache.taglibs.standard.tag.common.core.ForEachSupport.toForEachIterator(ForEachSupport.java:274) 
    org.apache.taglibs.standard.tag.common.core.ForEachSupport.supportedTypeForEachIterator(ForEachSupport.java:238) 
    org.apache.taglibs.standard.tag.common.core.ForEachSupport.prepare(ForEachSupport.java:155) 
    javax.servlet.jsp.jstl.core.LoopTagSupport.doStartTag(LoopTagSupport.java:256) 
    org.apache.jsp.WEB_002dINF.views.cart_jsp._jspx_meth_c_005fforEach_005f0(cart_jsp.java:178) 
    org.apache.jsp.WEB_002dINF.views.cart_jsp._jspService(cart_jsp.java:138) 
    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:968) 
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:870) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:648) 
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
    org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108) 

cart.jsp

<body> 
    <div style="padding-right: 40px"> 
     <table border="1"> 
      <tr> 
       <c:forEach items="${cpu}" var="c"> 
        <td>Name</td> 
        <td>${c.name}</td> 
       </c:forEach> 



    </div> 

控制器 @Controller

public class CartController { 

    @RequestMapping(value = "/cart", method = RequestMethod.POST) 
    public String someAction(@ModelAttribute("cpu") Cpu cpu, Model model) { 
     model.addAttribute("cpu", cpu); 
     return "cart"; 
    } 
} 

的Cpu

@SuppressWarnings("serial") 
public class Cpu implements Serializable { 

    public static final String SESSION_PROPERTY = "cpu"; 

    private int id; 

    private String name; 

    private String socket; 

    private int price; 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getSocket() { 
     return socket; 
    } 

    public void setSocket(String socket) { 
     this.socket = socket; 
    } 

    public int getPrice() { 
     return price; 
    } 

    public void setPrice(int price) { 
     this.price = price; 
    } 

} 
+1

您可以從「不知道如何遍歷提供的項目」到您的'Cpu'類型(顯然不會實現'Iterable')一路跟蹤錯誤。不可能知道,因爲這是您決定*不*包含的唯一代碼。 – kryger

+0

@kryger我已經添加了cpu類。 – RockOrDead

回答

0

你試圖使用<c:forEach>在您的自定義Cpu類型的單個實例進行迭代,而迭代明顯的要求是...有,你可以遍歷對象的集合。這就是JSTL規範說:

大量的集合類型由<c:forEach>支持,包括java.util.Collection中的所有實現(包括表,鏈表,ArrayList中,向量,堆棧,集),和java.util.Map(包括HashMap,Hashtable,Properties,Provider,Attributes)。

(...)

java.util.Iterator中和的java.util.Enumeration的實現都 支持,以及(...)。

您的控制器應更改爲創建List<Cpu>,稍後將通過模型屬性傳遞給JSP頁面。在網上商店中實施購物車的常見習慣用法是使用HttpSession來累積所有用戶的物品。稍後,您可以訪問CartController中的此共享會話對象,並檢索用戶放置的所有項目。

+0

你能告訴我如何做到這一點? – RockOrDead

+0

你一週前已經在處理這個問題:http://stackoverflow.com/q/36682201/1240557。如果有一些基本概念你不明白,你應該從一個有信譽的來源參考正式文件或教程,而不是「嘗試一切,直到它工作」。 – kryger

相關問題