一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

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

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術(shù)|正則表達(dá)式|

服務(wù)器之家 - 編程語言 - JAVA教程 - mvc架構(gòu)實(shí)現(xiàn)商品的購買(二)

mvc架構(gòu)實(shí)現(xiàn)商品的購買(二)

2020-07-05 13:31scx_white JAVA教程

這篇文章主要為大家詳細(xì)介紹了mvc架構(gòu)實(shí)現(xiàn)商品購買功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在上篇文章中 使用了mode1的模型來實(shí)現(xiàn)商品的瀏覽,這篇文章在上篇的基礎(chǔ)上,使用mvc架構(gòu)實(shí)現(xiàn)商品的購買。
運(yùn)行結(jié)果:

mvc架構(gòu)實(shí)現(xiàn)商品的購買(二)

相對與上篇文章  我們多了購物車類
由于我們在購買物品時(shí),購物車需要的屬性為購買的商品和數(shù)量   所以我們用map的鍵值來保存購買的商品。
當(dāng)然還有一個(gè)總價(jià)格,購物車的方法有 添加商品  刪除商品  計(jì)算總價(jià)格  其中總價(jià)格應(yīng)該在每次添加商品和刪除商品時(shí) 重新計(jì)算  購物車商品集合  只在初始化購物車的時(shí)候?qū)嵗淮渭纯?/p>

?
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
package entity;
 
import java.util.HashMap;
 
public class Cart {
  //購物車商品集合
  private HashMap<Items,Integer> cart;
  //總金額
  private double totalPrices;
  public Cart() {
    cart=new HashMap<Items, Integer>();
    totalPrices=0.0;
  }
  public HashMap<Items, Integer> getCart() {
    return cart;
  }
  public void setCart(HashMap<Items, Integer> cart) {
    this.cart = cart;
  }
  public double getTotalPrices() {
    return totalPrices;
  }
  public void setTotalPrices(double totalPrices) {
    this.totalPrices = totalPrices;
  }
  //添加物品進(jìn)購物車
  public boolean addToCart(Items item,int counts){
    //如果當(dāng)前物品 已經(jīng)添加過 只增加數(shù)量
    if(cart.containsKey(item)){
      cart.put(item, cart.get(item)+counts);
    }else{
      cart.put(item, counts);
    }
    //重新計(jì)算價(jià)格
    calTotalPrice(item.getPrice()*counts);
    return true;
  }
  //將物品從購物車刪除
  public boolean removeFromCart(Items item){
    if(cart!=null&&cart.containsKey(item)){
      calTotalPrice(-item.getPrice()*cart.get(item));
      cart.remove(item);
    }
    return true;
  }
  //計(jì)算總金額
  private void calTotalPrice(double price){
    totalPrices+=price;
  }
}

CartServlet的doGett方法根據(jù)action來進(jìn)行相應(yīng)的處理

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
if (request.getParameter("action") != null) {
      action = request.getParameter("action");
 
      if ("add".equals(action)) { // 添加商品
        if (addGoodsToCart(request, response)) {
          request.getRequestDispatcher("../success.jsp").forward(
              request, response);
        } else {
          request.getRequestDispatcher("../failure.jsp").forward(
              request, response);
        }
 
      } else if ("show".equals(action)) {// 顯示購物車
        request.getRequestDispatcher("../cart.jsp").forward(request,
            response);
      } else if ("delete".equals(action)) {// 刪除物品
        deleteGoodFromCart(request, response);
        request.getRequestDispatcher("../cart.jsp").forward(request,
            response);
      }
    }

當(dāng)在商品界面 我們點(diǎn)擊了放入購物車時(shí) 將當(dāng)前商品的編號傳到 購物車的servlet類CartServlet開始處理當(dāng)前物品 并將當(dāng)前物品放入購物車

再放入購物車之前 首先判斷是否是第一次創(chuàng)建購物車  (購物車肯定只有一個(gè)  不能有多個(gè))如果是第一次創(chuàng)建購物車cart
將當(dāng)前購物車放入session ,然后通過ItemsDao對象調(diào)用getItemById(id)方法 獲得商品對象  。然后將相應(yīng)的商品對象 和商品數(shù)量放入購物車

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//向購物車添加商品
  private boolean addGoodsToCart(HttpServletRequest request,
      HttpServletResponse response) {
    String id=request.getParameter("id");
    String counts=request.getParameter("num");
    Items item=dao.getItemById(Integer.parseInt(id));
    //判斷是否是第一次創(chuàng)建購物車
    if(request.getSession().getAttribute("cart")==null){
      Cart cart=new Cart();
      request.getSession().setAttribute("cart", cart);
      request.getSession().setAttribute("dao", dao);
    }
    Cart cart=(Cart) request.getSession().getAttribute("cart");
    //將商品添加到購物車
    if(cart.addToCart(item, Integer.parseInt(counts))){
      return true;
    }else{
      return false;
    }
  }

