综合项目重点笔记专业资料.doc
新巴巴运动网 项目第一天传智.上官云1 课前回顾1、 单点登陆 (搭建环境) 2、 去登陆页面(http:/localhost:8082/login.aspx?return Url = EncodeURLComment(window.location.href)3、 提交登陆(1)顾客名、密码、ReturnUrl)4、 Value=login.aspx ,method=POST GET5、 加密6、 把顾客名保存Redis(Session共享) 1)分析K:V CSESSIONID:USER-Name V:fbb2)SessionProvider ( SessionProviderImpl) 手动实例化 (xml配备时间 60分钟)3)保存顾客名到Redis中 7、重定向到之前访问页面8、页面 (登陆、注册、退出,我订单) 分析:页面(动) 静 ajax判断顾客与否登陆页面(前台系统判断顾客与否登陆) ajax 去此外一种系统 返回值被拦截(浏览器)判断程序(单点登陆系统)Ajax 返回值 :jsonàjsonp登陆系统(接受String callback) Spring MappingJacksonValue (保存数据 返回JSONP数据) mjv.setJSONFunction(callback); return mjv;(办法返回值处ResponseBody MappingJacksonValue2 今天内容主打购物车1、 购物车 2、 面向对象设计购物车3、 购物车 非登陆 登陆4、 关闭浏览器还必要在5、 跨电脑或区域购物车还在6、 同款数量追加 同款商品合并7、 排序同窗完毕3 面向对象设计购物车4 加入购物车4.1 设立途径4.2 Controller5 分析购物车Cookie中Session中(Redis) 顾客名(顾客ID)数据库非登陆:商品保存Cookie里 关闭浏览器还在 (七天、一种月)顾客清理Cookie就没了登陆 : Redis 性能比较好 关闭浏览器(不登陆不在) 登陆就在了 (跨区域) 永久保存5.1 Redis保存购物车加入购物车登陆非登陆1:从Request中取Cookies、遍历Cookie 取出之前购物车2:判断Cookie中没有购物车3:有 把购物车中商品添加到Redis购物车中,清理之前Cookie4:没有 5:直接添加当前商品到Redis中购物车里1:从Request中取Cookies、遍历Cookie 取出之前购物车2:判断Cookie中没有购物车3:有 4:没有 创立购物车5:追加当前商品到购物车6:创立Cookie 把新购物车放进去7:保存Cookie写回浏览器去购物车页面判断与否登陆登陆1:从Request中取Cookies、遍历Cookie 取出之前购物车2:再把购物车保存到Redis中,清理Cookie3:从Redis中取出所有购物车把购物车装满跳转到购物车页面回显购物车内容1:从Request中取Cookies、遍历Cookie 取出之前购物车2:有 购物车数量及SKUID3:没有把购物车装满跳转到购物车页面回显购物车内容购物车(回显数据到购物车页面6 购物车6.1 对象与JSON字符串互转6.2 SkuService入参: SKUID返回值:Sku对象SKu对象(涉及颜色对象、商品对象)商品表SKU表颜色表保存商品到REdis中从Redis中取出购物车6.3 Controller6.3.1 未 优化之前代码package cn.itcast.core.controller;import java.io.StringWriter;import java.util.List;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import com.fasterxml.jackson.annotation.JsonInclude.Include;import com.fasterxml.jackson.databind.ObjectMapper;import mon.utils.RequestUtils;import mon.web.Constants;import cn.itcast.core.bean.BuyerCart;import cn.itcast.core.bean.BuyerItem;import cn.itcast.core.bean.product.Sku;import cn.itcast.core.service.product.SkuService;import cn.itcast.core.service.user.SessionProvider;/* * 购物车 * 去购物车页面 * 添加商品到购物车 * 删除 * + * author lx * */Controllerpublic class CartController Autowiredprivate SessionProvider sessionProvider;/加入购物车RequestMapping(value = "/addCart")public String addCart(Long skuId,Integer amount,Model model,HttpServletRequest request,HttpServletResponse response) throws ExceptionObjectMapper om = new ObjectMapper();/不要NULL 不要转了om.setSerializationInclusion(Include.NON_NULL);/声明BuyerCart buyerCart = null;/1:从Request中取Cookies、Cookie cookies = request.getCookies();if(null != cookies&& cookies.length >0)/遍历Cookie 取出之前购物车for (Cookie cookie :cookies) /2:判断Cookie中没有购物车if(Constants.BUYER_CART.equals(cookie.getName()/转回对象buyerCart = om.readValue(cookie.getValue(),BuyerCart.class);break;/顾客与否登陆String username = sessionProvider.getAttributeForUsername(RequestUtils.getCSESSIONID(request,response);if(null != username)if(null != buyerCart)/3:有 把购物车中商品添加到Redis购物车中,skuService.insertBuyerCartToRedis(buyerCart,username);/清理之前Cookie4Cookie cookie = new Cookie(Constants.BUYER_CART,null);cookie.setMaxAge(0);cookie.setPath("/");response.addCookie(cookie);/4:没有 /5:直接添加当前商品到Redis中购物车里/程序未写else/3:有 /4:没有 创立购物车/判断购物车与否为nullif(null = buyerCart)buyerCart = new BuyerCart();/5:追加当前商品到购物车Sku sku = new Sku();/ID sku.setId(skuId);BuyerItem buyerItem = new BuyerItem();buyerItem.setSku(sku);/AmountbuyerItem.setAmount(amount);/追加商品到购物车buyerCart.addItem(buyerItem);/6:创立Cookie 把新购物车放进去StringWriter w = new StringWriter();om.writeValue(w,buyerCart);Cookie cookie = new Cookie(Constants.BUYER_CART,w.toString();/设立时间 写程序1天cookie.setMaxAge(60*60*24);/设立途径 cookie.setPath("/");/上线后 申请域名/7:保存写回浏览器response.addCookie(cookie);return "redirect:/toCart"Autowiredprivate SkuService skuService;/去购物车页面RequestMapping(value = "/toCart")public String toCart(Model model,HttpServletRequest request,HttpServletResponse response) throws Exception/1:从Request中取Cookies、遍历Cookie 取出之前购物车ObjectMapper om = new ObjectMapper();/不要NULL 不要转了om.setSerializationInclusion(Include.NON_NULL);/声明BuyerCart buyerCart = null;/1:从Request中取Cookies、Cookie cookies = request.getCookies();if(null != cookies&& cookies.length >0)/遍历Cookie 取出之前购物车for (Cookie cookie :cookies) /2:判断Cookie中没有购物车if(Constants.BUYER_CART.equals(cookie.getName()/转回对象buyerCart = om.readValue(cookie.getValue(),BuyerCart.class);break;if(null != buyerCart)/2:有 购物车数量及SKUID/把购物车装满List<BuyerItem> items = buyerCart.getItems();for (BuyerItem buyerItem :items) buyerItem.setSku(skuService.selectSkuById(buyerItem.getSku().getId();/3:没有/回显购物车内容model.addAttribute("buyerCart",buyerCart);/跳转到购物车页面return "cart"6.3.2 优化之后裔码package cn.itcast.core.controller;import java.io.StringWriter;import java.util.List;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import com.fasterxml.jackson.annotation.JsonInclude.Include;import com.fasterxml.jackson.databind.ObjectMapper;import mon.utils.RequestUtils;import mon.web.Constants;import cn.itcast.core.bean.BuyerCart;import cn.itcast.core.bean.BuyerItem;import cn.itcast.core.bean.product.Sku;import cn.itcast.core.service.product.SkuService;import cn.itcast.core.service.user.SessionProvider;/* * 购物车 * 去购物车页面 * 添加商品到购物车 * 删除 * + * author lx * */Controllerpublic class CartController Autowiredprivate SessionProvider sessionProvider;/加入购物车RequestMapping(value = "/addCart")public String addCart(Long skuId,Integer amount,Model model,HttpServletRequest request,HttpServletResponse response) throws ExceptionObjectMapper om = new ObjectMapper();/不要NULL 不要转了om.setSerializationInclusion(Include.NON_NULL);/声明BuyerCart buyerCart = null;/1:从Request中取Cookies、Cookie cookies = request.getCookies();if(null != cookies&& cookies.length >0)/遍历Cookie 取出之前购物车for (Cookie cookie :cookies) /2:判断Cookie中没有购物车if(Constants.BUYER_CART.equals(cookie.getName()/转回对象buyerCart = om.readValue(cookie.getValue(),BuyerCart.class);break;/3:有 /4:没有 创立购物车/判断购物车与否为nullif(null = buyerCart)buyerCart = new BuyerCart();/5:追加当前商品到购物车Sku sku = new Sku();/ID sku.setId(skuId);BuyerItem buyerItem = new BuyerItem();buyerItem.setSku(sku);/AmountbuyerItem.setAmount(amount);/追加商品到购物车buyerCart.addItem(buyerItem);/顾客与否登陆String username = sessionProvider.getAttributeForUsername(RequestUtils.getCSESSIONID(request,response);if(null != username)/3:有 把购物车中商品添加到Redis购物车中,skuService.insertBuyerCartToRedis(buyerCart,username);/清理之前Cookie4Cookie cookie = new Cookie(Constants.BUYER_CART,null);cookie.setMaxAge(0);cookie.setPath("/");response.addCookie(cookie);else/6:创立Cookie 把新购物车放进去StringWriter w = new StringWriter();om.writeValue(w,buyerCart);Cookie cookie = new Cookie(Constants.BUYER_CART,w.toString();/设立时间 写程序1天cookie.setMaxAge(60*60*24);/设立途径 cookie.setPath("/");/上线后 申请域名/7:保存写回浏览器response.addCookie(cookie);return "redirect:/toCart"Autowiredprivate SkuService skuService;/去购物车页面RequestMapping(value = "/toCart")public String toCart(Model model,HttpServletRequest request,HttpServletResponse response) throws Exception/1:从Request中取Cookies、遍历Cookie 取出之前购物车ObjectMapper om = new ObjectMapper();/不要NULL 不要转了om.setSerializationInclusion(Include.NON_NULL);/声明BuyerCart buyerCart = null;/1:从Request中取Cookies、Cookie cookies = request.getCookies();if(null != cookies&& cookies.length >0)/遍历Cookie 取出之前购物车for (Cookie cookie :cookies) /2:判断Cookie中没有购物车if(Constants.BUYER_CART.equals(cookie.getName()/转回对象buyerCart = om.readValue(cookie.getValue(),BuyerCart.class);break;if(null != buyerCart)/2:有 购物车数量及SKUID/把购物车装满List<BuyerItem> items = buyerCart.getItems();for (BuyerItem buyerItem :items) buyerItem.setSku(skuService.selectSkuById(buyerItem.getSku().getId();/3:没有/回显购物车内容model.addAttribute("buyerCart",buyerCart);/跳转到购物车页面return "cart"7 问题7.1 同款商品合并非登陆登陆7.2 小计7.3 排序非登陆登陆7.4 去结算7.5 提交订单7.6 付款(不做了)