--------------------购物车-------------
- el不会报空指针,会自动检测
- map键值对可get(key)来获得对应值,但for循环获取到的是entry,需要entry.getvalue获得相应值
---------清空购物车
//清空购物车
public void clearCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.removeAttribute("cart");//invalidate是把session整体干掉
//跳转回cart.jsp
response.sendRedirect(request.getContextPath()+"/cart.jsp");
}
------------jsp清空
function clearCart(){
if(confirm("您是否要清空购物车?")){
location.href="${pageContext.request.contextPath }/product?method=clearCart";
}
}
------<!-- 判断购物车中是否有商品 -->
<c:if test="${!empty cart.cartItems }">
---map
if(cartItems.containsKey(pid))
cartItems.get(pid)
----------cart.jsp---
---跳出选框成if条件
---简写不跳转javascript:
function clearCart(){
if(confirm("您是否要清空购物车?")){
location.href="${pageContext.request.contextPath }/product?method=clearCart";
}
}
---<a href="javascript:;" onclick="clearCart()" id="clear" class="clear">清空购物车</a>
--------------servlet删除单一品
public void delProFromCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获得要删除的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");
}
---------------------添加到购物车
//将商品添加到购物车
public void addProductToCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
ProductService service = new ProductService();
//获得要放到购物车的商品的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);
//直接跳转到购物车页面
response.sendRedirect(request.getContextPath()+"/cart.jsp");
}
- javascript:void(0)=javascript:
- 注意加入购物车之前需要判断车内是否有该商品
- 商品加入购物车转发地址跳转到购物车页面,如果购车界面刷新则刚买商品会继续加入购物车(地址参数,因为地址不变刷新重新运行一次购买功能代码),解决办法:改成重定向(仅刷新jsp页面)
- 一个控件跳转中需获取另一个控件参数
在本控件不跳转,而是转到点击方法
<a href="javascript:void(0);" onclick="addCart()">
---------------
function addCart(){
//获得购买的商品的数量
var buyNum = $("#buyNum").val();
location.href="${pageContext.request.contextPath}/product?method=addProductToCart&pid=${product.pid}&buyNum="+buyNum;
}
--------------------抽取---------------
- 抽取可以将多个servlet方法放到一个,传递参数method确定调用那个方法
- 系统找子方法中没有相应方法会据需向上一层找例如:找product方法doget,但没有会找product的父类中相应方法
----------父类抽取子类的方法------
@SuppressWarnings("all")
public class BaseServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
try {
//1、获得请求的method的名称
String methodName = req.getParameter("method");
//2、获得当前被访问的对象的字节码对象
Class clazz = this.getClass();//ProductServlet.class ---- UserServlet.class
//3、获得当前字节码对象的中的指定方法
Method method = clazz.getMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
//4、执行相应功能方法
method.invoke(this, req,resp);
} catch (Exception e) {
e.printStackTrace();
} }


网友评论