终极完美《简易商城购物车系统》揭秘

开发 项目管理
大家经常去网上够物,对网上所谓的购物车应该不会陌生吧,那么今天我们就用使用javaweb的MVC设计模式来实现一个网上购物系统的案例。

《简易商城购物系统》

大家经常去网上够物,对网上所谓的购物车应该不会陌生吧,那么今天我们就用使用javaweb的MVC设计模式来实现一个网上购物系统的案例。

最终效果如下:

image

三层架构的简单介绍

image

一、开发步骤

首先要搞清楚你要做什么,然后:

1.搭建开发环境

 jstl.jar

standard.jar
 

2.建立类包

3.建立数据库

使用内存数据库

总之,此购物车的实现,使用的是MVC设计模式,MVC设计模式的思路就是从jsp--javabean—servlet--jsp页面做显示

流程图:

图一:MVC设计模式的简介

image

图二:购物系统案例的实现思路:

image

图三:设计购物车页面

image

节日正式开始,精彩不容错过。。。。

#p#

1.写一个jsp购物页面

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3.     String path = request.getContextPath();  
  4.     String basePath = request.getScheme() + "://" 
  5.             + request.getServerName() + ":" + request.getServerPort()  
  6.             + path + "/";  
  7. %>  
  8.  
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  10. <html>  
  11.   <head>   
  12.       
  13.     <title>飘叶网上商城</title>  
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!--  
  20.     <link rel="stylesheet" type="text/css" href="styles.css">  
  21.     -->  
  22.   </head>  
  23.   <body>  
  24.   <hr/>  
  25.   <h2 >欢迎进入飘叶网上购物商城</h2>  
  26.   <hr>  
  27.        <a href="${pageContext.request.contextPath}/ListBookServlet">进入购物页面</a> <br>  
  28.   </body>  
  29. </html> 

