天天看點

購物車子產品總結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" rel="external nofollow" 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>" οnkeypress="javascript:InputIntNumberCheck()"></TD>

        <TD align="middle" width="66"><a href="<html:rewrite action=" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" /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" ></a></TD>

      </TR>

      <TR vAlign="top">

        <TD colSpan="5"><IMG height=1 src="/images/buy/green-pixel.gif" width="100%" ></TD>

      </TR>

  </c:forEach>

  <!-- loop end -->   

代碼注釋

<TD align="middle" width="66"><a href="<html:rewrite action=" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" /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" ></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類