计算机网站设计源代码

更新时间:2024-06-28 20:22:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

购物车核心代码 在userAction中:

public String userLogin() { String sql=\ Object[] con={userName,userPw}; List userList=userDAO.getHibernateTemplate().find(sql,con); if(userList.size()==0) { this.setMessage(\用户名或密码错误\ this.setPath(\ } else { Map session= ServletActionContext.getContext().getSession(); TUser user=(TUser)userList.get(0); session.put(\ Cart cart=new Cart(); //用户登陆成功后生成购物车,并将其放入到session中。 session.put(\ this.setMessage(\成功登录\ this.setPath(\ } return \ }

在buyAction中:

public String addToCart() { TGoods goods=goodsDAO.findById(goodsId); TOrderItem orderItem=new TOrderItem(); orderItem.setGoods(goods); orderItem.setGoodsQuantity(quantity); Map session= ServletActionContext.getContext().getSession(); Cart cart = (Cart)session.get(\ //将session中的cart取出,也就是上边存储在session中的cart cart.addGoods(goodsId, orderItem); session.put(\ //再把cart放入session //this.setMessage(\ this.setPath(\ return \ }

public String myCart() //获取我的购物车 {

return ActionSupport.SUCCESS; }

public String orderQueren() //确认提交订单 { Map request=(Map)ServletActionContext.getContext().get(\ return ActionSupport.SUCCESS; }

public String orderSubmit() //订单提交 { Map session= ServletActionContext.getContext().getSession(); Cart cart = (Cart)session.get(\ //将cart取出 TUser user=(TUser)session.get(\ //将用户取出 TOrder order=new TOrder(); order.setOrderBianhao(new

SimpleDateFormat(\Date())); //设定订单编号 order.setOrderDate(new SimpleDateFormat(\hh:mm:ss\ //确认订单的日期 order.setOrderZhuangtai(\ //订单状态默认为未受理 order.setOrderUserId(user.getUserId()); //设定订单的用户ID

order.setOrderJine(cart.getTotalPrice()); //商品的价格 orderDAO.save(order); //保存订单 for (Iterator it = cart.getItems().values().iterator(); it.hasNext();) //把订单里的商品存入数据库 { TOrderItem orderItem = (TOrderItem) it.next(); orderItem.setOrderId(order.getOrderId()); orderItem.setGoodsId(orderItem.getGoods().getGoodsId()); orderItemDAO.save(orderItem); } cart.getItems().clear(); //清空购物车 session.put(\ Map request=(Map)ServletActionContext.getContext().get(\ request.put(\ return ActionSupport.SUCCESS;

}

public String myOrder() //根据用户查询订单 { Map session= ServletActionContext.getContext().getSession(); TUser user=(TUser)session.get(\ //获取用户 String sql=\ List orderList=orderDAO.getHibernateTemplate().find(sql); //调用orderDAO中的find方法,执行sql语句 Map request=(Map)ServletActionContext.getContext().get(\ request.put(\ return ActionSupport.SUCCESS; }

public String orderMana() //订单管理 { String sql=\ List orderList=orderDAO.getHibernateTemplate().find(sql); Map request=(Map)ServletActionContext.getContext().get(\ request.put(\ return ActionSupport.SUCCESS; }

public String orderDel() //用户自己删除订单 { TOrder order=orderDAO.findById(orderId); orderDAO.delete(order); Map session= ServletActionContext.getContext().getSession(); TUser user=(TUser)session.get(\ this.setMessage(\删除成功\ this.setPath(\ //跳转到 myOrder.action

return \ }

public String orderDelByAd() //管理员删除订单 { TOrder order=orderDAO.findById(orderId); orderDAO.delete(order); String sql=\ orderItemDAO.getHibernateTemplate().bulkUpdate(sql); this.setMessage(\删除成功\ this.setPath(\ return \ }

public String orderShouli() //对订单进行受理 { TOrder order=orderDAO.findById(orderId); order.setOrderZhuangtai(\已经受理订单 orderDAO.attachDirty(order); this.setMessage(\受理订单成功\ this.setPath(\ return \ }

public String orderDetail() //订单明细 { String sql=\ List orderItemList=orderItemDAO.getHibernateTemplate().find(sql); for(int i=0;i

在cart.java中 package com.util;

import java.util.HashMap; import java.util.Iterator; import java.util.Map;

import com.model.TGoods; import com.model.TOrderItem;

public class Cart //定义购物车类 {

protected Map items;

public Cart() //定义构造方法 { if (items == null) { items = new HashMap(); } }

public void addGoods(Integer goodsId, TOrderItem orderItem) //添加购物车 { if (items.containsKey(goodsId)) { TOrderItem _orderitem = items.get(goodsId); _orderitem.setGoodsQuantity(_orderitem.getGoodsQuantity()+

orderItem.getGoodsQuantity()); //如果购买商品重复,则加上数量 items.put(goodsId, _orderitem); } else { items.put(goodsId, orderItem); } }

public void delGoods(Integer goodsId) //删除购物车 { items.remove(goodsId); }

public void updateCart(Integer goodsId, int quantity) //更新购物车的数量 { TOrderItem orderItem = items.get(goodsId); orderItem.setGoodsQuantity(quantity); items.put(goodsId, orderItem); }

public int getTotalPrice() //计算总的价格 { int totalPrice = 0; for (Iterator it = items.values().iterator(); it.hasNext();) { TOrderItem orderItem = (TOrderItem) it.next(); TGoods goods = orderItem.getGoods(); int quantity = orderItem.getGoodsQuantity(); totalPrice += goods.getGoodsTejia() * quantity; 数量 } return totalPrice; } }

//单价乘以

public void delGoods(Integer goodsId) //删除购物车 { items.remove(goodsId); }

public void updateCart(Integer goodsId, int quantity) //更新购物车的数量 { TOrderItem orderItem = items.get(goodsId); orderItem.setGoodsQuantity(quantity); items.put(goodsId, orderItem); }

public int getTotalPrice() //计算总的价格 { int totalPrice = 0; for (Iterator it = items.values().iterator(); it.hasNext();) { TOrderItem orderItem = (TOrderItem) it.next(); TGoods goods = orderItem.getGoods(); int quantity = orderItem.getGoodsQuantity(); totalPrice += goods.getGoodsTejia() * quantity; 数量 } return totalPrice; } }

//单价乘以

本文来源:https://www.bwwdw.com/article/3vg3.html

Top