###Jsp+Servlet购物商城day03.1:商品添加到购物车。重点笔记

来源:互联网 发布:苹果笔记本知乎 编辑:程序博客网 时间:2024/05/18 03:17


商品添加到购物车:

功能入口:商品详情页面productInfo.jsp,“加到购物车”按钮,

功能出口:购物车信息页面cart.jsp


①功能入口:商品详情页面productInfo.jsp,“加到购物车”按钮,

<div style="margin:0 auto;width:950px;"><div class="col-md-6"><img style="opacity: 1;width:400px;height:350px;" title="" class="medium" src="${pageContext.request.contextPath}/${pro.pimage}"></div><div class="col-md-6"><div><strong>${pro.pname }</strong></div><div style="border-bottom: 1px dotted #dddddd;width:350px;margin:10px 0 10px 0;"><div>商品编号:${pro.pid }</div></div><div style="margin:10px 0 10px 0;">商城价: <strong style="color:#ef0101;">¥:${pro.shop_price }</strong> 市场价: <del>¥${pro.market_price }</del></div><div style="margin:10px 0 10px 0;">促销: <a target="_blank" title="限时抢购 (2014-07-30 ~ 2015-01-01)" style="background-color: #f07373;">限时抢购</a> </div><div style="padding:10px;border:1px solid #e7dbb1;width:330px;margin:15px 0 10px 0;;background-color: #fffee6;"><div style="margin:5px 0 10px 0;">颜色分类</div><!-- ======【地址栏有哪些参数?===业务熟练。】 --><form action="${pageContext.request.contextPath}/CartServlet?method=addCart&pid=${pro.pid}" method="post"><div style="border-bottom: 1px solid #faeac7;margin-top:20px;padding-left: 10px;">购买数量:<input id="count" name="count" value="1" maxlength="4" size="10" type="text"> </div><div style="margin:20px 0 10px 0;;text-align: center;"><input style="background: url('${pageContext.request.contextPath}/images/product.gif') no-repeat scroll 0 -600px rgba(0, 0, 0, 0);height:36px;width:127px;" value="加入购物车" type="submit">  收藏商品</div></div></form></div></div>


②CartServlet:

