在listcart.jsp中修改如下代码
Listcart.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
购物车显示页面
您购买了如下商品
您没有购买任何商品A
编号
书名
单价
数量
小计
操作
${me.key}
${me.value.book.name}
${me.value.book.price}
${me.value.price}
删除
总价
${cart.price}
清空购物车
listcart.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
购物车显示页面
您购买了如下商品
您没有购买任何商品A
编号
书名
单价
数量
小计
操作
${me.key}
${me.value.book.name}
${me.value.book.price}
${me.value.price}
删除
总价
${cart.price}
清空购物车
新建ChangeQuantityServlet
package www.hbsi.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import www.hbsi.domain.Cart;
import www.hbsi.service.BookService;
import www.hbsi.service.BookServiceImpl;
public class ChangeQuantityServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
String quantity = request.getParameter("quantity");
Cart cart = (Cart) request.getSession().getAttribute("cart");
BookService service = new BookServiceImpl();
service.changeQuantity(id,quantity,cart);
request.getRequestDispatcher("/WEB-INF/jsp/listcart.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
在BookService写public void changeQuantity(String id, String quantity, Cart cart);方法
package www.hbsi.service;
import java.util.List;
import www.hbsi.domain.Book;
import www.hbsi.domain.Cart;
public interface BookService {
//查询所有书籍
public List getAll();
//根据ID查找书籍
public Book findById(String id);
//删除购物项
public void deleteById(String id, Cart cart);
//清空购物车
public void clearCart(Cart cart);
public void changeQuantity(String id, String quantity, Cart cart);
}
package www.hbsi.service;
import java.util.List;
import www.hbsi.dao.BookDao;
import www.hbsi.dao.BookDaoImpl;
import www.hbsi.domain.Book;
import www.hbsi.domain.Cart;
import www.hbsi.domain.CartItem;
public class BookServiceImpl implements BookService {
BookDao bd = new BookDaoImpl();
public Book findById(String id) {
// TODO Auto-generated method stub
return bd.findById(id);
}
public List getAll() {
// TODO Auto-generated method stub
return bd.getAll();
}
public void deleteById(String id, Cart cart) {
// TODO Auto-generated method stub
cart.getMap().remove(id);
}
public void clearCart(Cart cart) {
// TODO Auto-generated method stub
cart.getMap().clear();
}
public void changeQuantity(String id, String quantity, Cart cart) {
// TODO Auto-generated method stub
//获取购物项
CartItem item = cart.getMap().get(id);
item.setQuantity(Integer.parseInt(quantity));
}
}
Jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
购物车显示页面
您购买了如下商品
您没有购买任何商品A
编号
书名
单价
数量
小计
操作
${me.key}
${me.value.book.name}
${me.value.book.price}
${me.value.price}
删除
总价
${cart.price}
清空购物车
点击确定