1@
;
3//去购物车结算,这里有两个地方可以直达:1,在商品详情页中点击加入购物车按钮2,直接点击购物车按钮4@(="")
(,,
)throwsJsonParseException,JsonMappingException,IOException{
7//将对象转换成json字符串/json字符串转成对象8ObjectMapperom=newObjectMapper();
9om.setSerializationInclusion(Include.NON_NULL);
10BuyerCartbuyerCart=null;
11//1,获取Cookie中的购物车12Cookie[]cookies=request.getCookies();
13if(null!=cookiescookies.length0){
14for(Cookiecookie:cookies){
15//
16if(Constants.BUYER_CART.equals(cookie.getName())){
17//购物车对象与json字符串互转18buyerCart=om.readValue(cookie.getValue(),BuyerCart.class);
19break;
20}
21}
22}
2324//判断是否登录25Stringusername=sessionProviderService.getAttributterForUsername(RequestUtils.getCSessionId(request,response));
26if(null!=username){
27//登录了
28//2,购物车有东西,则将购物车的东西保存到Redis中29if(null==buyerCart){
30cartService.insertBuyerCartToRedis(buyerCart,username);
31//清空Cookie设置存活时间为0,立马销毁32Cookiecookie=newCookie(Constants.BUYER_CART,null);
33cookie.setPath("/");
34cookie.setMaxAge(-0);
35response.addCookie(cookie);
36}
37//3,取出Redis中的购物车38buyerCart=cartService.selectBuyerCartFromRedis(username);
39}
404142//4,没有则创建购物车43if(null==buyerCart){
44buyerCart=newBuyerCart();
45}
4647//5,将购物车装满,前面只是将skuId装进购物车,这里还需要查出sku详情48ListBuyerItemitems=buyerCart.getItems();
49if(items.size()0){
50//只有购物车中有购物项,才可以将sku相关信息加入到购物项中51for(BuyerItembuyerItem:items){
52buyerItem.setSku(cartService.selectSkuById(buyerItem.getSku().getId()));
53}
54}
5556//5,上面已经将购物车装满了,这里直接回显页面57model.addAttribute("buyerCart",buyerCart);
5859//跳转购物页面60return"cart";
61}
这里就是购物车详情展示页面,这里需要注意,如果是同一件商品连续添加,是需要合并的.
购物车详情展示页面就包括两大块,1)商品详情2)总计(商品总额,运费)
其中1)商品详情又包括商品尺码,商品颜色,商品购买数量,是否有货.
取出Redis中的购物车:buyerCart=cartService.selectBuyerCartFromRedis(username);
1//取出Redis中购物车2publicBuyerCartselectBuyerCartFromRedis(Stringusername){
3BuyerCartbuyerCart=newBuyerCart();
4//获取所有商品,redis中保存的是skuId为key,amount为value的Map集合5MapString,StringhgetAll=jedis.hgetAll("buyerCart:"+username);
6SetEntryString,StringentrySet=hgetAll.entrySet();
7for(EntryString,Stringentry:entrySet){
8//entry.getKey():skuId9Skusku=newSku();
10sku.setId(Long.parseLong(entry.getKey()));
11BuyerItembuyerItem=newBuyerItem();
12buyerItem.setSku(sku);
13//entry.getValue():amount14buyerItem.setAmount(Integer.parseInt(entry.getValue()));
15//添加到购物车中16buyerCart.addItem(buyerItem);
17}
1819returnbuyerCart;
20}
ViewCode
将购物车装满,前面只是将skuId装进购物车,这里还需要查出sku详情:ListBuyerItemitems=buyerCart.getItems();
buyerItem.setSku(cartService.selectSkuById(buyerItem.getSku().getId()));
1//向购物车中的购物项添加相应的数据,通过skuId查询sku对象,颜色对象,商品对象2publicSkuselectSkuById(LongskuId){
3Skusku=skuDao.selectByPrimaryKey(skuId);
4//颜色5sku.setColor(colorDao.selectByPrimaryKey(sku.getColorId()));
6//添加商品信息7sku.setProduct(productDao.selectByPrimaryKey(sku.getProductId()));
8returnsku;
9}
ViewCode
接着就返回"cart.jsp",这个就是购物车详情展示页面了.