如果點(diǎn)擊了查看購物車   CartServlet重定向到購物車頁面

?
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
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%@ page import="entity.Cart" %>
<%@ page import="entity.Items" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <base href="<%=basePath%>">
  <title>My JSP 'cart.jsp' starting page</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">
  <!--
  <link rel="stylesheet" type="text/css" href="styles.css">
  -->
  <link type="text/css" rel="stylesheet" href="css/style1.css" />
  <script language="javascript">
    function delcfm() {
      if (!confirm("確認(rèn)要刪除?")) {
        window.event.returnValue = false;
      }
    }
  </script>
 </head>
  
 <body>
  <h1>我的購物車</h1>
  <a href="goods.jsp">首頁</a> >> <a href="goods.jsp">商品列表</a>
  <hr
  <div id="shopping">
  <form action="" method="">   
      <table>
        <tr>
          <th>商品名稱</th>
          <th>商品單價(jià)</th>
          <th>商品價(jià)格</th>
          <th>購買數(shù)量</th>
          <th>操作</th>
        </tr>
        <% 
          //首先判斷session中是否有購物車對象
          if(request.getSession().getAttribute("cart")!=null)
          {
        %>
        <!-- 循環(huán)的開始 -->
           <% 
             Cart cart = (Cart)request.getSession().getAttribute("cart");
             HashMap<Items,Integer> goods = cart.getCart();
             Set<Items> items = goods.keySet();
             Iterator<Items> it = items.iterator();
              
             while(it.hasNext())
             {
              Items i = it.next();
           %> 
        <tr name="products" id="product_id_1">
          <td class="thumb"><img src="images/<%=i.getPicture()%>" /><a href=""><%=i.getName()%></a></td>
          <td class="number"><%=i.getPrice() %></td>
          <td class="price" id="price_id_1">
            <span><%=i.getPrice()*goods.get(i) %></span>
            <input type="hidden" value="" />
          </td>
          <td class="number">
            <%=goods.get(i)%>         
          </td>            
          <td class="delete">
           <a href="servlet/CartServlet?action=delete&id=<%=i.getId()%>" onclick="delcfm();">刪除</a>                   
          </td>
        </tr>
           <% 
             }
           %>
        <!--循環(huán)的結(jié)束-->
         
      </table>
       <div class="total"><span id="total">總計(jì):<%=cart.getTotalPrices() %>¥</span></div>
       <% 
        }
       %>
      <div class="button"><input type="submit" value="" /></div>
    </form>
  </div>
 </body>
</html>

當(dāng)點(diǎn)擊了刪除商品 CartServlet類調(diào)用刪除商品的方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 從購物車刪除商品
  private boolean deleteGoodFromCart(HttpServletRequest request,
      HttpServletResponse response) {
    //從session中獲取購物車對象
    Cart cart = (Cart) request.getSession().getAttribute("cart");
    if (cart != null) {
      int id = Integer.parseInt(request.getParameter("id"));
      if (cart.removeFromCart(dao.getItemById(id))) {
        return true;
      }
    }
    return false;
 
  }

邏輯代碼主要如上。

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

原文鏈接:http://blog.csdn.net/su20145104009/article/details/53176702

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成年人免费观看 | 思久久 | 果冻传媒在线播放观看228集 | 成人免费福利网站在线看 | 久久最新地址获取 | 第一国内永久免费福利视频 | 久久亚洲精品成人 | 欧美在线看片a免费观看 | 国产精品色图 | 亚洲人成网站在线观看青青 | 男人爱看的网站 | 国产japanese孕妇孕交 | 精品高潮呻吟99AV无码 | 精品老司机在线视频香蕉 | 亚洲AV无码国产精品色在线看 | 四虎在线视频免费观看视频 | 99热这里只有精品免费 | 免费在线观看网址大全 | 国产农村一一级特黄毛片 | 日韩国产成人资源精品视频 | 日本黄a| 国产精品资源在线观看 | 9420高清完整版在线观看国语 | 99久久国产综合精品麻豆 | 色吊丝每日永久访问网站 | 欧美人体高清在线观看ggogo | 青草园网站在线观看 | 草综合| 国产卡一卡二卡四卡无卡 | 亚洲第一色网站 | 免费在线观看小视频 | 香蕉久久久久 | 狠狠撸在线播放 | 女仆色永久免费网站 | 久久99国产精品二区不卡 | 无人区免费一二三四乱码 | 国产一二在线观看视频网站 | 成人做视频免费 | 男女男在线精品网站免费观看 | 男男同志gaysxxx | 3d美女触手怪爆羞羞漫画 |