一:servlet的抽取1
1.servlet层新建ProductServlet /product
//模块中的功能用方法进行区分
//抽取成方法
...
/product?method=xxxx
//获得请求的方法
String methodName = request.getParameter("method");
if("productList".equals(methodName)){
...
}else if(...){
...
}
web.xml中删除servlet多余的
改相应页面的URL
二:servlet抽取2
1.user相关的servlet也可以抽取
2.抽取BaseServlet extends HttpServlet
其他继承BaseServlet
// service方法中
request.setCharacterEncoding("UTF-8");
//1.获得请求的method的名称
String methodName = request.getParameter("method");
//2.获得当前被访问的对象的字节码对象
Class clazz = this.getClass();
//3.获得当前字节码对象中的指定方法
Method method = clazz.getMethod(methodName,HttpServletRequest.class,HttpServletResponse.class)
//4.执行相应功能方法
method.invoke(this,request,response);
三.购物车分析
1.购物车放那些信息?
product ,buyNum,subTotal//购物项
购物项包含product对象,数量,小计
Cart对象放多个购物项CartItem
用集合,但是为了删除取出方便用Map集合
Cart对象放到session中
多个购物项组成购物车
2.放到哪个位置?session中
四:购物车的代码实现一
1.domain中
新建CartItem
public class CartItem{
private Product product;
private int buyNum;
private double subtotal;
getter和setter;
}
2.domain中
新建Cart对象
public class Cart{
//多个购物项
private Map<String,CartItem> cartItems = new HashMap<String,CartItem>();
//商品的总计
private double total;
getter和setter;
}
3.product_info.jsp中
加入购物车:<a href="javascript:void(0);" οnclick=
"addCart()">
<script type="text/javascript">
function addCart(){
var buyNum = $("#buyNum").val();
location.href="${pageContext.request.contextPath}/product?method=addProductToCart&pid=${product.pid}&buyNum="+buyNum;
}
</script>
加入购物车:${pageContext.request.contextPath}/product?method=addProductToCart&pid=${product.pid}
4.ProductServlet中
新增方法addProductToCart
//将商品添加到购物车
public void addProductToCart(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ProductService service = new ProductService();
HttpSession session = request.getSession();
//获得要放到购物车的商品的pid
String pid = request.getParameter("pid");
//获得该商品的购买数量
int buyNum = Integer.parseInt(request.getParameter("buyNum"));
//获得product对象
Product product = service.findProductByPid(pid);
//计算小计
double subtotal = product.getShop_price()*buyNum;
//封装CartItem
CartItem item = new CartItem();
item.setProduct(product);
item.setBuyNum(buyNum);
item.setSubtotal(subtotal);
//获得购物车--判断是否在session中已经存在购物车
Cart cart = (Cart) session.getAttribute("cart");
if(cart==null){
cart = new Cart();
}
//将购物项放到车中--key是pid
//先判断购物车中是否已经包含此购物项 判断key是否已经存在
//如果购物车已经存在该商品,将现在买的数量与原有数量进行相加操作
Map<String,CartItem> cartItems = cart.getCartItems();
double newsubtotal=0.0;
if(cartItems.containsKey(pid)){
//取出原有商品的数量
CartItem cartItem = cartItems.get(pid);
int oldBuyNum = cartItem.getBuyNum();
oldBuyNum+=buyNum;
cartItem.setBuyNum(oldBuyNum);
cart.setCartItems(cartItems);
//修改小计
double oldsubtotal = cartItem.getSubtotal();
//新买的商品小计
newsubtotal = buyNum*product.getShop_price();
cartItem.setSubtotal(oldsubtotal+newsubtotal);
}else{
cart.getCartItems().put(product.getPid(),item);
newsubtotal = buyNum*product.getShop_price();
}
//计算总计
double total = cart.getTotal()+newsubtotal;
cart.setTotal(total);
//将购物车再次放回session中
session.setAttribute("cart",cart);
//直接跳转到购物车页面
//转发有问题,所以用重定向
//request.getRequestDispatcher("/cart.jsp").forward(request,response);
response.sendRedirect(request.getContextPath()+"/cart.jsp");
}
5.cart.jsp中
导入taglib
<c:forEach items="${cart.cartItems}" var="entry">
${pageContext.request.contextPath}/${entry.value.product.pimage}
${entry.value.product.pname}
${entry.value.product.shop_price}
${entry.value.buyNum}
${entry.value.subtotal}
${cart.total}
${cart.total}
</c:forEach>
五:购物车的代码实现二
1.注意重定向和转发在用户刷新cart.jsp时的表现
2.注意两次买同一商品数量以及小计,总计的计算
3.<c:forEach>放table中
六:删除单个商品:
1.cart.jsp中
删除:
<a href="javascript:void(0);" οnclick="delProFromCart('${entry.value.product.pid}')" class="delete"
<script type="text/javascript">
function delProFromCart(pid){
if(confirm("您是否要删除该项?")){
location.href="${pageContext.request.contextPath}/product?method=delProFromCart&pid="+pid;
}
}
</script>
<!--判断购物车中是否有商品-->
<c:if test="${!empty cart.cartItems}">
...
</c:if>
<c:if test="${empty cart.cartItems}">
<div>
<img alt="" src="${pageContext.request.contextPath}/images/cart-empty.png">
<a href="${pageContext.request.contextPath}">返回首页</a>
</div>
</c:if>
2.productServlet中
delProFromCart{
//获得要删除的item的pid
String pid = request.getParameter("pid");
//删除session中的购物车中的购物项集合中的item
HttpSession session = request.getSession();
Cart cart = (Cart)session.getAttribute("cart");
if(cart!=null){
Map<String,CartItem> cartItems = cart.getCartItems();
//需要修改总价
cart.setTotal(cart.getTotal()-cartItems.get(pid).getSubtotal());
//改完价格再删除
cartItems.remove(pid);
cart.setCartItems(cartItems);
}
session.setAttribute("cart",cart);
//跳转回cart.jsp中
response.sendRedirect(request.getContextPath()+"/cart.jsp");
}
七:清空购物车:
1.cart.jsp中
<a href="javascript:void(0);" οnclick="clearCart()"
function clearCart(){
if(confirm("您是否要清空购物车")){
location.href="${pageContext.request.contextPath}/product?method=clearCart";
}
}
2.productServlet中
clearCart
HttpSession session = request.getSession();
session.removeAttribute("cart");
//跳转到cart.jsp
response.sendRedirect(request.getContextPath()+"/cart.jsp");