package com.leonard.actions;
import java.util.*;
import java.util.Map.Entry;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.actions.MappingDispatchAction;
import com.leonard.actionforms.*;
import com.leonard.entity.*;
import com.leonard.service.CartService;
public class CartAction extends MappingDispatchAction {
public ActionForward input(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
CartService cs = new CartService();
List<Product> productList = cs.getProducts();
if(productList==null)
{
ActionErrors errors = new ActionErrors();
ActionMessage msg = new ActionMessage("cart.non");
errors.add("loginerror", msg);
saveErrors(request, errors);
}
else
{
request.getSession().setAttribute("productList", productList);
}
return mapping.findForward("to");
}
public ActionForward addProduct(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ItemForm itf = (ItemForm)form;
Cart cart = (Cart)request.getSession().getAttribute("cart");
if(cart==null)
{
cart = new Cart();
}
List<Product> productList = (List)request.getSession().getAttribute("productList");
if(productList!=null)
{
Iterator<Product> pl = productList.iterator();
while(pl.hasNext())
{
Product pr = pl.next();
if(pr.getId()==Integer.parseInt(itf.getPid()))
{
cart.addItem(pr, 1);
break;
}
}
}
request.getSession().setAttribute("cart", cart);
return mapping.findForward("goback");
}
public ActionForward clearCart(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
request.getSession().removeAttribute("cart");
return mapping.findForward("next");
}
public ActionForward affordCart(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginForm lf = (LoginForm)form;
Cart cart = (Cart)request.getSession().getAttribute("cart");
User user = (User)request.getSession().getAttribute("user");
Order order = new Order();
HashMap<Integer,Item> map = cart.getItems();
Set<Entry<Integer, Item>> set = map.entrySet();
Iterator it = set.iterator();
order.setStatus(new Integer(1).shortValue());
//产生新USER 用来保存USER
User newUser = new User();
newUser.setId(user.getId());
newUser.setPassword(user.getPassword());
newUser.setName(user.getName());
newUser.setOrders(user.getOrders());
newUser.setPostcode(user.getPostcode());
newUser.setOffice_phone(user.getOffice_phone());
newUser.setHome_phone(user.getHome_phone());
newUser.setEmail(user.getEmail());
newUser.setCell_phone(user.getCell_phone());
newUser.setAddress(user.getAddress());
user.setPostcode(lf.getPostcode());
user.setOffice_phone(lf.getOffice_phone());
user.setHome_phone(lf.getHome_phone());
user.setEmail(lf.getEmail());
user.setCell_phone(lf.getCell_phone());
user.setAddress(lf.getAddress());
//用户和订单的双向关系
user.getOrders().add(order);
order.setUser(user);
double total = 0;
while(it.hasNext())
{
Entry<Integer,Item> en = (Entry<Integer, Item>) it.next();
Item item = en.getValue();
//订单和产品条目的双向关系
order.getItems().add(item);
item.setOrder(order);
total+=item.getCost();
}
order.setCost(total);
CartService cs = new CartService();
int i = cs.saveOrder(order,user);
if(i>0)
{
request.setAttribute("orderid", i);
request.getSession().setAttribute("user", user);
request.getSession().removeAttribute("cart");
}
else
{
request.getSession().setAttribute("user", newUser);
}
return mapping.findForward("next");
}
public ActionForward gotobuy(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
User user = (User)request.getSession().getAttribute("user");
if(user==null)
{
request.getSession().setAttribute("buy", "buy");
return mapping.findForward("tologin");
}
return mapping.findForward("tobuy");
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
.rar (75个子文件)
shoppingcard
.project 1KB
.mymetadata 309B
WebRoot
WEB-INF
validation.xml 2KB
.struts-config.mex 8KB
tiles-defs.xml 2KB
web.xml 1KB
struts-config.xml 4KB
validator-rules.xml 13KB
lib
classes
com
leonard
actions
CartAction.class 6KB
CartOPAction.class 4KB
UserAction.class 4KB
dao
ICartDao.class 238B
IUserDao.class 333B
CartDaoImp.class 2KB
UserDaoImp.class 2KB
actionforms
LoginForm.class 2KB
ItemForm.class 1KB
transaction
UTransaction.class 1014B
resource
Messages.properties 909B
service
UserService.class 2KB
CartService.class 2KB
entity
Cart.class 3KB
Order.class 2KB
User.class 3KB
Product.hbm.xml 538B
User.hbm.xml 915B
Order.hbm.xml 778B
Item.hbm.xml 704B
Item.class 1KB
Product.class 1KB
util
USessionFactory.class 728B
hibernate.cfg.xml 1KB
index.jsp 854B
META-INF
MANIFEST.MF 39B
tilespage
login.jsp 972B
head.jsp 546B
layout
templet.jsp 644B
lookcart.jsp 3KB
regist.jsp 2KB
image
bg.jpg 28KB
ordersuccess.jsp 1KB
buycart.jsp 3KB
footer.jsp 173B
menu.jsp 925B
product.jsp 924B
lookorder.jsp 3KB
exception.jsp 292B
.myeclipse
src
com
leonard
actions
UserAction.java 3KB
CartAction.java 4KB
CartOPAction.java 3KB
dao
UserDaoImp.java 822B
ICartDao.java 185B
IUserDao.java 196B
CartDaoImp.java 624B
actionforms
ItemForm.java 615B
LoginForm.java 2KB
transaction
UTransaction.java 447B
resource
Messages.properties 909B
service
UserService.java 2KB
CartService.java 1KB
entity
Order.java 889B
Product.hbm.xml 538B
User.java 2KB
Item.java 822B
User.hbm.xml 915B
Order.hbm.xml 778B
Item.hbm.xml 704B
Product.java 709B
Cart.java 2KB
util
USessionFactory.java 333B
hibernate.cfg.xml 1KB
.mystrutsdata 262B
.classpath 683B
.myhibernatedata 439B
mysql.sql 5KB
共 75 条
- 1
资源评论
fanwander0711
- 粉丝: 4
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功