案例3-對購物車進行操作(清空購物車) 步驟分析: 1.在購物車頁面上,有一個清空購物車的連接 /store/cart?method=clear 2.在cartServlet中需要提供 clear 先獲取購物車 調用Clearcart方法 重定向 jsp/cart.jsp頁面 3.判斷購物車是否為空, 若為空:提示購物車空空如也 若不為空:展示
/store/WebContent/jsp/cart.jsp
<!--判斷是否為空-->
<c:if test="${empty cart.map }">
<h1>購物車空空如也~~趕緊逛逛去!!</h1>
</c:if>
<c:if test="${not empty cart.map }"></>
....
<div style="text-align:right;margin-top:10px;margin-bottom:10px;"> <a href="${pageContext.request.contextPath }/cart?method=clear" id="clear" class="clear">清空購物車</a> <a href="${pageContext.request.contextPath }/order?method=add"> <input type="submit" width="100" value="提交訂單" name="submit" border="0" style="background: url('${pageContext.request.contextPath}/images/register.gif') no-repeat scroll 0 0 rgba(0, 0, 0, 0); height:35px;width:100px;color:white;"> </a> </div>
com.louis.web.servlet.CartServlet
/* * 清空購物車 * */ public String clear(HttpServletRequest request, HttpServletResponse response) throws Exception{ //1、獲取購物車 //2、調用購物車的lear方法 getCart(request).clearCart(); //3、重定向 response.sendRedirect(request.getContextPath()+"/jsp/cart.jsp"); return null; }