思路分析:
public class CartItem {private Integer id;private String name;private Integer count;private BigDecimal price;private BigDecimal totalPrice;}
package com.aiguigu.pojo;import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.Map;public class Cart {/*** key 是商品编号* value 是商品信息*/private Map<Integer,CartItem> items = new LinkedHashMap<Integer,CartItem>();/*** 添加商品*/public void addItem(CartItem cartItem){//先查看购物车是否已经添加过此商品,如果已经添加,则数量累加,总金额更新,如果没有添加过,直接放到集合中CartItem item = (Id());if (item == null){//之前没添加过此商品items.Id(),cartItem);}else {//已经添加过的情况item.Count() + 1);//数量累加item.Price().multiply(new Count())));//更新总金额}}/*** 删除商品项*/public void deleteItem(Integer id){ve(id);}/*** 清空购物车*/public void claer(){items.clear();}/*** 修改商品*/public void updateCount(Integer id,Integer count){CartItem cartItem = (id);if (cartItem != null){cartItem.setCount(count);//修改数量cartItem.Price().multiply(new Count())));//更新总金额}}public Integer getTotalCount() {Integer totalCount = 0;for (Map.Entry<Integer,CartItem>entry : Set()){totalCount += Value().getCount();}return totalCount;}public BigDecimal getTotalPrice() {BigDecimal totalPrice = new BigDecimal(0);for (Map.Entry<Integer,CartItem>entry : Set()){totalPrice = totalPrice.Value().getTotalPrice());}return totalPrice;}
}
获取重商品数量,和总商品价格 用 getTotalCount()、getTotalPrice()方法获取,参数 totalCount、totalPrice 直接在方法里面定义。用Map集合遍历
package com.aiguigu.web;import com.aiguigu.pojo.Book;
import com.aiguigu.pojo.Cart;
import com.aiguigu.pojo.CartItem;
import com.aiguigu.service.BookService;
import com.aiguigu.service.impl.BookServiceImpl;
import com.aiguigu.utils.WebUtils;import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;public class CartServlet extends BaseServlet {}
一:给加入购物车添加 class 并 通过 class 绑定点击事件,跳转到 CartServlet
<div class="book_add"><button bookId="${book.id}" class="addToCart">加入购物车</button></div><script type="text/javascript">$(function () {$("button.addToCart").click(function () {var bookId = $(this).attr("bookId");location.href = "localhost:8080/book/cartServlet?action=addItem&id=" + bookId;});});</script>
二:CartServlet页面 编写 addItem 方法
public class CartServlet extends BaseServlet {private BookService bookService = new BookServiceImpl();protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// System.out.println(id);// 获取请求的参数 商品编号int id = WebUtils.Parameter("id"), 0);//调用 bookService.queryBookById(id) Book得到图书信息Book book = bookService.queryBookById(id);//调用 Caet.addItem(CaerItem);添加商品项CartItem cartItem = new Id(),Name(),Price(),Price());//Cart cart = (Cart) Session().getAttribute("cart");if (cart == null){cart = new Cart();Session().setAttribute("cart",cart);}cart.addItem(cartItem);//将最后一个商品名字放入 session域中Session().setAttribute("lastName",Name());System.out.println("请求头Referer的值" + Header("Referer"));//重定向回原来商品所在地址页面resp.Header("Referer") );}
}
运行结果:
动态输出 图书内容:
购物车为空时:
<c:if test="${ empty sessionScope.cart.items}"><tr><td colspan="5"><a href="index.jsp">亲购物车为空快去浏览商品</a> </td><</tr></c:if>
购物车不为空时:
<c:if test="${not empty sessionScope.cart.items}"><c:forEach items="${sessionScope.cart.items}" var="items"><tr><td>${items.value.name}</td><td><input class="updateCount" bookId="${items.value.id}" style="width: 80px;" type="text" value="${unt}"></td><td>${items.value.price}</td><td>${alPrice}</td><td><a class="deleteItem" href="cartServlet?action=deleteItem&id=${items.value.id}">删除</a></td></tr></c:forEach></c:if>
当购物车为空时就不现实以上内容
<c:if test="${not empty sessionScope.cart.items}"><div class="cart_info"><span class="cart_span">购物车中共有<span class="b_count">${alCount}</span>件商品</span><span class="cart_span">总金额<span class="b_price">${alPrice}</span>元</span><span class="cart_span"><a id="clearCart" href="cartServlet?action=clear">清空购物车</a></span><span class="cart_span"><a href="pages/cart/checkout.jsp">去结账</a></span></div></c:if>
<td><a class="deleteItem" href="cartServlet?action=deleteItem&id=${items.value.id}">删除</a></td>
给删除 绑定click事件
$(function () {// 给 【删除】绑定单击事件$("a.deleteItem").click(function () {return confirm("确定要删除【"+ $(this).parent().parent().find("td:first").text() +"】吗")});
在 CartServlet 编写删除方法
protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//获取请求的参数,商品编号int id = WebUtils.Parameter("id"), 0);//获取购物车对象Cart cart = (Cart) Session().getAttribute("cart");if (cart != null){//删除商品cart.deleteItem(id);//重定向回页面resp.Header("referer"));}}
运行结果:
分析:
在 cart.jsp 页面,将数量用输入框替代,修改商品数量需要获取商品的id 和 count
<td><input class="updateCount" bookId="${items.value.id}" style="width: 80px;" type="text" value="${unt}"></td>
给 输入框绑定 change 事件:
//给修改【商品数量】 绑定 onchange事件$(".updateCount").change(function () {//获取商品名称var name = $(this).parent().parent().find("td:first").text();//获取商品数量var count = this.value;//获取商品idvar id = $(this).attr("bookId");if (confirm("确认要修改["+ name +"],数量为[" + count +"]吗")){location.href = "localhost:8080/book/cartServlet?action=updateCount&count=" + count + "&id=" + id;}else {//defaultValue 属性是表单项Dom 对象的属性。它表示默认的value属性值this.value = this.defaultValue;}});
在CartServlet 页面 编写 updateCount
protected void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//获取请求的参数 商品编号,和数量int id = WebUtils.Parameter("id"), 0);int count = WebUtils.Parameter("count"), 1);//获取 购物车对象Cart cart = (Cart) Session().getAttribute("cart");if (cart != null){cart.updateCount(id,count);resp.Header("referer"));}}
在 Cart.jsp 页面修改 跳转页面和调用的方法
<span class="cart_span"><a id="clearCart" href="cartServlet?action=clear">清空购物车</a></span>
在 CartServlet层 编写 clear 方法
protected void clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//获取 购物车对象Cart cart = (Cart) Session().getAttribute("cart");if (cart != null){cart.claer();resp.Header("referer"));}}
当购物车为空时,提示用户去购物
当购物不为空时,显示图书信息
<%-- 如果购物车非空的情况--%><c:if test="${not empty sessionScope.cart.items}"><c:forEach items="${sessionScope.cart.items}" var="items"><tr><td>${items.value.name}</td><td><input class="updateCount" bookId="${items.value.id}" style="width: 80px;" type="text" value="${unt}"></td><td>${items.value.price}</td><td>${alPrice}</td><td><a class="deleteItem" href="cartServlet?action=deleteItem&id=${items.value.id}">删除</a></td></tr></c:forEach></c:if><%-- 如果购物车为空的情况--%><c:if test="${ empty sessionScope.cart.items}"><tr><td colspan="5"><a href="index.jsp">亲购物车为空快去浏览商品</a> </td><</tr></c:if>
分两种情况:
<%--购物车不会空的情况--%><c:if test="${not empty sessionScope.cart.items}"><span>您的购物车中有${alCount}件商品</span><div>您刚刚将<span style="color: red">${sessionScope.lastName}</span>加入到了购物车中</div></c:if>
<c:if test="${empty sessionScope.cart.items}"><span></span><div><span style="color: red">购物车为空</span></div></c:if>
本文发布于:2024-02-03 03:46:28,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170690318648441.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |