图书商城:购物车模块
购物车存储
- 保存在session中
- 保存在cookie中
- 保存在数据库中
1、创建相关类
购物车的结构:
- CartItem:购物车条目,包含图书和数量
- Cart:购物车,包含一个Map
/*** 购物车类*/
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());//设置老条目的数量为,其自己数量+新条目的数量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();}
}
/*** 购物车条目类* */
public class CartItem {private Book book;// 商品private int count;// 数量/*** 小计方法* @return* 处理了二进制运算误差问题*/public double getSubtotal() {//小计方法,但它没有对应的成员!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、添加购物车条目
<%@ 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><%-- 如果没有车,或车的内容集合为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>数量</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>
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. 得到条目(得到图书和数量)* 3. 把条目添加到车中*//** 1. 得到车*/Cart cart = (Cart)request.getSession().getAttribute("cart");/** 表单传递的只有bid和数量* 2. 得到条目* > 得到图书和数量* > 先得到图书的bid,然后我们需要通过bid查询数据库得到Book* > 数量表单中有*/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. 设置车的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、清空条目
4、删除购物车条目
5、我的购物车
top.jsp中存在一个链接:我的购物车
我的购物车直接访问/jsps/cart/list.jsp,它会显示session中车的所有条目
图书商城:购物车模块相关推荐
- web day25 web day24 小项目练习图书商城, 购物车模块,订单模块,支付(易宝支付)
购物车模块 购物车存储: 保存在session中(本次使用的) 保存在cookie中 保存在数据库中 购物车相关类 购物车结构 CartItem:包含图书和数量,小计 Cart:包含一个Map< ...
- Mvp快速搭建商城购物车模块
代码地址如下: http://www.demodashi.com/demo/12834.html 前言: 说到MVP的时候其实大家都不陌生,但是涉及到实际项目中使用,还是有些无从下手.因此这里小编带着 ...
- jQuery实现PC端商城购物车模块基本功能(每个商品的小计和合计都会根据添加和删除的操作来动态计算)
jQuery实现PC端商城购物车模块基本功能 先上效果图: 因为主要是想练习jQuery的使用,所以页面CSS部分比较简陋,有需要的话,大家在参考代码时,可以自己再完善下CSS部分的代码,让购物车页面 ...
- 淘淘商城---购物车模块
一:模仿JD,在用户不登录的情况下,可以实现添加商品到购物车内.---将商品存放到cookie中 此处是商品详情页面-----点击加入购物车按钮的连接:端口:8090 参数:商品id . 商品数 ...
- mmall商城购物车模块总结
购物车模块的设计思想 购物车的实现方式有很多,但是最常见的就三种:Cookie,Session,数据库.三种方法各有优劣,适合的场景各不相同.Cookie方法:通过把购物车中的商品数据写入Cookie ...
- Vue node.js商城-购物车模块
一.渲染购物车列表页面 新建src/views/Cart.vue 获取cartList购物车列表数据就可以在页面中渲染出该用户的购物车列表数据 data(){ return { ca ...
- springboot网上图书商城
085-springboot网上图书商城演示录像2022 开发语言:Java 框架:springboot JDK版本:JDK1.8 服务器:tomcat7 数据库:mysql 5.7 数据库工具:Na ...
- 【3D商城】使用Vuex状态管理完成购物车模块
[3D商城]使用Vuex状态管理完成购物车模块 创建购物车的全局数据 添加产品到购物车 导航栏的购物车模块 结果 常见问题总结 创建购物车的全局数据 在store的index.js中 ,创建购物车变量 ...
- 功能测试用例编写2(商城注册登录及购物车模块)
版本管理工具GIT,集成工具Jenkins,抓包工具fiddler,Charles,接口测试工具jmeter,postman 功能测试用例编写2 用例八大要素: 1.用例编号:区分用例唯一标识符,格式 ...
- 谷粒商城12——购物车模块、消息队列RabbitMQ
文章目录 十.购物车模块 1.需求分析 2.封装vo 3.添加商品 4.查询购物车 5.选中商品 6.在购物车修改商品数量 7.在购物车删除商品 十一.消息队列RabbitMQ 1.场景分析 2.概述 ...
最新文章
- 3种团队分组适应项目_暴利生意:3种适合农村夫妻创业致富的项目,年赚10多万...
- 只有2GB内存在20亿个整数中找到出现次数最多的数
- 十万腾讯人,自救1000天
- 媒体应用大数据,先解决三大难题
- Docker4Dev #6 使用 Windows Container 运行.net应用
- w7系统计算机e盘无法打开,Win7电脑磁盘打不开怎么办
- 苹果发布会日期再曝光 2019新iPhone发布会定在这一天?
- 完全弄懂如何用pycharm安装pyqt5及其相关配置
- Python线性代数扩展库numpy.linalg中几个常用函数
- docker中如何制作自己的基础镜像
- Pandas——处理丢失的数据(含NaN的数据)
- 利用SusuCMS快速创建网站(一)
- 计算机程序领域专利撰写,干货 | 计算机软件专利撰写模板
- 尚硅谷大数据技术之 DataX—1)概述
- WIN10-x86虚拟机镜像-32位-VMware(亲测可用)
- 前端生成PDF,让后端刮目相看
- 萤石云设备下线是什么导致的_萤石设备突然看不了,提示不在线怎么办?
- 推荐交互设计师阅读的一本书
- 使用ArrayList集合,对其添加10个不同的元素,并使用Iterator遍历该集合
- gis统计百分比_详细讲解ArcGIS数据统计及字段计算