最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

javaweb圖書商城設(shè)計(jì)之購(gòu)物車模塊(3)

 更新時(shí)間:2016年11月11日 14:06:37   作者:Android-Dev  
這篇文章主要為大家詳細(xì)介紹了javaweb圖書商城設(shè)計(jì)之購(gòu)物車模塊的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文繼續(xù)為大家分享了javaweb圖書商城中購(gòu)物車模塊,供大家參考,具體內(nèi)容如下

購(gòu)物車存儲(chǔ)

保存在session中
保存在cookie中
保存在數(shù)據(jù)庫(kù)中

1、創(chuàng)建相關(guān)類

購(gòu)物車的結(jié)構(gòu):

CartItem:購(gòu)物車條目,包含圖書和數(shù)量
Cart:購(gòu)物車,包含一個(gè)Map

/**
 * 購(gòu)物車類
 */
public class Cart {
  private Map<String,CartItem> map = new LinkedHashMap<String,CartItem>();

  /**
   * 計(jì)算合計(jì)
   * @return
   */
  public double getTotal() {
    // 合計(jì)=所有條目的小計(jì)之和
    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())) {//判斷原來(lái)車中是否存在該條目
      CartItem _cartItem = map.get(cartItem.getBook().getBid());//返回原條目
      _cartItem.setCount(_cartItem.getCount() + cartItem.getCount());//設(shè)置老條目的數(shù)量為,其自己數(shù)量+新條目的數(shù)量
      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();
  }
}

/**
 * 購(gòu)物車條目類
 * 
 */
public class CartItem {
  private Book book;// 商品
  private int count;// 數(shù)量

