package com.softeem.jingdong.action; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; import com.softeem.jingdong.entity.GoodsBean; import com.softeem.jingdong.entity.ShoppingCarBean; import com.softeem.jingdong.entity.UsersBean; import com.softeem.jingdong.service.GoodsService; import com.softeem.jingdong.service.ShoppingCarService; /*** * 商品信息 * @author Administrator * */ public class GoodsAction extends ActionSupport { private static final long serialVersionUID = 8979942382300596390L; private GoodsService goodsService; private ShoppingCarService scs; private int id; private List<GoodsBean> list = new ArrayList<GoodsBean>(); private GoodsBean goods = new GoodsBean(); private String key; /** * 用户首页商品列表 */ public String getGoodsList(){ //调用方法获取商品集合 list = goodsService.getAllGoods(); return SUCCESS; } /** * 查看商品详细信息 * @return */ public String getGoodsInfo(){ goods = goodsService.getGoodsById(id); return SUCCESS; } /** * 购买商品 * @return */ public String buyGoods(){ //获取 Session HttpSession session = ServletActionContext.getRequest().getSession(true); //获取用户 UsersBean users = (UsersBean)session.getAttribute("users"); //获取用户的购物车信息 ShoppingCarBean shoppingCar = users.getShoppingCar(); //将商品添加到购物车里面 GoodsBean goods = scs.addGoodsToShoppingCar(id); shoppingCar.getList().add(goods); //设置购物车总价 shoppingCar.setSumPrice( this.getShoppingCarSumPrice( shoppingCar.getList() ) ); //设置商品总数量 shoppingCar.setNumber( shoppingCar.getList().size() ); return SUCCESS; } /** * 求购物车总价 */ public double getShoppingCarSumPrice(List<GoodsBean> list){ double sum = 0; for(GoodsBean g : list){ sum += g.getPrice(); } return sum; } /** * 模糊查询商品 * @return */ public String getSearchGoodsList(){ list = goodsService.getSearchList(key); return SUCCESS; } public List<GoodsBean> getList() { return list; } public void setList(List<GoodsBean> list) { this.list = list; } public int getId() { return id; } public void setId(int id) { this.id = id; } public GoodsBean getGoods() { return goods; } public void setGoods(GoodsBean goods) { this.goods = goods; } public String getKey() { return key; } public void setKey(String key) { this.key = key; } public void setGoodsService(GoodsService goodsService) { this.goodsService = goodsService; } public void setScs(ShoppingCarService scs) { this.scs = scs; } public GoodsService getGoodsService() { return goodsService; } public ShoppingCarService getScs() { return scs; } }

baoligeer LV5
2022年6月6日
任星浩 LV1
2021年11月18日
giantinheart LV3
2021年5月23日
wusiyin LV14
2020年12月17日
曾学成 LV2
2020年12月14日
refreshbird LV1
2020年12月14日
方立亮 LV1
2020年11月6日
渺无人烟 LV11
2020年7月31日
cpemy_abc LV10
2020年6月9日
远大帅比 LV6
2020年5月19日

sn764889012
4月16日
暂无贡献等级
dongandmin LV8
2024年12月31日
mianju
2024年12月19日
暂无贡献等级
lqzixi LV4
2024年11月6日
lyh1989 LV34
2024年7月21日
lyt010628 LV4
2024年7月9日
曾显示 LV6
2024年7月7日
樱花味小然子 LV5
2024年7月2日
微信网友_7034303418945536 LV1
2024年6月12日
chutianyu LV4
2024年6月6日