vo里面写的ShoppingCar.java类
package com.bt.vo;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class ShoppingCar {
private Map<ProductVO, Integer> map = new HashMap<ProductVO, Integer>();
// 向购物车增加一个产品
public void addProduct(ProductVO vo) {
if (map.containsKey(vo)) {// 判断该物品是否已经在购物车中
int value = map.get(vo);// 如果已经有盖类型的物品,则取出该物品的数量
value++;// 修改数量
map.put(vo, value);
} else {
map.put(vo, 1);// 如果盖物品不存在,则直接加入,数量初始化为1个
}
}
public void clear() {
map.clear();
}
// 从购物车中移除产品
public void remove(Integer id, int num) {
ProductVO key = new ProductVO();// 移除集合中的元素需要key,需要ProductVO作为key
key.setProductid(id);
int value = map.get(key).intValue();// 根据key查找对应的产品数量
if (num < value) {// 判断你要移除的商品个数是否小于购买量
value -= num;// 小于已购买量则减去num
map.put(key, value);// 重新修改产品对应的数量
} else if (num == value) {// 如果要减去该产品所有的购买数
map.remove(key);// 则直接将该产品移除
}
}
public Map<ProductVO, Integer> getMap() {
return map;
}
// 计算购物车总价格
public double getTotal() {
double total = 0.0;
// 需要将每个产品的价格和数量相乘,计算总价格
Iterator<ProductVO> it = map.keySet().iterator();// 列举所有产品的集合
while (it.hasNext()) {
ProductVO vo = it.next();
// 取出该产品的购买数量
int num = map.get(vo).intValue();
// 取出该产品的单价
double price = vo.getBaseprice();
// 计算该类产品的总价格
double sum = price * num;
// 计算购物车所有产品的总价格
total += sum;
}
return total;
}
public static void main(String[] args) {
ProductVO vo1 = new ProductVO();
vo1.setBaseprice(20);
vo1.setProductid(3);
ProductVO vo2 = new ProductVO();
vo2.setBaseprice(20);
vo2.setProductid(3);
ProductVO vo3 = new ProductVO();
vo3.setBaseprice(8);
vo3.setProductid(4);
ProductVO vo4 = new ProductVO();
vo4.setBaseprice(8);
vo4.setProductid(4);
ShoppingCar car = new ShoppingCar();
car.addProduct(vo1);
car.addProduct(vo2);
car.addProduct(vo3);
car.addProduct(vo4);
System.out.println(car.getTotal());
car.remove(4, 1);
System.out.println(car.getTotal());
}
}
action里面
package com.bt.control;
import java.util.Map;
import com.bt.service.ProductService;
import com.bt.util.WebConstant;
import com.bt.vo.ShoppingCar;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ShoppingCarAction extends ActionSupport {
private Integer productid;
private ProductService ps;
public void setPs(ProductService ps) {
this.ps = ps;
}
public Integer getProductid() {
return productid;
}
public void setProductid(Integer productid) {
this.productid = productid;
}
/**
* 每当购物时,就要判断是否已经有购物车了, 如果是第一次购物,就给一个购物车实例,并保存在session中
*
* @return
*/
public String add() {
// 在Action中访问ServletAPI,获取session
Map<String, Object> session = ActionContext.getContext().getSession();
// 判断是否第一次登录,因为只要登录,就会在session中保存该对象的信息
Object obj = session.get(WebConstant.SESSION_CURRENT_USER);
// 准备一个购物车对象
ShoppingCar car = null;
// 如果已经登录
if (obj != null) {
// 判断是否已经有购物车了,如果没有,就实例化一个并保存在session中
Object temp_object = session.get(WebConstant.SESSION_CURRENT_CAR);
// 如果还没有购物车
if (temp_object == null) {
// 实例化一个购物车
car = new ShoppingCar();
// 然后将购物车放入session中,为下次购物做准备
session.put(WebConstant.SESSION_CURRENT_CAR, car);
} else {
// 如果已经有购物车了,则将对象还原为购物车对象
car = (ShoppingCar) temp_object;
}
// 将购买的产品放入购物车,调用ProductService的load方法访问数据库,获得所购书籍的详细信息
car.addProduct(ps.load(productid));
return SUCCESS;// 成功后跳转至shopcart.jsp页面
} else {
return ERROR;// 如果没有登录则重新定位到login.jsp页面
}
}
}
struts_car.xml配置文件 ()页面可以点击购买超链接传过来
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="car" extends="struts-default" namespace="/car">
<action name="add" class="caraction" method="add">
<result name="success">/shopcart.jsp</result>
<result name="error">/login.jsp</result>
</action>
</package>
</struts>
购物车的jsp页面
<%@page import="com.bt.vo.ProductVO"%>
<%@page import="com.bt.vo.ShoppingCar"%>
<%@page import="com.bt.util.WebConstant"%>
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
ShoppingCar car=(ShoppingCar)session.getAttribute(WebConstant.SESSION_CURRENT_CAR);
Iterator<ProductVO> it=car.getMap().keySet().iterator();
%>
<!--文件头开始-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta name="description" content="电子商务门户">
<title>电子商务门户</title>
<LINK href="<%=basePath%>css/test.css" rel=stylesheet>
<script language = "JavaScript" src = "<%=basePath%>js/test.js"></script>
</head>
<body onLoad="MM_preloadImages('<%=basePath%>images/index_on.gif','<%=basePath%>images/reg_on.gif','<%=basePath%>images/order_on.gif','<%=basePath%>images/top/topxmas/jp_on.gif','<%=basePath%>images/top/topxmas/download_on.gif','<%=basePath%>images/top/topxmas/bbs_on.gif','<%=basePath%>images/top/topxmas/designwz_on.gif')" topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0">
<table width="100%" border="0" cellspacing="0" cellpadding="0" id="table2">
<tr>
<td align="left" width="7%" background="<%=basePath%>images/top_bg.gif"><img src="<%=basePath%>images/logo.gif" width="286" height="58"></td>
<td width="62%" background="<%=basePath%>images/top_bg.gif"> </td>
<td width="31%" background="<%=basePath%>images/top_r.gif" align="right">
<a href="<%=basePath%>login.jsp">[登录]</a>
<a href="<%=basePath%>register.jsp">[注册]</a> </td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td background="<%=basePath%>images/dh_bg.gif" align="left" height="12">
<table width="100" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td width="5%"> </td>
<td width="10%"><a href="<%=basePath%>index.jsp" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('<%=basePath%>image1','','<%=basePath%>images/index_on.gif',1)">
<img name="<%=basePath%>image1" border="0" src="<%=basePath%>images/index.gif" width="90" height="36"></a></td>
<td width="10%"><a href="<%=basePath%>user/userinfo.jsp" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('<%=basePath%>image2','','<%=basePath%>images/reg_on.gif',1)">
<img name="<%=basePath%>image2" border="0" src="<%=basePath%>images/reg.gif" width="92" height="36"></a></td>
<td width="10%"><a href="<%=basePath%>shopcart.jsp" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('<%=basePath%>image4','','<%=basePath%>images/carts_on.gif',1)">
<img name="<%=basePath%>image4" border="0" src="<%=basePath%>images/cart.gif" width="92" height="36"></a></td>
<td width="10%"><a href="<%=basePath%>user/order.jsp" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('<%=basePath%>image5','','<%=basePath%>images/order_on.gif',1)">
<img name="<%=basePath%>image5" border="0" src="<%=basePath%>images/order.gif" width="92" height="36"></a></td>
<td width="10%"><a href="<%=basePath%>user/logout.action" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('<%=basePath%>image6','','<%=basePath%>images/exit_on.gif',1)">
<img name="<%=basePath%>image6" border="0" src="<%=basePath%>images/exit.gif" width="92" height="36"></a></td>
</tr>
</table>
</td>
</tr>
</table>
<table cellspacing="1" cellpadding="3" align="center" border="0" width="98%">
<tr>
<td width="65%"><BR>
>> 欢迎访问 <b>电子商务门户</b> </td>
<td width="35%" align="right">
</td></tr></table>
<!--文件体开始-->
<table cellspacing=1 cellpadding=3 align=center class=tableBorder2>
<tr>
<td height=25 valign=middle>
<img src="<%=basePath%>images/Forum_nav.gif" align="absmiddle">
<a href="<%=basePath%>index.jsp">电子商务门户</a> →
<img border="0" src="<%=basePath%>images/dog.gif" width="19" height="18">
购物清单
</td>
</tr>
</table>
<br>
<table cellpadding=3 cellspacing=1 align=center class=tableborder1>
<tr>
<td valign=middle align=center height=25 background="<%=basePath%>images/bg2.gif" width=""><font color="#ffffff"><b>序号</b></font></td>
<td valign=middle align=center height=25 background="<%=basePath%>images/bg2.gif" width=""><font color="#ffffff"><b>产品名称</b></font></td>
<td valign=middle align=center height=25 background="<%=basePath%>images/bg2.gif" width=""><font color="#ffffff"><b>价格</b></font></td>
<td valign=middle align=center height=25 background="<%=basePath%>images/bg2.gif" width=""><font color="#ffffff"><b>数量</b></font></td>
<td valign=middle align=center height=25 background="<%=basePath%>images/bg2.gif" width=""><font color="#ffffff"><b>合计</b></font></td>
<td valign=middle align=center height=25 background="<%=basePath%>images/bg2.gif" width=""><font color="#ffffff"><b>操作</b></font></td>
</tr>
<%
int count=0;
while(it.hasNext()){
ProductVO vo=it.next();
int num=car.getMap().get(vo).intValue();
count++;
%>
<tr>
<form method="post" action="updateAction.do" name="f1">
<input type="hidden" name="productid" value="<%=vo.getProductid()%>">
<input type="hidden" name="number" value="<%=vo.getProductid()%>">
<td class=tablebody2 valign=middle align=center width=""><%=count%></td>
<td class=tablebody1 valign=middle width=""> <a href="<%=basePath%>product/showDetail.action?productid=<%=vo.getProductid()%>"><%=vo.getName()%></a></td>
<td class=tablebody2 valign=middle align=center width="">¥<%=vo.getBaseprice()%></td>
<td class=tablebody1 valign=middle align=center width=""><%=num%></td>
<td class=tablebody2 valign=middle align=left width=""> ¥<%=(vo.getBaseprice()*num)%></td>
<td class=tablebody1 valign=middle align=center width="">
<input type="button" value="取消" onclick="javascript:window.location='removeProductAction.do?productid=2';"> <input type="submit" value="保存修改"></td>
</form>
</tr>
<%}%>
<tr>
<td class=tablebody1 valign=middle align=center colspan="4"> </td>
<td class=tablebody1 valign=middle align=left width=""> <font color="#ff0000"><b><%=car.getTotal()%></b></font></td>
<td class=tablebody1 valign=middle align=center width=""> </td>
</tr>
<tr>
<td class=tablebody2 valign=middle align=center colspan="6"><input type="button" value="提交订单" onclick="javascript:window.location='<%=basePath%>order/create.action';"> <input type="button" value="继续购物" onclick="javascript:window.location='<%=basePath%>index.jsp';"> <input type="button" value="清空购物车" onclick="javascript:window.location='removeAllProductAction.do';"></td>
</tr>
</table><br>
<!--文件尾开始-->
<tab
le width="100%" border="0" cellspacing="0" cellpadding="0" align="center" height="28">
<tr>
<td height="17" background="<%=basePath%>images/bot_bg.gif">
</td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td bgcolor="#f1f1f6" height="53" valign="center">
<p align="center">Copyright ©2004 - 2012 <a href="<%=basePath%>index.jsp"><b><font face="Verdana,">test</font></b><font color=#CC0000>.com.cn</font></b></a><br>
</td>
</tr>
</table>
</body>
</html>