  /**
   * 小計(jì)方法
   * @return
   * 處理了二進(jìn)制運(yùn)算誤差問(wèn)題
   */
  public double getSubtotal() {//小計(jì)方法,但它沒(méi)有對(duì)應(yīng)的成員!
    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、添加購(gòu)物車條目

<%@ 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>購(gòu)物車列表</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>購(gòu)物車</h1>
<c:choose>
  <%-- 如果沒(méi)有車,或車的內(nèi)容集合為0長(zhǎng) --%>
  <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'/>">清空購(gòu)物車</a>
    </td>
  </tr>
  <tr>
    <th>圖片</th>
    <th>書名</th>
    <th>作者</th>
    <th>單價(jià)</th>
    <th>數(shù)量</th>
    <th>小計(jì)</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">
      合計(jì):${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 {
  /**
   * 添加購(gòu)物條目
   * @param request
   * @param response
   * @return
   * @throws ServletException
   * @throws IOException
   */
  public String add(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    /*
     * 1. 得到車
     * 2. 得到條目(得到圖書和數(shù)量)
     * 3. 把條目添加到車中
     */
    /*
     * 1. 得到車
     */
    Cart cart = (Cart)request.getSession().getAttribute("cart");
    /*
     * 表單傳遞的只有bid和數(shù)量
     * 2. 得到條目
     *  > 得到圖書和數(shù)量
     *  > 先得到圖書的bid,然后我們需要通過(guò)bid查詢數(shù)據(jù)庫(kù)得到Book
     *  > 數(shù)量表單中有
     */
    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";
  }

  /**
   * 清空購(gòu)物條目
   * @param request
   * @param response
   * @return
   * @throws ServletException
   * @throws IOException
   */
  public String clear(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    /**
     * 1. 得到車
     * 2. 設(shè)置車的clear
     */
    Cart cart = (Cart)request.getSession().getAttribute("cart");
    cart.clear();
    return "f:/jsps/cart/list.jsp";
  }

  /**
   * 刪除購(gòu)物條目
   * @param request
   * @param response
   * @return
   * @throws ServletException
   * @throws IOException
   */
  public String delete(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    /*
     * 1. 得到車
     * 2. 得到要?jiǎng)h除的bid
     */
    Cart cart = (Cart)request.getSession().getAttribute("cart");
    String bid = request.getParameter("bid");
    cart.delete(bid);
    return "f:/jsps/cart/list.jsp";
  }
}

3、清空條目

4、刪除購(gòu)物車條目

5、我的購(gòu)物車

top.jsp中存在一個(gè)鏈接:我的購(gòu)物車

我的購(gòu)物車直接訪問(wèn)/jsps/cart/list.jsp,它會(huì)顯示session中車的所有條目。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot整合Shiro實(shí)現(xiàn)權(quán)限控制的代碼實(shí)現(xiàn)

    SpringBoot整合Shiro實(shí)現(xiàn)權(quán)限控制的代碼實(shí)現(xiàn)

    Apache Shiro是一個(gè)強(qiáng)大且易用的Java安全框架,執(zhí)行身份驗(yàn)證、授權(quán)、密碼和會(huì)話管理,今天通過(guò)本文給大家介紹SpringBoot整合Shiro實(shí)現(xiàn)權(quán)限控制的方法,感興趣的朋友一起看看吧
    2021-07-07
  • 解決spring boot啟動(dòng)掃描不到自定義注解的問(wèn)題

    解決spring boot啟動(dòng)掃描不到自定義注解的問(wèn)題

    這篇文章主要介紹了解決spring boot啟動(dòng)掃描不到自定義注解的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-09-09
  • Springboot中spring-boot-starter-quartz的使用及說(shuō)明

    Springboot中spring-boot-starter-quartz的使用及說(shuō)明

    這篇文章主要介紹了Springboot中spring-boot-starter-quartz的使用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Jenkins自動(dòng)部署SpringBoot項(xiàng)目實(shí)踐教程

    Jenkins自動(dòng)部署SpringBoot項(xiàng)目實(shí)踐教程

    這篇文章主要介紹了Jenkins自動(dòng)部署SpringBoot項(xiàng)目實(shí)踐教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java數(shù)據(jù)結(jié)構(gòu)篇之實(shí)現(xiàn)二叉搜索樹的核心方法

    Java數(shù)據(jù)結(jié)構(gòu)篇之實(shí)現(xiàn)二叉搜索樹的核心方法

    二叉搜索樹是一種常用的數(shù)據(jù)結(jié)構(gòu),它是一棵二叉樹,且每個(gè)節(jié)點(diǎn)的值都大于其左子樹中任何節(jié)點(diǎn)的值,而小于其右子樹中任何節(jié)點(diǎn)的值,這篇文章主要給大家介紹了關(guān)于Java數(shù)據(jù)結(jié)構(gòu)篇之實(shí)現(xiàn)二叉搜索樹的核心方法,需要的朋友可以參考下
    2023-12-12
  • java線程并發(fā)blockingqueue類使用示例

    java線程并發(fā)blockingqueue類使用示例

    BlockingQueue是一種特殊的Queue,若BlockingQueue是空的,從BlockingQueue取東西的操作將會(huì)被阻斷進(jìn)入等待狀態(tài)直到BlocingkQueue進(jìn)了新貨才會(huì)被喚醒,下面是用BlockingQueue來(lái)實(shí)現(xiàn)Producer和Consumer的例子
    2014-01-01
  • Java Swing BoxLayout箱式布局的實(shí)現(xiàn)代碼

    Java Swing BoxLayout箱式布局的實(shí)現(xiàn)代碼

    這篇文章主要介紹了Java Swing BoxLayout箱式布局的實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • IDEA的Mybatis Log Plugin插件配置和使用詳解

    IDEA的Mybatis Log Plugin插件配置和使用詳解

    這篇文章主要介紹了IDEA的Mybatis Log Plugin插件配置和使用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Spring中使用ehcache緩存的方法及原理詳解

    Spring中使用ehcache緩存的方法及原理詳解

    這篇文章主要介紹了Spring中使用ehcache緩存的方法及原理詳解,ehcache具有很強(qiáng)的靈活性,提供了LRU、LFU和FIFO緩存淘汰算法,Ehcache 1.2引入了最近最少使用、最久未使用和先進(jìn)先 出緩存淘汰算法, 構(gòu)成了完整的緩存淘汰算法,,需要的朋友可以參考下
    2024-01-01
  • JAVA 多線程爬蟲實(shí)例詳解

    JAVA 多線程爬蟲實(shí)例詳解

    這篇文章主要介紹了JAVA 多線程爬蟲實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04

最新評(píng)論

思茅市| 阳山县| 固镇县| 浦县| 滨州市| 宽甸| 富民县| 密云县| 连州市| 合川市| 五原县| 海阳市| 布拖县| 云林县| 永和县| 普安县| 邯郸县| 北流市| 昔阳县| 宾川县| 仙居县| 张家港市| 古田县| 黄浦区| 西盟| 阳朔县| 绥芬河市| 奉贤区| 健康| 五指山市| 塔河县| 红安县| 探索| 荥阳市| 姜堰市| 巢湖市| 镇康县| 衡水市| 盈江县| 通道| 石嘴山市|