javaweb图书商城设计之购物车模块(3)

 更新时间:2016年11月11日 14:06:37   作者:Android-Dev  
这篇文章主要为大家详细介绍了javaweb图书商城设计之购物车模块的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文继续为大家分享了javaweb图书商城中购物车模块,供大家参考,具体内容如下

购物车存储

保存在session中
保存在cookie中
保存在数据库中

1、创建相关类

购物车的结构:

CartItem:购物车条目,包含图书和数量
Cart:购物车,包含一个Map

/**
 * 购物车类
 */
public class Cart {
  private Map<String,CartItem> map = new LinkedHashMap<String,CartItem>();

  /**
   * 计算合计
   * @return
   */
  public double getTotal() {
    // 合计=所有条目的小计之和
    BigDecimal total = new BigDecimal("0");
    for(CartItem cartItem : map.values()) {
      BigDecimal subtotal = new BigDecimal("" + cartItem.getSubtotal());
      total = total.add(subtotal);
    }
    return total.doubleValue();
  }

  /**
   * 添加条目到车中
   * @param cartItem
   */
  public void add(CartItem cartItem) {
    if(map.containsKey(cartItem.getBook().getBid())) {//判断原来车中是否存在该条目
      CartItem _cartItem = map.get(cartItem.getBook().getBid());//返回原条目
      _cartItem.setCount(_cartItem.getCount() + cartItem.getCount());//设置老条目的数量为,其自己数量+新条目的数量
      map.put(cartItem.getBook().getBid(), _cartItem);
    } else {
      map.put(cartItem.getBook().getBid(), cartItem);
    }
  }

  /**
   * 清空所有条目
   */
  public void clear() {
    map.clear();
  }

  /**
   * 删除指定条目
   * @param bid
   */
  public void delete(String bid) {
    map.remove(bid);
  }

  /**
   * 获取所有条目
   * @return
   */
  public Collection<CartItem> getCartItems() {
    return map.values();
  }
}

/**
 * 购物车条目类
 * 
 */
public class CartItem {
  private Book book;// 商品
  private int count;// 数量

  /**
   * 小计方法
   * @return
   * 处理了二进制运算误差问题
   */
  public double getSubtotal() {//小计方法,但它没有对应的成员!
    BigDecimal d1 = new BigDecimal(book.getPrice() + "");
    BigDecimal d2 = new BigDecimal(count + "");
    return d1.multiply(d2).doubleValue();
  }

  public Book getBook() {
    return book;
  }

  public void setBook(Book book) {
    this.book = book;
  }

  public int getCount() {
    return count;
  }

  public void setCount(int count) {
    this.count = count;
  }
}

2、添加购物车条目

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <title>购物车列表</title>

  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
  <meta http-equiv="content-type" content="text/html;charset=utf-8">
  <!--
  <link rel="stylesheet" type="text/css" href="styles.css">
  -->
<style type="text/css">
  * {
    font-size: 11pt;
  }
  div {
    margin:20px;
    border: solid 2px gray;
    width: 150px;
    height: 150px;
    text-align: center;
  }
  li {
    margin: 10px;
  }

  #buy {
    background: url(<c:url value='/images/all.png'/>) no-repeat;
    display: inline-block;

    background-position: 0 -902px;
    margin-left: 30px;
    height: 36px;
    width: 146px;
  }
  #buy:HOVER {
    background: url(<c:url value='/images/all.png'/>) no-repeat;
    display: inline-block;

    background-position: 0 -938px;
    margin-left: 30px;
    height: 36px;
    width: 146px;
  }
</style>
 </head>

 <body>
<h1>购物车</h1>
<c:choose>
  <%-- 如果没有车,或车的内容集合为0长 --%>
  <c:when test="${empty sessionScope.cart or fn:length(sessionScope.cart.cartItems) eq 0}">
    <img src="<c:url value='/images/cart.png'/>" width="300"/>
  </c:when>
  <c:otherwise>
<table border="1" width="100%" cellspacing="0" background="black">
  <tr>
    <td colspan="7" align="right" style="font-size: 15pt; font-weight: 900">
      <a href="<c:url value='/CartServlet?method=clear'/>">清空购物车</a>
    </td>
  </tr>
  <tr>
    <th>图片</th>
    <th>书名</th>
    <th>作者</th>
    <th>单价</th>
    <th>数量</th>
    <th>小计</th>
    <th>操作</th>
  </tr>

<c:forEach items="${sessionScope.cart.cartItems }" var="cartItem">
  <tr>
    <td><div><img src="<c:url value='/${cartItem.book.image }'/>"/></div></td>
    <td>${cartItem.book.bname }</td>
    <td>${cartItem.book.author }</td>
    <td>${cartItem.book.price }元</td>
    <td>${cartItem.count }</td>
    <td>${cartItem.subtotal }元</td>
    <td><a href="<c:url value='/CartServlet?method=delete&bid=${cartItem.book.bid }'/>">删除</a></td>
  </tr>
</c:forEach>

  <tr>
    <td colspan="7" align="right" style="font-size: 15pt; font-weight: 900">
      合计:${sessionScope.cart.total }元
    </td>
  </tr>
  <tr>
    <td colspan="7" align="right" style="font-size: 15pt; font-weight: 900">
      <a id="buy" href="<c:url value='/OrderServlet?method=add'/>"></a>
    </td>
  </tr>
