SSH(struts+spring+hibernate)迅速开发--第八章 浏览和选购商品(3)

本文介绍了一个基于Struts框架的ShopAction类实现,该类实现了商品分页查询、购物车商品显示、添加商品到购物车等功能。通过具体的代码示例展示了如何在Web应用中管理和操作商品数据。

 

同样,我们也产生一个ShopAction类,里面主要有如下几个功能的方法:

a.       分页查询商品列表

b.       显示购物车中的商品

c.       往购物车中添加商品

d.       改变某商品在购物车中的数量

e.       从购物车中清除某商品

具体代码清单如下:

/*

 * Generated by MyEclipse Struts

 * Template path: templates/java/JavaClass.vtl

 */

package cn.com.book.demo.struts.action;

 

import java.util.ArrayList;

import java.util.List;

import java.util.Map;

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.actions.DispatchAction;

 

import cn.com.book.demo.hibernate.dao.Shop;

import cn.com.book.demo.services.PrepareShop;

import cn.com.book.demo.services.ShopServices;

import cn.com.book.demo.services.ShoppingCar;

import cn.com.book.demo.services.impl.ShopServicesImpl;

import cn.com.book.demo.struts.form.ShopForm;

import cn.com.book.demo.struts.util.Constants;

 

/**

 * MyEclipse Struts

 * Creation date: 12-11-2007

 *

 * XDoclet definition:

 * @struts.action path="/shop" name="shopForm" scope="request"

 */

public class ShopAction extends DispatchAction {

      

       private ShopServices shopService = null;

       private final int pageSize = 10;

       /*

        * Generated Methods

        */

 

       /**

        * 分页查询商品列表

        * @param mapping

        * @param form

        * @param request

        * @param response

        * @return ActionForward

        */

       public ActionForward find(ActionMapping mapping, ActionForm form,

                     HttpServletRequest request, HttpServletResponse response) {

              ShopForm shopForm = (ShopForm) form;// TODO Auto-generated method stub

              this.initServices();

              String path = "success";

             

              int targetPage = shopForm.getTargetPage();

              String shopName = shopForm.getShopName();

              Map shopMsgMap = this.shopService.findShopsMsg(shopName, targetPage, pageSize);

             

              List shopList = (List)shopMsgMap.get(ShopServices.SHOP_LIST);

              int totalPage = ((Integer)shopMsgMap.get(ShopServices.PAGE_COUNT)).intValue();

             

              shopForm.setShops(shopList);

              shopForm.setTotalPage(totalPage);

              shopForm.setHasFirst(targetPage>1);

              shopForm.setHasPrev(targetPage>1);

              shopForm.setHasNext(targetPage<totalPage);

              shopForm.setHasLast(targetPage<totalPage);

              //System.out.println(shopForm.isHasLast());

             

              return mapping.findForward(path);

       }

      

       /**

        * 显示购物车中的商品

        * */

       public ActionForward displayAddedShops(ActionMapping mapping, ActionForm form,

                     HttpServletRequest request, HttpServletResponse response) {

              ShopForm shopForm = (ShopForm) form;// TODO Auto-generated method stub

             

              ShoppingCar shopCar = this.initShoppingCar(request);

              int[] shopIds = shopCar.getShops();

              //System.out.println("size=" + shopIds.length);

              if(shopIds != null && shopIds.length>0){

                     this.initServices();

                     List shops = new ArrayList();

                     shopForm.setShops(shops);

                     PrepareShop preShop = null;

                     Shop shop = null;

                     for(int i=0; i<shopIds.length; i++){

                            shop = this.shopService.findShopById(shopIds[i]);

                            if(shop != null){

                                   preShop = new PrepareShop();

                                   preShop.setPrice(shop.getPrice());

                                   preShop.setShopId(shop.getId());

                                   preShop.setShopName(shop.getName());

                                   preShop.setAmount(shopCar.getAmount(shopIds[i]));

                                   preShop.setTotalPrice(preShop.getPrice()*preShop.getAmount());

                                  

                                   shops.add(preShop);

                            }

                            i++;

                     }

              }

              return mapping.findForward("displayAddedShops");

       }    

      

       /**

        * 往购物车里面添加商品

        * */

       public ActionForward addShop(ActionMapping mapping, ActionForm form,

                     HttpServletRequest request, HttpServletResponse response) {

              ShopForm shopForm = (ShopForm) form;// TODO Auto-generated method stub

              int shopId = shopForm.getShopId();

              ShoppingCar shopCar = this.initShoppingCar(request);

              shopCar.addShop(shopId);

              //System.out.println("add a shop");

              return mapping.findForward("addShop");

       }

      

       /**

        * 改变商品在购物车中的数量

        * */

       public ActionForward changeShopAmount(ActionMapping mapping, ActionForm form,

                     HttpServletRequest request, HttpServletResponse response) {

              ShopForm shopForm = (ShopForm) form;// TODO Auto-generated method stub

              int shopId = shopForm.getShopId();

              int shopAmount = shopForm.getShopAmount();

              ShoppingCar shopCar = this.initShoppingCar(request);

              shopCar.change(shopId, shopAmount);

             

              return mapping.findForward("changeShopAmount");

       }

      

       /**

        * 删除购物车中的商品

        * */

       public ActionForward deleteShop(ActionMapping mapping, ActionForm form,

                     HttpServletRequest request, HttpServletResponse response) {

              ShopForm shopForm = (ShopForm) form;// TODO Auto-generated method stub

              int shopId = shopForm.getShopId();

              ShoppingCar shopCar = this.initShoppingCar(request);

              shopCar.removeShop(shopId);

             

              return mapping.findForward("deleteShop");

       }

      

       private ShoppingCar initShoppingCar(HttpServletRequest request){

              ShoppingCar shopCar = null;

              Object shopCarObj = request.getSession().getAttribute(Constants.SHOPPING_KEY);

              if(shopCarObj != null){

                     shopCar = (ShoppingCar)shopCarObj;

              }else{

                     shopCar = new ShoppingCar();

                     request.getSession().setAttribute(Constants.SHOPPING_KEY, shopCar);

              }

              return shopCar;

       }

      

       private void initServices(){

              if(this.shopService == null){

                     this.shopService = new ShopServicesImpl();

              }

       }

 

       public void setShopService(ShopServices shopServices) {

              this.shopService = shopServices;

       }

}

 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值