购物车模块总结3

来源:互联网 发布:八门神器软件下载 编辑:程序博客网 时间:2024/04/29 18:27

BuyCartManageAction类的详细代码:用于处理购物车的修改,删除,清空等功能

中间return mapping.findForward("directUrl");这条语句用来跳转至全局配置的页面

    <global-forwards>
        <forward name="message" path="/WEB-INF/page/share/message.jsp" />
        <forward name="directUrl" path="/WEB-INF/page/share/directUrl.jsp"/>
    </global-forwards>

跳转至directUrl.jsp

该页面使用了<c:redirect url="${directUrl}"/>,再跳转至directUrl对应的页面


@Controller("/shopping/cart/manage")
public class BuyCartManageAction extends DispatchAction {
    /**
     * 清空购物车
     */
    public ActionForward deleteAll(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        BuyCart cart = WebUtil.getBuyCart(request);
        cart.deleteAll();
        BuyCartForm formbean = (BuyCartForm)form;
        String param = formbean.getDirectUrl()!=null && !"".equals(formbean.getDirectUrl()) ? "?directUrl=" + formbean.getDirectUrl() : "";
        request.setAttribute("directUrl", "/shopping/cart.do"+ param);
        return mapping.findForward("directUrl");
    }
    /**
     * 删除购物项
     */
    public ActionForward delete(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        BuyCartForm formbean = (BuyCartForm)form;
        BuyCart cart = WebUtil.getBuyCart(request);
        ProductInfo product = new ProductInfo(formbean.getProductid());
        product.addProductStyle(new ProductStyle(formbean.getStyleid()));
        BuyItem item = new BuyItem(product);
        cart.deleteBuyItem(item);
        String param = formbean.getDirectUrl()!=null && !"".equals(formbean.getDirectUrl()) ? "?directUrl=" + formbean.getDirectUrl() : "";
        request.setAttribute("directUrl", "/shopping/cart.do"+ param);
        return mapping.findForward("directUrl");
    }
    /**
     * 修改购买数量
     */
    public ActionForward updateAmount(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        setAmount(request);
        BuyCartForm formbean = (BuyCartForm)form;
        String param = formbean.getDirectUrl()!=null && !"".equals(formbean.getDirectUrl()) ? "?directUrl=" + formbean.getDirectUrl() : "";
        request.setAttribute("directUrl", "/shopping/cart.do"+ param);
        return mapping.findForward("directUrl");
    }
    /**
     * 修改商品购买数量
     * @param request
     */
    private void setAmount(HttpServletRequest request) {
        BuyCart cart = WebUtil.getBuyCart(request);
        for(BuyItem item : cart.getItems()){
            String paramName = "amount_"+ item.getProduct().getId()+"_"+
                                item.getProduct().getStyles().iterator().next().getId();
            Integer amount = new Integer(request.getParameter(paramName));
            item.setAmount(amount);
        }
    }
    /**
     * 结算
     */
    public ActionForward settleAccounts(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        setAmount(request);
        BuyCartForm formbean = (BuyCartForm)form;
        String url = "/customer/shopping/deliver.do";
        if(formbean.getDirectUrl()!=null && !"".equals(formbean.getDirectUrl())){
            url = new String(Base64.decodeBase64(formbean.getDirectUrl().trim().getBytes()));//获取解码后的url
        }
        request.setAttribute("directUrl", url);
        return mapping.findForward("directUrl");
    }
    
}


原创粉丝点击