购物车模块总结2

1 前台业务功能

流程:首先产品页面 里面的购买链接 /shopping/cart.do,然后跳转至该对应的类BuyCartAction类进行处理

用formbean收集信息

public class BuyCartForm extends BaseForm {
    private Integer productid;
    private Integer styleid;
    private String directUrl;

}

然后BuyCartAction类处理代码

/**
 * 购物车处理
 */
@Controller("/shopping/cart")
public class BuyCartAction extends Action {
    @Resource ProductInfoService infoService;

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        BuyCartForm formbean = (BuyCartForm)form;
        BuyCart cart = WebUtil.getBuyCart(request); //从session中取得购物车
        if(cart == null ){
            cart = new BuyCart();
            request.getSession().setAttribute("buyCart", cart);
        }

        //往cookie里面添加SessionID,这样可以解决不同浏览器共享Session的问题,时间和Session的生命周期一样

        WebUtil.addCookie(response, "JSESSIONID", request.getSession().getId(), request.getSession().getMaxInactiveInterval());
        if(formbean.getProductid()!=null && formbean.getProductid()>0){ //往购物车里面添加商品
            ProductInfo product = infoService.find(formbean.getProductid()); //处理用户的非法输入
            if(product!=null){//下面处理产品的样式,只保留用户选择的样式
                ProductStyle currentStyle = null;
                for(ProductStyle style : product.getStyles()){
                    if(style.getId().equals(formbean.getStyleid())){
                        currentStyle = style;
                        break;
                    }
                }
                product.getStyles().clear();
                product.addProductStyle(currentStyle);
            }
            BuyItem item = new BuyItem(product);//把商品添加进购物车
            cart.addBuyItem(item);
        }        
        return mapping.findForward("buyCart");
    }
}

在struts.xml文件中保存对应的跳转页面

         <!--购物车 -->
        <action path="/shopping/cart" name="buyCartForm" scope="request">
            <forward name="buyCart" path="/WEB-INF/page/shopping/cart.jsp"/>
        </action>

cart.jsp显示页面中显示购物项:

  <!-- loop begin -->
  <c:forEach items="${buyCart.items}" var="item">
       <TR vAlign="top">
        <TD><STRONG><A href="" target="_blank">${item.product.name}</A></STRONG> <span class="h3color">[颜色/样式:<c:forEach items="${item.product.styles}" var="style">${style.name}</c:forEach>]</span><BR><BR></TD>
        <TD width="112" align="center"><SPAN class="price" title="¥${item.product.marketprice}元"><FONT color="black"><S><B>¥${item.product.marketprice}元</B></S></FONT></SPAN></TD>
        <TD width="181"><P align="center"><SPAN class="price"><B>¥${item.product.sellprice} 元</B></SPAN> <BR>
          为您节省:<SPAN class=price>¥${item.product.savedPrice}元 </SPAN><BR> </P></TD>
        <TD align="middle" width="73"><INPUT type="text" style="WIDTH: 30px" maxLength="3" value="${item.amount}"  name="amount_${item.product.id}_<c:forEach items="${item.product.styles}" var="style">${style.id}</c:forEach>" onkeypress="javascript:InputIntNumberCheck()"></TD>
        <TD align="middle" width="66"><a href="<html:rewrite action="/shopping/cart/manage" />?method=delete&directUrl=${param.directUrl }&buyitemid=${item.product.id}-<c:forEach items="${item.product.styles}" var="style">${style.id}</c:forEach>"><img height="17" src="/images/buy/delete.gif" width="45" border="0"></a></TD>
      </TR>
      <TR vAlign="top">
        <TD colSpan="5"><IMG height=1 src="/images/buy/green-pixel.gif" width="100%" border="0"></TD>
      </TR>
  </c:forEach>
  <!-- loop end -->   
代码注释

<TD align="middle" width="66"><a href="<html:rewrite action="/shopping/cart/manage" />?method=delete&directUrl=${param.directUrl }&buyitemid=${item.product.id}-<c:forEach items="${item.product.styles}" var="style">${style.id}</c:forEach>"><img height="17" src="/images/buy/delete.gif" width="45" border="0"></a></TD>

该代码用于删除一条购物项:

对应1-1,前面表示产品ID,后面为样式ID,用于确定一个购物项

在class BuyCartForm类中有直接方法对buyitemid进行拆开,一个参数可以保存两个值

    public void setBuyitemid(String buyitemid) {
        if(buyitemid!=null){
            String[] ids = buyitemid.split("-");
            if(ids.length==2){
                productid = new Integer(ids[0]);
                styleid = new Integer(ids[1]);
            }
        }
    }

然后就是购物车页面中的删除购物项,清空购物车,修改数量等等操作,对应的类为BuyCartManageAction类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值