购物车中"加入购物车”按钮的实现


details.html中

<form action="addCart" th:object="${goodsForm}" method="post" name="goodsForm">

<input type ="hidden"  name="commodityId" th:value="${goodsForm.commodityId}"/>

<div class="col-sm-6">

<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 ">立即购买</button><button class="btnYellow yh">加入购物车</button></p>

</div>

</form>



CartController.java

    @RequestMapping(value = "addCart", method = RequestMethod.POST)

    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/details";

           } 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/details";

   } 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";

    }

    }

点击进入"我的订单"的相关方法

<tbody >
<div th:each="cartsInfo,status:${cartList}">
		<tr>
		<td class="chk"><input type="checkbox" name="checkTest" /></td>
			<td>
			<div class="cont cf">
	<a href="#"><img th:src="@{showImage(pictureId=${cartsInfo.pictureId})}"
	alt="购物车详细情况" class="pic" /></a>
	<h4>
	<span th:text="${cartsInfo.commodityName}"></span>
	</h4>
	<p class=" mt10">
	规格:每<span
													th:text="${#strings.concat(cartsInfo.unit).concat(cartsInfo.specifications)}"></span>
		</p>
		<p>
	品牌:<span th:text="${cartsInfo.brandName}"></span>
	</p>
	</div>
	</td>
	<td><p class="price yh">
	<span th:text="${cartsInfo.retailPrice}"></span>元
	</p></td>
	<td>
	<div class="chooseAmount">
	<a href="javascript:void(0);"
	th:onclick="${#strings.concat('subNum(').concat(cartsInfo.commodityId).concat(')')}"></a>
	<input th:id="${cartsInfo.commodityId}" type="text" class="fl inp-t" value="1" />
	<a href="javascript:void(0);"
	th:onclick="${#strings.concat('addNum(').concat(cartsInfo.commodityId).concat(')')}"></a>
	</div>
		</td>
			<td><p class="price yh">
											<span th:text="${cartsInfo.retailPrice}"></span>元
										</p></td>
									<td><a class="button"
										th:href="@{delCart(cartId=${cartsInfo.cartId},count=${cartsInfo.count},commodityId=${cartsInfo.commodityId})}"><span>删除</span></a></td>
								</tr>
								</div>
							</tbody>

						</table>



加入购物车的数量增加

<td class="chk">                                 
   <input type="hidden" th:name="${#strings.concat('listBean[').concat(status.index).concat('].commodityId')}" th:value="${cartsInfo.commodityId}"/>
<input type="checkbox" th:name="${#strings.concat('listBean[').concat(status.index).concat('].checkArray')}" /></td>


你可能感兴趣的:(购物车中"加入购物车”按钮的实现)