</table>
  </c:otherwise>
</c:choose>
 </body>
</html>

public class CartServlet extends BaseServlet {
  /**
   * 添加购物条目
   * @param request
   * @param response
   * @return
   * @throws ServletException
   * @throws IOException
   */
  public String add(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    /*
     * 1. 得到车
     * 2. 得到条目(得到图书和数量)
     * 3. 把条目添加到车中
     */
    /*
     * 1. 得到车
     */
    Cart cart = (Cart)request.getSession().getAttribute("cart");
    /*
     * 表单传递的只有bid和数量
     * 2. 得到条目
     *  > 得到图书和数量
     *  > 先得到图书的bid,然后我们需要通过bid查询数据库得到Book
     *  > 数量表单中有
     */
    String bid = request.getParameter("bid");
    Book book = new BookService().load(bid);
    int count = Integer.parseInt(request.getParameter("count"));
    CartItem cartItem = new CartItem();
    cartItem.setBook(book);
    cartItem.setCount(count);

    /*
     * 3. 把条目添加到车中
     */
    cart.add(cartItem);

    return "f:/jsps/cart/list.jsp";
  }

  /**
   * 清空购物条目
   * @param request
   * @param response
   * @return
   * @throws ServletException
   * @throws IOException
   */
  public String clear(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    /**
     * 1. 得到车
     * 2. 设置车的clear
     */
    Cart cart = (Cart)request.getSession().getAttribute("cart");
    cart.clear();
    return "f:/jsps/cart/list.jsp";
  }

  /**
   * 删除购物条目
   * @param request
   * @param response
   * @return
   * @throws ServletException
   * @throws IOException
   */
  public String delete(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    /*
     * 1. 得到车
     * 2. 得到要删除的bid
     */
    Cart cart = (Cart)request.getSession().getAttribute("cart");
    String bid = request.getParameter("bid");
    cart.delete(bid);
    return "f:/jsps/cart/list.jsp";
  }
}

3、清空条目

4、删除购物车条目

5、我的购物车

top.jsp中存在一个链接:我的购物车

我的购物车直接访问/jsps/cart/list.jsp,它会显示session中车的所有条目。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java网络编程之URL+URLconnection使用方法示例

    Java网络编程之URL+URLconnection使用方法示例

    这篇文章主要介绍了Java网络编程之URL+URLconnection使用方法示例,还是比较不错的,这里分享给大家,供需要的朋友参考。
    2017-11-11
  • Jmeter逻辑控制器事务控制器使用方法解析

    Jmeter逻辑控制器事务控制器使用方法解析

    这篇文章主要介绍了Jmeter逻辑控制器事务控制器使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-11-11
  • Springboot工具类FileCopyUtils使用教程

    Springboot工具类FileCopyUtils使用教程

    这篇文章主要介绍了Springboot内置的工具类之FileCopyUtils的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2022-12-12
  • Java设计模式之观察者模式(Observer Pattern)详解

    Java设计模式之观察者模式(Observer Pattern)详解

    观察者模式(Observer Pattern)是一种行为型设计模式,它定义了一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都能够自动地得到通知并进行更新,本文将详细的给大家介绍一下Java观察者模式,需要的朋友可以参考下
    2023-07-07
  • Spring如何通过注解存储和读取对象详解

    Spring如何通过注解存储和读取对象详解

    在Spring中,要想更简单的存储和读取对象的核心是使用注解,这篇文章主要给大家介绍了关于Spring如何通过注解存储和读取对象的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-07-07
  • Java使用opencv识别二维码的完整步骤

    Java使用opencv识别二维码的完整步骤

    OpenMV是一个开源,低成本,功能强大的机器视觉模块,下面这篇文章主要给大家介绍了关于Java使用opencv识别二维码的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2021-09-09
  • Java方法重写_动力节点Java学院整理

    Java方法重写_动力节点Java学院整理

    在Java和其他一些高级面向对象的编程语言中,子类可继承父类中的方法,而不需要重新编写相同的方法。但有时子类并不想原封不动地继承父类的方法,而是想作一定的修改,这就需要采用方法的重写。方法重写又称方法覆盖,下文给大家介绍java方法重写及重写规则,一起学习吧
    2017-04-04
  • 更改idea的JDK版本超简单便捷方法

    更改idea的JDK版本超简单便捷方法

    idea很多地方都设置了jdk版本,不同模块的jdk版本也可能不一样,下面这篇文章主要给大家介绍了关于更改idea的JDK版本的超简单便捷方法,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2023-11-11
  • SpringBoot声明式事务的简单运用说明

    SpringBoot声明式事务的简单运用说明

    这篇文章主要介绍了SpringBoot声明式事务的简单运用说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • Flyway详解及Springboot集成Flyway的详细教程

    Flyway详解及Springboot集成Flyway的详细教程

    Flayway是一款数据库版本控制管理工具,,支持数据库版本自动升级,Migrations可以写成sql脚本,也可以写在java代码里。这篇文章主要介绍了Flyway详解及Springboot集成Flyway的详细教程的相关资料,需要的朋友可以参考下
    2020-07-07

最新评论