激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術(shù)|

服務(wù)器之家 - 編程語言 - JAVA教程 - javaweb圖書商城設(shè)計之購物車模塊(3)

javaweb圖書商城設(shè)計之購物車模塊(3)

2020-07-04 10:43Android-Dev JAVA教程

這篇文章主要為大家詳細(xì)介紹了javaweb圖書商城設(shè)計之購物車模塊的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

購物車存儲

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

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

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

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

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
 * 購物車類
 */
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());//設(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();
  }
}

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
 * 購物車條目類
 *
 */
public class CartItem {
  private Book book;// 商品
  private int count;// 數(shù)量
 
  /**
   * 小計方法
   * @return
   * 處理了二進制運算誤差問題
   */
  public double getSubtotal() {//小計方法,但它沒有對應(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、添加購物車條目

javaweb圖書商城設(shè)計之購物車模塊(3)

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<%@ 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>
  <%-- 如果沒有車,或車的內(nèi)容集合為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>數(shù)量</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>

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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. 得到條目(得到圖書和數(shù)量)
     * 3. 把條目添加到車中
     */
    /*
     * 1. 得到車
     */
    Cart cart = (Cart)request.getSession().getAttribute("cart");
    /*
     * 表單傳遞的只有bid和數(shù)量
     * 2. 得到條目
     *  > 得到圖書和數(shù)量
     *  > 先得到圖書的bid,然后我們需要通過bid查詢數(shù)據(jù)庫得到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";
  }
 
  /**
   * 清空購物條目
   * @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";
  }
 
  /**
   * 刪除購物條目
   * @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、清空條目

javaweb圖書商城設(shè)計之購物車模塊(3)

4、刪除購物車條目

javaweb圖書商城設(shè)計之購物車模塊(3)

5、我的購物車

top.jsp中存在一個鏈接:我的購物車

我的購物車直接訪問/jsps/cart/list.jsp,它會顯示session中車的所有條目。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 综合国产在线 | xxxxhd73国产| 久久久久久久久久久av | 免费专区 - 91爱爱 | 精品国产一区二区三区四区阿崩 | 粉嫩粉嫩一区二区三区在线播放 | 激情久久婷婷 | 中国美女一级黄色大片 | 国产超碰人人做人人爱ⅴa 色天天综合网 | 国产91精品久久久久久 | 久久亚洲精品11p | 国产污污视频 | 综合成人在线 | 青青草成人影视 | 一区二区三区日韩在线观看 | 香蕉久久久精品 | 一区二区久久精品66国产精品 | 免费a级毛片大学生免费观看 | 人禽l交免费视频观看 视频 | 久草在线高清 | 3级毛片 | 国产精品久久久久久久久久妇女 | 免费观看的毛片手机视频 | 久久久久北条麻妃免费看 | 亚洲福利在线免费观看 | 精品三区视频 | 青青国产在线视频 | 538在线精品 | 国产亚洲精品yxsp | 56av国产精品久久久久久久 | 欧美视频在线一区二区三区 | 精品欧美一区二区精品久久久 | 黄色毛片18 | 色人阁导航| 玖玖精品视频在线 | 爽爽淫人综合网网站 | hdhdhd79xxxxх| 国产精品爱久久久久久久 | 91成人免费看 | 一级免费黄色免费片 | 91精品国产91久久久 |