|
|
|
@ -1,12 +1,20 @@
|
|
|
|
|
// 导入所需的包和类
|
|
|
|
|
package com.yj.web;
|
|
|
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
// 用于将Java对象转换为JSON字符串
|
|
|
|
|
import com.yj.bean.Book;
|
|
|
|
|
// 书籍实体类
|
|
|
|
|
import com.yj.bean.Cart;
|
|
|
|
|
// 购物车实体类
|
|
|
|
|
import com.yj.bean.CartItem;
|
|
|
|
|
// 购物车商品项实体类
|
|
|
|
|
import com.yj.service.BookService;
|
|
|
|
|
// 书籍服务接口
|
|
|
|
|
import com.yj.service.impl.BookServiceImpl;
|
|
|
|
|
// 书籍服务接口的实现类
|
|
|
|
|
import com.yj.utils.WebUtils;
|
|
|
|
|
// 工具类,用于处理Web请求中的常见操作
|
|
|
|
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
@ -15,100 +23,93 @@ import java.io.IOException;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author yj
|
|
|
|
|
* @create 2020-08-27 9:47
|
|
|
|
|
*/
|
|
|
|
|
// 定义一个Servlet类,用于处理与购物车相关的请求
|
|
|
|
|
public class CartServlet extends BaseServlet {
|
|
|
|
|
|
|
|
|
|
// 创建一个书籍服务实例,用于访问书籍数据
|
|
|
|
|
private BookService bookService = new BookServiceImpl();
|
|
|
|
|
|
|
|
|
|
// 处理通过AJAX方式添加商品到购物车的请求
|
|
|
|
|
protected void ajaxAddItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
|
|
int id = WebUtils.parseInt(req.getParameter("id"),0);
|
|
|
|
|
// 从请求中获取书籍ID,并转换为整数类型,默认为0
|
|
|
|
|
int id = WebUtils.parseInt(req.getParameter("id"), 0);
|
|
|
|
|
// 根据书籍ID查询书籍信息
|
|
|
|
|
Book book = bookService.queryBookById(id);
|
|
|
|
|
CartItem cartItem = new CartItem(book.getId(),book.getName(),1,book.getPrice(),book.getPrice());
|
|
|
|
|
// 创建一个购物车商品项实例,数量为1,单价和总价均为书籍价格
|
|
|
|
|
CartItem cartItem = new CartItem(book.getId(), book.getName(), 1, book.getPrice(), book.getPrice());
|
|
|
|
|
// 从会话中获取购物车对象,如果不存在则创建一个新的购物车对象
|
|
|
|
|
Cart cart = (Cart) req.getSession().getAttribute("cart");
|
|
|
|
|
if(cart==null) {
|
|
|
|
|
if (cart == null) {
|
|
|
|
|
cart = new Cart();
|
|
|
|
|
req.getSession().setAttribute("cart",cart);
|
|
|
|
|
req.getSession().setAttribute("cart", cart);
|
|
|
|
|
}
|
|
|
|
|
// 将商品项添加到购物车中
|
|
|
|
|
cart.addItem(cartItem);
|
|
|
|
|
req.getSession().setAttribute("lastName",cartItem.getName());
|
|
|
|
|
// 在会话中保存最后添加的商品名称(可能用于显示或提示)
|
|
|
|
|
req.getSession().setAttribute("lastName", cartItem.getName());
|
|
|
|
|
|
|
|
|
|
//返回购物车总数量和最后一个商品的名称
|
|
|
|
|
Map<String,Object> resultMap = new HashMap<String,Object>();
|
|
|
|
|
resultMap.put("totalCount",cart.getTotalCount());
|
|
|
|
|
resultMap.put("lastName",cartItem.getName());
|
|
|
|
|
// 准备返回给客户端的数据,包括购物车总数量和最后一个商品的名称
|
|
|
|
|
Map<String, Object> resultMap = new HashMap<String, Object>();
|
|
|
|
|
resultMap.put("totalCount", cart.getTotalCount());
|
|
|
|
|
resultMap.put("lastName", cartItem.getName());
|
|
|
|
|
// 使用Gson将Map对象转换为JSON字符串
|
|
|
|
|
Gson gson = new Gson();
|
|
|
|
|
String resultMapJsonString = gson.toJson(resultMap);
|
|
|
|
|
// 将JSON字符串写入响应中
|
|
|
|
|
resp.getWriter().write(resultMapJsonString);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 加入购物车
|
|
|
|
|
* @param req
|
|
|
|
|
* @param resp
|
|
|
|
|
* @throws ServletException
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// 处理添加商品到购物车的请求(非AJAX方式)
|
|
|
|
|
protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
|
|
int id = WebUtils.parseInt(req.getParameter("id"),0);
|
|
|
|
|
Book book = bookService.queryBookById(id);
|
|
|
|
|
CartItem cartItem = new CartItem(book.getId(),book.getName(),1,book.getPrice(),book.getPrice());
|
|
|
|
|
// 以下代码与ajaxAddItem方法中的代码大致相同,只是最后返回的是页面重定向
|
|
|
|
|
int id = WebUtils.parseInt(req.getParameter("id"), 0);
|
|
|
|
|
Book book = bookService.queryBookById(id);
|
|
|
|
|
CartItem cartItem = new CartItem(book.getId(), book.getName(), 1, book.getPrice(), book.getPrice());
|
|
|
|
|
Cart cart = (Cart) req.getSession().getAttribute("cart");
|
|
|
|
|
if(cart==null) {
|
|
|
|
|
if (cart == null) {
|
|
|
|
|
cart = new Cart();
|
|
|
|
|
req.getSession().setAttribute("cart",cart);
|
|
|
|
|
req.getSession().setAttribute("cart", cart);
|
|
|
|
|
}
|
|
|
|
|
cart.addItem(cartItem);
|
|
|
|
|
req.getSession().setAttribute("lastName",cartItem.getName());
|
|
|
|
|
req.getSession().setAttribute("lastName", cartItem.getName());
|
|
|
|
|
// 重定向到请求来源页面(通常是通过Referer头部获取的)
|
|
|
|
|
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);
|
|
|
|
|
// 从请求中获取要删除的商品项ID
|
|
|
|
|
int id = WebUtils.parseInt(req.getParameter("id"), 0);
|
|
|
|
|
// 从会话中获取购物车对象
|
|
|
|
|
Cart cart = (Cart) req.getSession().getAttribute("cart");
|
|
|
|
|
if(cart!=null) {
|
|
|
|
|
// 如果购物车对象存在,则删除指定ID的商品项
|
|
|
|
|
if (cart != null) {
|
|
|
|
|
cart.deleteItem(id);
|
|
|
|
|
resp.sendRedirect(req.getHeader("Referer"));
|
|
|
|
|
}
|
|
|
|
|
// 重定向到请求来源页面
|
|
|
|
|
resp.sendRedirect(req.getHeader("Referer"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 清空商品项
|
|
|
|
|
* @param req
|
|
|
|
|
* @param resp
|
|
|
|
|
* @throws ServletException
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
// 处理清空购物车中所有商品项的请求
|
|
|
|
|
protected void clearItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
|
|
req.getSession().removeAttribute("cart");
|
|
|
|
|
// 从会话中移除购物车对象,从而清空购物车
|
|
|
|
|
req.getSession().removeAttribute("cart");
|
|
|
|
|
// 重定向到请求来源页面
|
|
|
|
|
resp.sendRedirect(req.getHeader("Referer"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 修改商品数量
|
|
|
|
|
* @param req
|
|
|
|
|
* @param resp
|
|
|
|
|
* @throws ServletException
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
// 处理修改购物车中商品项数量的请求
|
|
|
|
|
protected void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
|
|
int id = WebUtils.parseInt(req.getParameter("id"),0);
|
|
|
|
|
int count = WebUtils.parseInt(req.getParameter("count"),1);
|
|
|
|
|
// 从请求中获取要修改的商品项ID和新的数量
|
|
|
|
|
int id = WebUtils.parseInt(req.getParameter("id"), 0);
|
|
|
|
|
int count = WebUtils.parseInt(req.getParameter("count"), 1);
|
|
|
|
|
// 从会话中获取购物车对象
|
|
|
|
|
Cart cart = (Cart) req.getSession().getAttribute("cart");
|
|
|
|
|
if(cart!=null) {
|
|
|
|
|
cart.updateCount(id,count);
|
|
|
|
|
resp.sendRedirect(req.getHeader("Referer"));
|
|
|
|
|
// 如果购物车对象存在,则修改指定ID的商品项数量
|
|
|
|
|
if (cart != null) {
|
|
|
|
|
cart.updateCount(id, count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重定向到请求来源页面
|
|
|
|
|
resp.sendRedirect(req.getHeader("Referer"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|