由于我们实现的方法是使用的session所以我们这里只需要 编写每一行的表单信息,还有总体的购物车信息:
用来保存每一行表单信息的类:
package cn.zsp.pojo;
import java.math.BigDecimal;
/**
*购物车的商品项
*/
public class CartItem {
// 购物车CartItem
// 商品编号
// id
private Integer id;
// 商品名称
// name
private String name;
// 商品数量
// count
private Integer count;
// 商品单价
// price
private BigDecimal price;
// 商品总价
// totalPrice
private BigDecimal totalPrice;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public BigDecimal getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(BigDecimal totalPrice) {
this.totalPrice = totalPrice;
}
public CartItem() {
}
public CartItem(Integer id, String name, Integer count, BigDecimal price, BigDecimal totalPrice) {
this.id = id;
this.name = name;
this.count = count;
this.price = price;
this.totalPrice = totalPrice;
}
@Override
public String toString() {
return "CartItem{" +
"id=" + id +
", name='" + name + '\'' +
", count=" + count +
", price=" + price +
", totalPrice=" + totalPrice +
'}';
}
}
用来保存总体信息的类:
package cn.zsp.pojo;
import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.Map;
public class Cart {
// 购物车对象Cart
// 总商品数量
// totalCount
// private Integer totalCount;//这里直接用了get方法获取
//
// 总商品价格
// totalPrice
// private BigDecimal totalPrice;//这里直接用了get方法获取
// 购物车商品
// items
private Map<Integer,CartItem> items=new LinkedHashMap<Integer,CartItem>();
// addltem (Cartltem) ;
// 添加商品项
public void addItem(CartItem cartItem){
CartItem item =items.get(cartItem.getId());
if (item==null)
{
items.put(cartItem.getId(),cartItem);
}
else
{
item.setCount(item.getCount()+1);//数量累加
item.setTotalPrice(item.getTotalPrice().multiply(new BigDecimal(item.getCount())));
}
}
// deleteItem(id) ;
// 删除商品项
public void deleteItem(Integer id ){
items.remove(id);
}
// clear(){}
// 清空购物车
public void clear(){
items.clear();
}
// updateCount(id,count)
// 修改商品数量
public void updateCount(Integer id,Integer count){
// 先查看购物车中是否有此商品。如果有,修改商品数量,更新总金额
CartItem cartItem = items.get(id);
if (cartItem!=null){
// 耘查看购物车中是否有此商品。如果有,修改商品数量,更新总金额
cartItem.setCount(count);//修改商品数量
cartItem.setTotalPrice(cartItem.getTotalPrice().multiply(new BigDecimal(cartItem.getCount())));
}
}
public Cart() {
}
public Integer getTotalCount() {
Integer totalCount=0;
for (CartItem value : items.values()) {
totalCount+=value.getCount();
}
return totalCount;
}
public BigDecimal getTotalPrice() {
BigDecimal totalPrice=new BigDecimal(0);
for (CartItem value : items.values()) {
totalPrice=totalPrice.add(value.getTotalPrice());
}
return totalPrice;
}
public Map<Integer, CartItem> getItems() {
return items;
}
public void setItems(Map<Integer, CartItem> items) {
this.items = items;
}
@Override
public String toString() {
return "Cart{" +
"totalCount=" + getTotalCount() +
", totalPrice=" + getTotalPrice() +
", items=" + items +
'}';
}
}
调用的servlet方法:
package cn.zsp.web;
import cn.zsp.pojo.Book;
import cn.zsp.pojo.Cart;
import cn.zsp.pojo.CartItem;
import cn.zsp.service.BookService;
import cn.zsp.service.impl.BookServiceImpl;
import cn.zsp.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{
private BookService bookService=new BookServiceImpl();
protected void clearItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cart cart=(Cart) req.getSession().getAttribute("cart");
if (cart!=null)
{
cart.clear();
}
// 返回原本的网址
resp.sendRedirect(req.getHeader("Referer"));
}
/**
* 加入删除购物车的表单
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取商品编号
int id =WebUtils.parseInt(req.getParameter("id"),0);
// 从域中获取购物车对象
Cart cart=(Cart) req.getSession().getAttribute("cart");
if (cart!=null)
{
cart.deleteItem(id);
}
// 返回原本的网址
resp.sendRedirect(req.getHeader("Referer"));
}
/**
* 加入购物车
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取请求的参数商品编号
int id = WebUtils.parseInt(req.getParameter("id"),0);
// 调用bookService.queryBookById(id): Book得到图书的信息
Book book = bookService.queryBookById(id);
// 把图书信息,转换成为CartItem商品项
CartItem cartItem=new CartItem(book.getId(),book.getName(),1,book.getPrice(),book.getPrice());
// 调用cart.addItem( cartItem);添加商品项
Cart cart = (Cart) req.getSession().getAttribute("cart");
if (cart==null)
{
cart =new Cart();
req.getSession().setAttribute("cart",cart);
cart.addItem(cartItem);
}
cart.addItem(cartItem);
// 重定向回商品列表页面
resp.sendRedirect(req.getHeader("Referer"));
}
}
实现的jsp页面:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>购物车</title>
<%@include file="/pages/common/head.jsp"%>
<script type="text/javascript">
$(function () {
$("a.deleteItem").click(function () {
return confirm("你确定要删除【"+$(this).parent().parent().find("td:first").text()+"】吗");
});
$("#clearItem").click(function () {
return confirm("你确定要清空购物车吗?");
});
});
</script>
</head>
<body>
<div id="header">
<img class="logo_img" alt="" src="static/img/logo.gif" >
<span class="wel_word">购物车</span>
<%@include file="/pages/common/login_sucess_menu.jsp"%>
</div>
<div id="main">
<table>
<tr>
<td>商品名称</td>
<td>数量</td>
<td>单价</td>
<td>金额</td>
<td>操作</td>
</tr>
<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="book">
<tr>
<td>${book.value.name}</td>
<td>${book.value.count}</td>
<td>${book.value.price}</td>
<td>${book.value.totalPrice}</td>
<td><a class="deleteItem" href="cartServlet?action=deleteItem&id=${book.value.id}">删除</a></td>
</tr>
</c:forEach>
</c:if>
</table>
<%-- 如果购物车非空才输出页面的内牡--%>
<c:if test="${not empty sessionScope.cart.items }">
<div class="cart_info">
<span class="cart_span">购物车中共有<span class="b_count">${sessionScope.cart.totalCount} </span>件商品</span>
<span class="cart_span">总金额<span class="b_price"> ${sessionScope.cart.totalPrice}</span>元</span>
<span class="cart_span"><a id="clearItem" href="cartServlet?action=clearItem">清空购物车</a></span>
<span class="cart_span"><a href="pages/cart/checkout.jsp">去结账</a></span>
</div>
</c:if>
</div>
<div id="bottom">
<%@include file="/pages/common/footer.jsp"%></div>
</body>
</html>