details.html
<form action="addCart" th:object="${goodsForm}" method="post">
<div class="col-sm-6">
<input type="hidden" name="commodityId" th:value="${goodsForm.commodityId}"/>
<h4 class="yh detailsT"><span th:text="${goodsForm.commodityName}">裕道府东北有机小米食用米 粗粮黄小米 五谷杂粮月子米</span></h4>
<dl class="dl-horizontal detailsInfo cf">
<dt>零售价:</dt>
<dd class="price yh"><i>¥</i><span th:text="${goodsForm.retailPrice}">128</span></dd>
<dt>运 费 :</dt>
<dd>8元</dd>
<dt>品 牌:</dt>
<dd><span th:text="${goodsForm.brandName}">日思</span></dd>
<dt>经销商:</dt>
<dd><span th:text="${goodsForm.supplierName}">天津日思优质小站稻开发公司</span></dd>
<dt>规 格:</dt>
<dd>每<span th:text="${#strings.concat(goodsForm.unit).concat(goodsForm.specifications)}">袋5kg</span></dd>
<dt>库 存:</dt>
<dd><span th:text="${goodsForm.stock}">99</span></dd>
<dt>数 量:</dt>
<dd style="height:32px;">
<div class="chooseAmount mt5">
<a onclick="subNum();" href="javascript:void(0)"></a>
<input type="text" name="count" id="count" class="fl inp-t" value="1"/>
<a onclick="addNum();" href="javascript:void(0)"></a>
</div>
</dd>
</dl>
<p class="mt20"><button class="btnBlue yh " name="buy">立即购买</button><button class="btnYellow yh" name="add" >加入购物车</button></p>
</div>
</form>
</div>
cartController.java
@RequestMapping(value = "addCart", method = RequestMethod.POST,params="buy") public String alipayConfirmaddCart(Model model, HttpSession session, Device device) { GoodsForm goodsForm=new GoodsForm(); // goodsForm.setType("粮食"); // model.addAttribute("goodsForm", goodsForm); List<GoodsForm> commodityType = goodsService.getType(); goodsForm.setCommodityTypeId(commodityType.get(0).getCommodityTypeId()); model.addAttribute("goodsForm", goodsForm); model.addAttribute("commodityType", commodityType); log.info("确认支付"); CartForm cartForm = new CartForm(); UVO uvo = (UVO)session.getAttribute("UVO"); if (uvo == null || StringUtils.isEmpty(uvo.getGuestId())) { return "redirect:/initGuestLogin"; } cartForm.setGuestId(uvo.getGuestId()); List<CartForm> list = cartService.searchCartList(cartForm); AlipayForm alipayForm = new AlipayForm(); String body = "您购买的商品如下:"; Double price = 0d; for (CartForm item : list) { body = body + "品名:" + item.getCommodityName() +", 数量:"+ item.getCount() +", 总价:"+ String.valueOf(Double.valueOf(item.getCount())*Double.valueOf(item.getRetailPrice())) +";"; price = price + Double.valueOf(item.getCount())*Double.valueOf(item.getRetailPrice()); } alipayForm.setBody(body); alipayForm.setOutTradeNo(list.get(0).getCartId()); // 不满88元加8元邮费 if(price < 88) { price = price + 8; body = body + "(由于本次订单未满88元,加收您邮费8元)"; } alipayForm.setPrice(price.toString()); alipayForm.setReceiveAddress(uvo.getAddress()); alipayForm.setReceiveMobile(uvo.getMobile()); alipayForm.setReceiveName(uvo.getGuestName()); alipayForm.setReceivePhone(uvo.getPhone()); alipayForm.setReceiveZip(uvo.getZip()); String host = env.getProperty("host.web"); alipayForm.setShowUrl(host + "/initCart"); alipayForm.setSubject(body); model.addAttribute("alipayForm", alipayForm); cartForm.setGuestId(uvo.getGuestId()); model.addAttribute("cartList", cartService.searchCartList(cartForm)); if(device.isNormal()) { return "shop/cart/cart-2"; } else { return "mobile/alipay/alipayConfirm"; } } @RequestMapping(value = "addCart", method = RequestMethod.POST,params="add") public String executeAddCart(Model model, HttpSession session, CartForm cartForm, Device device) throws SQLException { log.info("追加购物车"); GoodsForm goodsForm = new GoodsForm(); goodsForm.setCommodityId(cartForm.getCommodityId()); // goodsForm.setType("粮食"); List<GoodsForm> commodityType = goodsService.getType(); goodsForm.setCommodityTypeId(commodityType.get(0).getCommodityTypeId()); model.addAttribute("commodityType", commodityType); model.addAttribute("goodsForm", goodsService.searchGoods(goodsForm)); UVO uvo = (UVO)session.getAttribute("UVO"); if (uvo == null || StringUtils.isEmpty(uvo.getGuestId()) || uvo.getGuestId().length() > 4) { if (uvo == null || StringUtils.isEmpty(uvo.getGuestId()) || "Guest".equals(uvo.getGuestId().substring(0, 5))) { uvo = new UVO(); Date date = new Date(); SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMddHHmmss"); uvo.setUserId("Guest" + dateformat.format(date)); uvo.setGuestId("Guest" + dateformat.format(date)); uvo.setUserName("来宾" + dateformat.format(date)); log.info("匿名购买商品销售页面初始化。"); AlipayForm alipayForm = new AlipayForm(); cartForm.setGuestId(uvo.getGuestId()); alipayForm = cartService.searchAlipay(cartForm); List<CartForm> cartList = new ArrayList<>(); model.addAttribute("cartList", cartList); if (alipayForm == null) { model.addAttribute("message", "库存不够!"); if(device.isNormal()) { return "shop/goods/goodsDetail"; } else { return "mobile/goods/goodsDetail"; } } model.addAttribute("alipayForm", alipayForm); if(device.isNormal()) { return "shop/alipay/guestAlipayConfirm"; } else { return "mobile/alipay/guestAlipayConfirm"; } } } cartForm.setUpdateUser(uvo.getGuestName()); Date date = new Date(); SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); cartForm.setUpdateTime(dateformat.format(date)); cartForm.setGuestId(uvo.getGuestId()); AlipayForm alipayForm = new AlipayForm(); alipayForm = cartService.searchAlipay(cartForm); if (alipayForm == null) { model.addAttribute("cartList", cartService.searchCartList(cartForm)); model.addAttribute("message", "库存不够!"); if(device.isNormal()) { return "shop/goods/goodsDetail"; } else { return "mobile/goods/goodsDetail"; } } boolean result = cartService.addCart(cartForm); if (!result) { throw new SQLException("追加购物车失败!"); } model.addAttribute("cartList", cartService.searchCartList(cartForm)); model.addAttribute("list", cartService.searchAlipayHistoryList(cartForm)); if(device.isNormal()) { return "shop/cart/cart-1"; } else { return "mobile/cart/cartList"; } }