package cn.itcast.servlet;import java.io.IOException;import java.sql.SQLException;import java.util.Collection;import java.util.LinkedHashMap;import java.util.Map;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import cn.itcast.domain.Cart;import cn.itcast.domain.CartItem;import cn.itcast.domain.Product;import cn.itcast.service.ProductService;import cn.itcast.service.impl.ProductServiceImpl;public class CartServlet extends BaseServlet {private static final long serialVersionUID = 1L;public String addCart(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {/* CartServlet?method=addCart&pid=${pro.pid} */String pid = request.getParameter("pid");int count = Integer.parseInt(request.getParameter("count"));//判断是不是第一次登录,Cart c = (Cart) request.getSession().getAttribute("cart");try {if (c==null) {c = new Cart();} //1ProductService ps = new ProductServiceImpl();Product pro = ps.findByPid(pid);//2CartItem cartItem = new CartItem();cartItem.setPro(pro);cartItem.setCount(count);c.addCartItem(cartItem);} catch (Exception e) {e.printStackTrace();}request.getSession().setAttribute("cart", c);return "/cart.jsp";}public String delAll(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {/*//==teacher   request.getSession().removeAttribute("cart");  return "/cart.jsp";*//* CartServlet?method=delAll  *///判断是不是第一次登录,Cart c = (Cart) request.getSession().getAttribute("cart");if (c==null) {return "/cart.jsp";} request.getSession().setAttribute("cart", null);return "/cart.jsp";}  //===ok(teacher) /* public String delItem(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { //CartServlet?method=addCart&pid=${pro.pid} String pid = request.getParameter("pid");Cart cart = (Cart) request.getSession().getAttribute("cart");cart.delByPid(pid);//==######=不用更新session,购物车条目也能更新原因:【内存中应用对象之间的依赖,根据地址值(变量名--所谓引用)依赖】。
==###===这里。假如购物车存在数据库而不是session,那么,每次删除一个购物车条目时,就必须更新session:【session.setAttribute("cart",cart)】,才能保证页面购物车条目显示更新。】
===【session里存的是 引用(地址值),remove之后,session存的地址(cart变量)是不变的,访问session再去访问存的地址对应的对象值时,值就已经变了。===依赖的应用类型改变,自动改变】===【存储关系,实际是依赖关系。】===那么product.setCategory(category)为什么存的是对象?==因为存到磁盘了。持久化了。【内存和磁盘有本质区别:①内存:栈内存、堆内存===一般程序运行结束会释放内存:(session的生命周期未到期,也是在【磁盘】中持久化了一个session"备份"---查看监听器:session的钝化和激活));②磁盘:涉及到数据持久化(物理存在)。product.setCategory(category)存的是对象,说的是存入数据库持久化了。==###===这里。假如购物车存在数据库而不是session,那么,每次删除一个购物车条目时,就必须更新session:【session.setAttribute("cart",cart)】,才能保证页面购物车条目显示更新。】==######=Session本身是一个Map对象,【session看做是运行中内存中临时创建的Map对象,Map引用到其他《内存中的》引用对象(这里是cart),存的是cart的堆内存地址;cart改变,下次session 根据cart地址 访问对应的】//request.getSession().setAttribute("cart", cart);//====【没有这句话,session也能自动更新购物车条目。原因如上】return "/cart.jsp";}*///===self==okpublic String delItem(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { //CartServlet?method=delItem&pid=${pro.pid} String pid = request.getParameter("pid");Cart cart = (Cart) request.getSession().getAttribute("cart");Map<String, CartItem> items = cart.getItemsMap();items.remove(pid);request.getSession().setAttribute("cart",cart);return "/cart.jsp";}public static void main(String[] args) {String pid = "1";int count = 3;try {//1ProductService ps = new ProductServiceImpl();Product pro = ps.findByPid(pid);// System.err.println(pro);//2CartItem cartItem = new CartItem();cartItem.setPro(pro);cartItem.setCount(count);Cart cart = new Cart();cart.addCartItem(cartItem);//request.setAttribute("cart", cart);System.out.println(cart);} catch (Exception e) {e.printStackTrace();}}}

③功能入口:商品详情页面productInfo.jsp,“加到购物车”按钮,

<tbody><tr class="warning"><th>图片</th><th>商品</th><th>价格</th><th>数量</th><th>小计</th><th>操作</th></tr><!-- cart.items.pro.pid====不能多级取值?可以。但是c:forEach 遍历项 必须在items下填写。直接取值Pro是不确定的。所以报错。所以删除某个购物项,购物项的id(也就是pid),必须在循环体里 传递出去。(onClick事件 js函数传递参数!!)循环体外部直接取购物项id不确定,会报错。 --><c:forEach var="item" items="${cart.items}" ><tr class="active"><td width="60" width="40%"><input type="hidden" name="id" value="22"><img src="${pageContext.request.contextPath}/${item.pro.pimage}" width="70" height="60"></td><td width="30%"><a target="_blank"> ${item.pro.pname}</a></td><td width="20%">¥${item.pro.shop_price}</td><td width="10%"><input type="text" name="quantity" value="${item.count }" maxlength="4" size="10"></td><td width="15%"><span class="subtotal">¥${item.total}</span></td><!-- <td><input type="button" value="删除" class="btn btn-danger" onclick="del()"/></td> --><td><input type="button" value="删除" class="btn btn-danger" onclick="del(${item.pro.pid})"/></td> </tr></c:forEach></tr></tbody>



==###===这里。假如购物车存在数据库而不是session,那么,每次删除一个购物车条目时,就必须更新session:【session.setAttribute("cart",cart)】,才能保证页面购物车条目显示更新。】
阅读全文
0 0
原创粉丝点击