欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2016年11月16日 11:04:21   作者:scx_white  
這篇文章主要為大家詳細(xì)介紹了mvc架構(gòu)實(shí)現(xiàn)商品購(gòu)買(mǎi)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

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

package entity; 
 
import java.util.HashMap; 
 
public class Cart { 
  //購(gòu)物車(chē)商品集合 
  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)購(gòu)物車(chē) 
  public boolean addToCart(Items item,int counts){ 
    //如果當(dāng)前物品 已經(jīng)添加過(guò) 只增加數(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; 
  } 
  //將物品從購(gòu)物車(chē)刪除 
  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來(lái)進(jìn)行相應(yīng)的處理

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)) {// 顯示購(gòu)物車(chē) 
        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)擊了放入購(gòu)物車(chē)時(shí) 將當(dāng)前商品的編號(hào)傳到 購(gòu)物車(chē)的servlet類CartServlet開(kāi)始處理當(dāng)前物品 并將當(dāng)前物品放入購(gòu)物車(chē)

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

//向購(gòu)物車(chē)添加商品 
  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)建購(gòu)物車(chē) 
    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"); 
    //將商品添加到購(gòu)物車(chē) 
    if(cart.addToCart(item, Integer.parseInt(counts))){ 
      return true; 
    }else{ 
      return false; 
    } 
  } 

如果點(diǎn)擊了查看購(gòu)物車(chē)   CartServlet重定向到購(gòu)物車(chē)頁(yè)面

<%@ 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)要?jiǎng)h除?")) { 
        window.event.returnValue = false; 
      } 
    } 
  </script> 
 </head> 
  
 <body> 
  <h1>我的購(gòu)物車(chē)</h1> 
  <a href="goods.jsp">首頁(yè)</a> >> <a href="goods.jsp">商品列表</a> 
  <hr>  
  <div id="shopping"> 
  <form action="" method="">    
      <table> 
        <tr> 
          <th>商品名稱</th> 
          <th>商品單價(jià)</th> 
          <th>商品價(jià)格</th> 
          <th>購(gòu)買(mǎi)數(shù)量</th> 
          <th>操作</th> 
        </tr> 
        <%  
          //首先判斷session中是否有購(gòu)物車(chē)對(duì)象 
          if(request.getSession().getAttribute("cart")!=null) 
          { 
        %> 
        <!-- 循環(huán)的開(kāi)始 --> 
           <%  
             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)用刪除商品的方法

// 從購(gòu)物車(chē)刪除商品 
  private boolean deleteGoodFromCart(HttpServletRequest request, 
      HttpServletResponse response) { 
    //從session中獲取購(gòu)物車(chē)對(duì)象 
    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; 
 
  } 

邏輯代碼主要如上。

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

  • 詳解SpringBoot中自定義starter的開(kāi)發(fā)與使用

    詳解SpringBoot中自定義starter的開(kāi)發(fā)與使用

    starter是SpringBoot中非常重要的一個(gè)機(jī)制,他是基于約定優(yōu)于配置的思想所衍生出來(lái)的,本文主要介紹了SpringBoot中自定義starter的開(kāi)發(fā)與使用,感興趣的可以了解下
    2023-09-09
  • Java中泛型的使用和優(yōu)點(diǎn)解析

    Java中泛型的使用和優(yōu)點(diǎn)解析

    這篇文章主要介紹了Java中泛型的使用和優(yōu)點(diǎn)解析,泛型使用過(guò)程中,操作的數(shù)據(jù)類型被指定為一個(gè)參數(shù),這種參數(shù)類型可以用在類、接口和方法中,分別被稱為泛型類、泛型接口、泛型方法,需要的朋友可以參考下
    2023-09-09
  • Spring中BeanFactory和ApplicationContext的作用和區(qū)別(推薦)

    Spring中BeanFactory和ApplicationContext的作用和區(qū)別(推薦)

    這篇文章主要介紹了Spring中BeanFactory和ApplicationContext的作用和區(qū)別,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Java之?dāng)?shù)組在指定位置插入元素實(shí)現(xiàn)

    Java之?dāng)?shù)組在指定位置插入元素實(shí)現(xiàn)

    本文主要介紹了Java之?dāng)?shù)組在指定位置插入元素實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • Spring Cloud Feign實(shí)現(xiàn)文件上傳下載的示例代碼

    Spring Cloud Feign實(shí)現(xiàn)文件上傳下載的示例代碼

    Feign框架對(duì)于文件上傳消息體格式并沒(méi)有做原生支持,需要集成模塊feign-form來(lái)實(shí)現(xiàn),本文就詳細(xì)的介紹一下如何使用,感興趣的可以了解一下
    2022-02-02
  • Spring MVC溫故而知新系列教程之請(qǐng)求映射RequestMapping注解

    Spring MVC溫故而知新系列教程之請(qǐng)求映射RequestMapping注解

    這篇文章主要介紹了Spring MVC溫故而知新系列教程之請(qǐng)求映射RequestMapping注解的相關(guān)知識(shí),文中給大家介紹了RequestMapping注解提供的幾個(gè)屬性及注解說(shuō)明,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧
    2018-05-05
  • SpringBoot中的RabbitMQ用法詳解

    SpringBoot中的RabbitMQ用法詳解

    RabbitMQ是一個(gè)開(kāi)源的消息隊(duì)列系統(tǒng),它通過(guò)AMQP(高級(jí)消息隊(duì)列協(xié)議)來(lái)實(shí)現(xiàn)消息的傳遞,SpringBoot是目前非常流行的Java開(kāi)發(fā)框架,它提供了很多便利性的功能,其中就包括對(duì)RabbitMQ的支持,在本文中,我們將介紹如何在SpringBoot中使用RabbitMQ
    2023-07-07
  • java中 IO 常用IO操作類繼承結(jié)構(gòu)分析

    java中 IO 常用IO操作類繼承結(jié)構(gòu)分析

    本篇文章小編為大家介紹,java中 IO 常用IO操作類繼承結(jié)構(gòu)分析。需要的朋友參考下
    2013-04-04
  • SpringBoot使用Graylog日志收集的實(shí)現(xiàn)示例

    SpringBoot使用Graylog日志收集的實(shí)現(xiàn)示例

    Graylog是一個(gè)生產(chǎn)級(jí)別的日志收集系統(tǒng),集成Mongo和Elasticsearch進(jìn)行日志收集,這篇文章主要介紹了SpringBoot使用Graylog日志收集的實(shí)現(xiàn)示例,感興趣的小伙伴們可以參考一下
    2019-04-04
  • 最新評(píng)論