分析:
入口在cart.jsp页面的清空购物车a标签所有位置,单击触发事件,加上友好提示confirm ,确定删除后执行功能代码。
清空购物车,最直观,即把购物车cart对象,从session域中去除。而不能把session清掉,因为,session还有其他存放的信息。
准备:
步骤:
cart.jsp
1.在 清空购物车a标签位置,加入单击事件。
2.编写单击事件函数,该该函数加入confirm判断,函数跳到后台清空购物车代码。
ProductServlet
1.获取session域
2,清空session域中的cart对象
3,跳回cart.jsp页面
cart.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>黑马商城购物车</title>
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
<script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
<script src="js/bootstrap.min.js" type="text/javascript"></script>
<script type="text/javascript">
function delProFromCart(pid){
if(confirm("您是否确定删除该商品")){
location.href="${pageContext.request.contextPath}/product?method=delProFromCart&pid="+pid;
}
}
function clearCart(){
if(confirm("您是否确定清空购物车")){
location.href="${pageContext.request.contextPath}/product?method=clearCart";
}
}
</script>
<!-- 引入自定义css文件 style.css -->
<link rel="stylesheet" href="css/style.css" type="text/css" />
<style>
body {
margin-top: 20px;
margin: 0 auto;
}
.carousel-inner .item img {
width: 100%;
height: 300px;
}
font {
color: #3164af;
font-size: 18px;
font-weight: normal;
padding: 0 10px;
}
</style>
</head>
<body>
<!-- 引入header.jsp -->
<jsp:include page="/header.jsp"></jsp:include>
<div class="container">
<div class="row">
<!-- 判断购物车内的购物项是否为空 -->
<c:if test="${!empty cart.cartItems }">
<div style="margin:0 auto; margin-top:10px;width:950px;">
<strong style="font-size:16px;margin:5px 0;">订单详情</strong>
<table class="table table-bordered">
<tbody>
<tr class="warning">
<th>图片</th>
<th>商品</th>
<th>价格</th>
<th>数量</th>
<th>小计</th>
<th>操作</th>
</tr>
<!-- 动态显示购物项 -->
<c:forEach items="${cart.cartItems }" var="entry">
<tr class="active">
<td width="60" width="40%">
<input type="hidden" name="id" value="22">
<img src="${entry.value.product.pimage }" width="70" height="60">
</td>
<td width="30%">
<a target="_blank">${entry.value.product.pname }</a>
</td>
<td width="20%">
¥${entry.value.product.shop_price }
</td>
<td width="10%">
<input type="text" name="quantity" value="${entry.value.buyNum }" maxlength="4" size="10">
</td>
<td width="15%">
<span class="subtotal">${entry.value.subtotal }</span>
</td>
<td>
<a href="javascript:;" class="delete" onclick="delProFromCart('${entry.key}')">删除<