天天看点

MVC简易购物车项目--展示购物车页面

在展示购物车这个页面可以看到自己的订单,并且可以修改商品的数量,并更新总价,还可以删除商品。

当在购物大厅点击购买时,提交给GoShowMyCart这个servlet处理

GoShowMyCart.java

public class GoShowMyCart extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();		
		
		MyCart myCart=(MyCart)request.getSession().getAttribute("myCart");				
		//把要显示的数据放入request,准备显示。
		request.setAttribute("bookList", myCart.showMyCart());		
		request.setAttribute("totalPrice", myCart.getTotalPrice()+"");
		request.getRequestDispatcher("/WEB-INF/showMyCart.jsp").forward(request, response);		
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}
           

然后跳转到 showMyCart.jsp页面

showMyCart.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
import="java.util.*,com.wxh.domain.*" 
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body style="text-align:center; margin:0 auto; ">
    <h1>我的购物车</h1>
   <%-- <a href="/shopping/GoHallUI?id=<%=((Users)session.getAttribute(" target="_blank" rel="external nofollow" loginUser")).getId()%>&&password=<%=((Users)session.getAttribute("loginUser")).getPwd()%>">返回购物大厅</a> --%>
    <a href="/shopping/GoHallUI" target="_blank" rel="external nofollow" >返回购物大厅</a>
    <form action="/shopping/ShoppingClServlet?type=update" method="post">
    <table style="border-collapse: collapse; width: 600px "  align="center">
    <tr><th>BookID</th><th>书名</th><th>价格</th><th>出版社</th><th>数量</th><th>是否删除</th></tr>
    
    <%
    //从request中取出要显示的商品信息
    ArrayList al=(ArrayList)request.getAttribute("bookList");
    for(int i=0;i<al.size();i++){
    
    Book book=(Book)al.get(i);
    %>
     <tr><td><%=book.getId() %><input type="hidden" name="id" value="<%=book.getId() %>"/></td><td><%=book.getName()%></td><td><%=book.getPrice() %></td>
     <td><%=book.getPublishHouse() %></td><td><input type="text" name="booknum" value="<%=book.getShoppingNum()%>"/>本
     </td><td><a href="/shopping/ShoppingClServlet?type=del&id=<%=book.getId()%>" target="_blank" rel="external nofollow" >删除</a></td></tr>
    <%
    }    
     %>     
    <tr><td colspan="6"><input type="submit" value="update"></td></tr>
    <tr><td colspan="6">购物车的总价格是 :${totalPrice}  元</td></tr>
    </table>
    </form>
    <a href="/shopping/GoMyOrderServlet" target="_blank" rel="external nofollow" >提交订单</a>
  </body>
</html>
           

这里的数量修改和删除都交给ShoppingClServlet处理

点击提交订单以后交给GoMyOrderServlet处理。

——摘自《韩顺平细说jsp》