一:购物车
//购物项
public class CartItem {
private Product product;
private int buyNum;
private double subtotal;
}
//购物车 每一个购物项里面只有一个商品,因此用Map来做为购物车
public class Cart {
//该购物车中存储的n个购物项
private Map<String,CartItem> cartItems=new HashMap<String,CartItem>();
//商品的总计
private double total;
}
<script type="text/javascript">
function addCart(){
//获得购买的商品的数量
var buyNum=$("#buyNum").val();
location.href="${pageContext.request.contextPath}/addCartServlet?pid=${product.pid}&buyNum="+buyNum;
}
</script>
//将商品添加到购物车
public class AddCartServlet extends HttpServlet {
public void doGet(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);
//直接跳转到购物车页面
response.sendRedirect(request.getContextPath()+"/cart.jsp");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
二:删除单个商品
<script type="text/javascript">
function delProFromCart(pid){
if(confirm("您确定要删除该商品吗?")){
location.href="${pageContext.request.contextPath}/delProFromCartServlet?pid="+pid;
}
}
</script>
//删除单一商品
public class DelProFromCartServlet extends HttpServlet {
public void doGet(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 doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
三:购物车里面没有商品的时候,就显示购物车图片
从网上看到好看的图片,怎么将图片保存下来,
1.若是a标签或者img标签的图片,可以直接用鼠标拖出去保存
2.若是背景图片,可以用开发者工具
主要是在前台判断购物车是否有商品,然后显示不同的东西
四:清空购物车
<script type="text/javascript">
function clearCart(){
if(confirm("您确定要清空购物车吗?")){
location.href="${pageContext.request.contextPath}/clearCartServlet";
}
}
</script>
//清空购物车
public class ClearCartServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.removeAttribute("cart");
//跳转回cart.jsp
response.sendRedirect(request.getContextPath()+"/cart.jsp");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}