2.写一个javabean

  1. package cn.itcast.cart.domain;  
  2. public class Book  
  3. {  
  4.     private String id;  
  5.     private String name;//书名  
  6.     private String author;//作者  
  7.     private int price;  
  8.       
  9.     public Book()  
  10.     {  
  11.         super();  
  12.         // TODO Auto-generated constructor stub  
  13.     }  
  14.     public Book(String id, String name, String author, int price)  
  15.     {  
  16.         super();  
  17.         this.id = id;  
  18.         this.name = name;  
  19.         this.author = author;  
  20.         this.price = price;  
  21.     }  
  22.     public String getId()  
  23.     {  
  24.         return id;  
  25.     }  
  26.     public void setId(String id)  
  27.     {  
  28.         this.id = id;  
  29.     }  
  30.     public String getName()  
  31.     {  
  32.         return name;  
  33.     }  
  34.     public void setName(String name)  
  35.     {  
  36.         this.name = name;  
  37.     }  
  38.     public String getAuthor()  
  39.     {  
  40.         return author;  
  41.     }  
  42.     public void setAuthor(String author)  
  43.     {  
  44.         this.author = author;  
  45.     }  
  46.     public int getPrice()  
  47.     {  
  48.         return price;  
  49.     }  
  50.     public void setPrice(int price)  
  51.     {  
  52.         this.price = price;  
  53.     }  
  54.       

3.建立DB,用Map集合来模拟数据库

  1. package cn.itcast.cart.domain;  
  2. import java.util.Collection;  
  3. import java.util.LinkedHashMap;  
  4. import java.util.Map;  
  5. public class DB  
  6. {  
  7. //    使用map集合来模拟数据库  
  8.     private static Map<String , Book> books=new LinkedHashMap<String, Book>();  
  9.     static{  
  10.         books.put("1", new Book("1""《水浒传》""施耐庵", 48));  
  11.         books.put("2", new Book("2""《西游记》""吴承恩 ", 58));  
  12.         books.put("3", new Book("3""《三国演义》""罗贯中", 78));  
  13.         books.put("4", new Book("4""《红楼梦》""曹雪芹", 28));  
  14.         books.put("5", new Book("5""《平凡的世界》""路遥", 18));  
  15.     }  
  16. //    获得所有图书  
  17.     // 获得所有图书  
  18.     public static Collection<Book> getAll() {  
  19.         return books.values();  
  20.     }  
  21.     // 根据id查找图书  
  22.     public static Book find(String id) {  
  23.         return books.get(id);  
  24.     }  

4.用javabean建立一个购物车对象

  1. package cn.itcast.cart.domain;  
  2. public class ShoppingcartItem  
  3. {  
  4.     //购物车项,每一本书买了多少本,总共多少钱  
  5.     private Book book;  
  6.     private int quantity;  
  7.     private int price;  
  8.     public Book getBook()  
  9.     {  
  10.         return book;  
  11.     }  
  12.     public void setBook(Book book)  
  13.     {  
  14.         this.book = book;  
  15.     }  
  16.     public int getQuantity()  
  17.     {  
  18.         return quantity;  
  19.     }  
  20.     public void setQuantity(int quantity)  
  21.     {  
  22.         this.quantity = quantity;  
  23.         this.price=this.book.getPrice()*quantity;  
  24.     }  
  25.     public int getPrice()  
  26.     {  
  27.         return price;  
  28.     }  
  29.     public void setPrice(int price)  
  30.     {  
  31.         this.price = price;  
  32.     }  

购物车—cart

  1. package cn.itcast.cart.domain;  
  2. import java.util.HashMap;  
  3. import java.util.Map;  
  4. //购物车对象  
  5. public class Shoppingcart  
  6. {  
  7.     private Map<String, ShoppingcartItem> items=new HashMap<String, ShoppingcartItem>();  
  8.     private int price;//总价  
  9.     public Map<String, ShoppingcartItem> getItems()  
  10.     {  
  11.         return items;  
  12.     }  
  13.     public void setItems(Map<String, ShoppingcartItem> items)  
  14.     {  
  15.         this.items = items;  
  16.     }  
  17.     public int getPrice()  
  18.     {  
  19.         //计算总价  
  20.         int price=0;  
  21.         for(ShoppingcartItem item:items.values())  
  22.         {  
  23.             price+=item.getPrice();  
  24.         }  
  25.         return price;  
  26.     }  
  27.     public void setPrice(int price)  
  28.     {  
  29.         this.price = price;  
  30.     }  

5、获得图书商品列表的servlet—ListBookServlet.java

  1. package cn.itcast.cart.web.servlet;  
  2. import java.io.IOException;  
  3. import java.util.Collection;  
  4. import javax.servlet.ServletException;  
  5. import javax.servlet.http.HttpServlet;  
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8. import cn.itcast.cart.domain.Book;  
  9. import cn.itcast.cart.domain.DB;  
  10.  
  11. public class ListBookServlet extends HttpServlet  
  12. {  
  13.     //从DB中查询所有的图书  
  14.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  15.             throws ServletException, IOException  
  16.     {  
  17.         //查询所有的商品  
  18.         Collection<Book> books = DB.getAll();  
  19. //        转发给jsp显示  
  20.         request.setAttribute("books", books);  
  21.         request.getRequestDispatcher("/WEB-INF/pages/listbook.jsp").forward(request, response);  
  22.     }  
  23.  
  24.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  25.             throws ServletException, IOException  
  26.     {  
  27.         doGet(request, response);  
  28.     }  
  29.  

6.servlet处理完的数据转发到一个展示商品的页面—listbook.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
  2. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  4. <html> 
  5. <head> 
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  7. <title>显示所有的商品</title> 
  8. <script type="text/javascript"> 
  9.     function buy(id){  
  10.         window.location = "${pageContext.request.contextPath}/BuyServlet?id="+id;  
  11.     }  
  12. </script> 
  13.  
  14. </head> 
  15. <body style="text-align: center"> 
  16.     <h1 >商品列表</h1> 
  17.     <table border="1"  width="400px"> 
  18.         <tr> 
  19.             <td>图书名称</td> 
  20.             <td>作者</td> 
  21.             <td>价格</td> 
  22.             <td>购买</td> 
  23.         </tr> 
  24.         <c:forEach var="book" items="${requestScope.books}"> 
  25.             <tr> 
  26.             <td>${book.name}</td> 
  27.             <td>${book.author}</td> 
  28.             <td>${book.price}</td> 
  29.             <td> 
  30.                 <input type="button" value="购买" onclick="buy(${book.id})"/> 
  31.             </td> 
  32.         </tr> 
  33.         </c:forEach> 
  34.     </table> 
  35. </body> 
  36. </html> 

7.写一个购买处理的servlet

  1. package cn.itcast.cart.web.servlet;  
  2. import java.io.IOException;  
  3. import java.util.Map;  
  4. import javax.servlet.ServletException;  
  5. import javax.servlet.http.HttpServlet;  
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8. import cn.itcast.cart.domain.Book;  
  9. import cn.itcast.cart.domain.DB;  
  10. import cn.itcast.cart.domain.Shoppingcart;  
  11. import cn.itcast.cart.domain.ShoppingcartItem;  
  12. public class BuyServlet extends HttpServlet  
  13. {  
  14.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  15.             throws ServletException, IOException  
  16.     {  
  17.         //购买页面  
  18.         //获得图书  
  19.         String id=request.getParameter("id");  
  20.         Book book=DB.find(id);  
  21. //        获得购物车  
  22.         Shoppingcart cart = (Shoppingcart) request.getSession().getAttribute("cart");  
  23.         if(cart==null){  
  24.             //首次购物  
  25.             cart=new Shoppingcart();  
  26.             request.getSession().setAttribute("cart", cart);  
  27.         }  
  28. //        商品放入购物车  
  29.         bookInCart(book,cart);  
  30. //        跳转至购物车页面,是一个请求重定向的页面  
  31.         response.sendRedirect(request.getContextPath()+"/ListCartServlet");  
  32.     }  
  33. //购物  
  34.     private void bookInCart(Book book, Shoppingcart cart)  
  35.     {//判断是否买过  
  36.         Map<String, ShoppingcartItem> items = cart.getItems();       
  37.         ShoppingcartItem item = items.get(book.getId());      
  38.         if(item==null){  
  39.             //此书未买过,创建新条目  
  40.             item=new ShoppingcartItem();  
  41.             item.setBook(book);  
  42.             item.setQuantity(1);  
  43.             //条目存入购物车  
  44.             items.put(book.getId(), item);  
  45.         }else{  
  46.             //买过数量加1  
  47.             item.setQuantity(item.getQuantity()+1);  
  48.         }  
  49.           
  50.     }  
  51.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  52.             throws ServletException, IOException  
  53.     {  
  54.         doGet(request, response);  
  55.     }  
  56.  

#p#

8.写一个获得购物车数据处理的servlet—ListCartServlet.java 

  1. package cn.itcast.cart.web.servlet;  
  2. import java.io.IOException;  
  3. import javax.servlet.ServletException;  
  4. import javax.servlet.http.HttpServlet;  
  5. import javax.servlet.http.HttpServletRequest;  
  6. import javax.servlet.http.HttpServletResponse;  
  7. public class ListCartServlet extends HttpServlet  
  8. {     
  9.     //查看购物车,请求重定向的页面  
  10.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  11.             throws ServletException, IOException  
  12.     {  
  13.         request.getRequestDispatcher("/WEB-INF/pages/listcart.jsp").forward(request, response);  
  14.       
  15.     }  
  16.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  17.             throws ServletException, IOException  
  18.     {  
  19.         doGet(request, response);  
  20.     }  

9.购买的东西放入购物车,继而转向购物车展示页面—listcart.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" 
  2.     pageEncoding="UTF-8"%> 
  3.     <%--    只要使用foreach就要导入下面的这句代码--%> 
  4. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  6. <html> 
  7. <head> 
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  9. <title>购物车页面</title> 
  10. <script type="text/javascript"> 
  11.     function change(id,inputObj){  
  12.         var quantity=inputObj.value;  
  13.  
  14.         if(quantity==null || quantity=="") {  
  15.             alert("数量不能为空");  
  16.             inputObj.value = defaultValue;  
  17.             return;  
  18.         }  
  19.           
  20.         if(quantity.match(/^[1-9][0-9]*$/)==null) {  
  21.             alert("数量必须为正整数");  
  22.             inputObj.value = defaultValue;  
  23.             return;  
  24.         }  
  25.           
  26.         if(parseInt(quantity)>999) {  
  27.             alert("您购买的数量已达到团购标准,请致电800-820-8820");  
  28.             inputObj.value = defaultValue;  
  29.             return;  
  30.         }  
  31.               window.location="${pageContext.request.contextPath}/UpdateServlet?id="+id+"&quantity="+quantity;  
  32.     }  
  33. </script> 
  34.  
  35. </head> 
  36. <body style="text-align: center"> 
  37. <h1>我的购物车</h1><br> 
  38. <hr> 
  39. <table border="1" width="800px"> 
  40.     <tr> 
  41.         <td>商品名称</td> 
  42.         <td>单价</td> 
  43.         <td>数量</td> 
  44.         <td>小计</td> 
  45.         <td>操作</td> 
  46.     </tr> 
  47. <%--map迭代完后都是entry--%> 
  48.     <c:forEach var="entry" items="${cart.items}"> 
  49.     <tr> 
  50.         <td>${entry.value.book.name}</td> 
  51.         <td>${entry.value.book.price}</td> 
  52.         <td> 
  53.             <input type="text" value="${entry.value.quantity}"  onblur="change(${entry.key},this) " style="width: 40px;"/> 
  54.         </td> 
  55.         <td>${entry.value.price}</td> 
  56.         <td> 
  57.             <a href="${pageContext.request.contextPath}/DaleServlet?id=${entry.key }">删除</a> 
  58.         </td> 
  59.     </tr>      
  60.     </c:forEach> 
  61.     <tr> 
  62.         <td> 
  63.             <a href="${pageContext.request.contextPath}/clearServlet">清空购物车</a> 
  64.         </td> 
  65.         <td> 
  66.             <a href="${pageContext.request.contextPath}/ListBookServlet">继续购物</a> 
  67.         </td> 
  68.         <td> 
  69.             <a href="#">下订单</a> 
  70.         </td> 
  71.         <td colspan="2">购物车总价:¥${cart.price}元</td> 
  72.     </tr> 
  73. </table> 
  74. </body> 
  75. </html> 

下面就是实现购物车里面的一些操作功能

10.更新购物车,就是修改完数量后,自动更新购物车—UpdateServlet.java

  1. package cn.itcast.cart.web.servlet;  
  2. import java.io.IOException;  
  3. import java.util.Map;  
  4. import javax.servlet.ServletException;  
  5. import javax.servlet.http.HttpServlet;  
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8. import cn.itcast.cart.domain.Shoppingcart;  
  9. import cn.itcast.cart.domain.ShoppingcartItem;  
  10. public class UpdateServlet extends HttpServlet  
  11. {  
  12.     //更新购物车  
  13.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  14.             throws ServletException, IOException  
  15.     {  
  16.         //获得id和quantity  
  17.         String id=request.getParameter("id");  
  18.         int quantity=Integer.parseInt(request.getParameter("quantity"));  
  19.         //获得购物车  
  20.         Shoppingcart cart = (Shoppingcart) request.getSession().getAttribute("cart");  
  21.         // 修改数量  
  22.         /*  
  23.         Map<String, ShoppingcartItem> items = cart.getItems();  
  24.           
  25.         ShoppingcartItem item = items.get(id);  
  26.           
  27.         item.setQuantity(quantity);*/ 
  28.           
  29.         cart.getItems().get(id).setQuantity(quantity);  
  30.           
  31.         // 跳转至购物车页面  
  32.         response.sendRedirect(request.getContextPath() + "/ListCartServlet");  
  33.     }  
  34.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  35.             throws ServletException, IOException  
  36.     {  
  37.         doGet(request, response);  
  38.     }  
  39.  

11.删除购物车中的单行数据—DaleServlet.java

  1. package cn.itcast.cart.web.servlet;  
  2. import java.io.IOException;  
  3. import javax.servlet.ServletException;  
  4. import javax.servlet.http.HttpServlet;  
  5. import javax.servlet.http.HttpServletRequest;  
  6. import javax.servlet.http.HttpServletResponse;  
  7. import cn.itcast.cart.domain.Shoppingcart;  
  8. public class DaleServlet extends HttpServlet  
  9. {  
  10.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  11.             throws ServletException, IOException  
  12.     {  
  13.         // 获得id  
  14.         String id = request.getParameter("id");       
  15.         // 获得购物车  
  16.         Shoppingcart cart = (Shoppingcart) request.getSession().getAttribute("cart");          
  17.         //删除条目  
  18.         cart.getItems().remove(id);  
  19.  
  20.         // 跳转至购物车页面  
  21.         response.sendRedirect(request.getContextPath()+"/ListCartServlet");  
  22.     }  
  23.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  24.             throws ServletException, IOException  
  25.     {  
  26.         doGet(request, response);  
  27.     }  
  28.  

12.清空购物车—clearServlet.java

  1. package cn.itcast.cart.web.servlet;  
  2. import java.io.IOException;  
  3. import javax.servlet.ServletException;  
  4. import javax.servlet.http.HttpServlet;  
  5. import javax.servlet.http.HttpServletRequest;  
  6. import javax.servlet.http.HttpServletResponse;  
  7. import cn.itcast.cart.domain.Shoppingcart;  
  8. public class clearServlet extends HttpServlet  
  9. {  
  10.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  11.             throws ServletException, IOException  
  12.     {  
  13.         // 获得购物车  
  14.         Shoppingcart cart = (Shoppingcart) request.getSession().getAttribute("cart");  
  15.           
  16.         // 清空购物车  
  17.         cart.getItems().clear();  
  18.           
  19.         // 跳转至购买页面  
  20.         response.sendRedirect(request.getContextPath()+"/ListBookServlet");  
  21.     }  
  22.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  23.             throws ServletException, IOException  
  24.     {  
  25.         doGet(request, response);  
  26.     }  
  27.  

ok,忙碌到半夜,到此结束,下面看看我们的最终效果吧:

image

image

嘿嘿,没有美工处理的页面,还行,像那回事吧?

这上面的所有功能都可以实现的哦!

image

 

【编辑推荐】

  1. Java新浪微博客户端开发
  2. 跟李刚一起畅谈Java编程人生
  3. 8种常见的Java不规范代码
  4. 调研专题:Java语言的回顾与展望
  5. Java的封面:关于程序员的嗅觉

 

责任编辑:张伟 来源: 飘叶寻梦的博客
相关推荐

2022-12-16 08:52:14

购物车系统存储

2015-08-03 11:48:12

购物车动画

2022-09-13 16:01:13

购物车京东接口

2011-04-14 10:08:04

AJAXPHPJQuery

2022-06-28 14:42:26

ETS购物车应用

2013-12-11 11:26:24

移动互联网

2009-07-07 15:57:29

JSP购物车

2021-02-01 09:57:29

鸿蒙HarmonyOS应用

2018-05-28 09:53:12

京东购物车Java

2023-11-08 08:01:40

Spring购物车代码

2018-05-17 16:45:29

Java购物车京东

2009-07-28 13:47:47

ASP.NET电子商务ASP.NET购物车

2017-11-06 09:10:56

程序员数据行业

2021-05-08 08:01:05

Session登录浏览器

2013-09-13 13:25:16

html5拖拽

2021-10-20 06:00:34

淘宝微信朋友圈购物车

2017-11-10 13:21:45

华为

2021-09-17 14:10:27

区块链购物技术

2024-03-11 10:52:34

2009-07-02 16:14:28

JSP Session购物车程序
点赞
收藏

51CTO技术栈公众号