好文档就是一把金锄头!
欢迎来到金锄头文库![会员中心]
电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

JSP购物车代码.doc

5页
  • 卖家[上传人]:慢***
  • 文档编号:233056414
  • 上传时间:2022-01-01
  • 文档格式:DOC
  • 文档大小:44KB
  • / 5 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • 购物车添加商品代码// 取出购物车和添加的书籍Map cart = (Map) session.getAttribute("cart");BookBean book = (BookBean) session.getAttribute("bookToAdd");// 如果购物车不存在,创建购物车if (cart == null) { cart = new HashMap();// 将购物车存入session之中 session.setAttribute("cart", cart);}// 判断书籍是否在购物车中CartItemBean cartItem = (CartItemBean) cart.get(book.getISBN());// 如果书籍在购物车中,更新其数量.// 否则,创建一个条目到Map中.if (cartItem != null){ cartItem.setQuantity(cartItem.getQuantity() + 1); cart.put(book.getISBN(),cartItem);}else cart.put(book.getISBN(), new CartItemBean(book, 1));// 转向viewCart.jsp显示购物车dispatcher = request.getRequestDispatcher("/viewCart.jsp");dispatcher.forward(request, response);显示购物车信息<% Map cart = (Map) session.getAttribute("cart"); double total = 0; if (cart == null || cart.size() == 0) out.println("购物车当前为空."); else { // 创建用于显示内容的变量 Set cartItems = cart.keySet(); //Iterator iterator = cartItems.iterator(); Object[] isbn = cartItems.toArray(); BookBean book; CartItemBean cartItem; int quantity; double price, subtotal;%>

      <% // continue scriptlet int i = 0; while (i < isbn.length) { // 计算总和 cartItem = (CartItemBean) cart.get((String)isbn[i]); book = cartItem.getBook(); quantity = cartItem.getQuantity(); price = book.getPrice(); subtotal = quantity * price; total += subtotal; i++;%> <% } %>
      书籍名称 数量 价格 小计
      <%= book.getTitle() %> <%= quantity %> <%= new DecimalFormat( "0.00" ).format( price ) %> <%= new DecimalFormat( "0.00" ).format( subtotal ) %>
      总计: <%= new DecimalFormat( "0.00" ).format( total ) %>
      <% // continue scriptlet // make current total a session attribute session.setAttribute( "total", new Double( total ) ); } // end of else%>

      继续购物

      购物车单项商品类public class CartItemBean implements Serializable { private static final long serialVersionUID = 1L; private BookBean book; private int quantity; /** * @return book */ public BookBean getBook() { return book; } /** * @param book * 要设置的 book */ public void setBook(BookBean book) { this.book = book; } /** * @return quantity */ public int getQuantity() { return quantity; } /** * @param quantity * 要设置的 quantity */ public void setQuantity(int quantity) { this.quantity = quantity; } public CartItemBean(com.entity.BookBean book, int number) { this.book = book; this.quantity = number; }}。

      点击阅读更多内容
      关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
      手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
      ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.