/**
* 购物车
*/
public ActionForward cart(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
//此处记得保存rootid
// 获取Cookie
String pids="";
Cookie[] cookies = request.getCookies();
for(Cookie cookie:cookies){
String cookiename = cookie.getName();
System.out.println("cookie.getName()->"+cookiename);
if (cookiename.equals("shopcart")) {
pids=cookie.getValue();
System.out.println("cookie.getValue()->"+pids);
break;
}
}
pids=pids.replaceAll("%7C", "|");
pids=pids.replaceAll("%2C", ",");
System.out.println(pids);
//解析得到商品类别数据结构
Hashtable<Integer,Integer> p = new Hashtable<Integer,Integer>();
Hashtable<Integer,Integer> pl = new Hashtable<Integer,Integer>();
String pkey=null;
String pnum=null;
String po = null;
if(pids !=null){
StringTokenizer st = new StringTokenizer(pids,"|");
while(st.hasMoreTokens()){
String s = st.nextToken();
StringTokenizer st2 = new StringTokenizer(s,",");
int idx=0;
while(st2.hasMoreTokens()){
String ss = st2.nextToken();
//商品的ID
if (idx==0) {
pkey=ss;
System.out.println(ss);
}else if (idx==2) {
pnum=ss;
System.out.println(ss);
}else{
po = ss;
}
idx++;
}
//保存到数据结构
p.put(Integer.valueOf(pkey), Integer.valueOf(pnum));
pl.put(Integer.valueOf(pkey), Integer.valueOf(po));
}
//循环完毕得到所有的商品ID与对应的数量
}
//查询商品并结算金额
List<ECartProduct> lstr = new Vector<ECartProduct>();
//ProductDAO daop = new ProductDAO();
Double total = 0d;
Enumeration<Integer> en = p.keys();
while(en.hasMoreElements()){
Integer id = en.nextElement();
//访问DAO得到商品信息
ECartProduct pt = daop.getById(id);
pt.setCount(p.get(pt.getId()));
pt.setTotal(pt.getPrice()*pt.getCount());
System.out.println("pt.getTotal()->"+pt.getTotal());
pt.setCid(pl.get(pt.getId()));
//计算单价,并累加
if (pt!=null) {
lstr.add(pt);
total = total+pt.getTotal();
}
}
ECarts cart = new ECarts(lstr,total);
request.setAttribute("book", cart.getCart());
request.setAttribute("amount", cart.getTotal());
return mapping.findForward("cart");
}
/**
* 订单结算
*/
public ActionForward checkOut(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
//此处记得保存rootid
String uid = request.getParameter("uid");
String total = request.getParameter("total");
return mapping.findForward("checkout");
}
分享到:
相关推荐
代码欣赏代码欣赏代码欣赏代码欣赏代码欣赏代码欣赏代码欣赏代码欣赏代码欣赏代码欣赏代码欣赏代码欣赏代码欣赏
源代码欣赏oms源代码欣赏oms 源代码欣赏oms源代码欣赏oms
java_fqs源代码欣赏
LUA进阶源代码欣赏是新手和老人必看的LUA语言进阶代码
欣赏优美的代码
Apollo 11号登月(1969年人类第一次登月)控制系统源代码欣赏
一些基本编程元素,数据结构,高级控制流程
汇编代码,被写进教科书,雷军也是牛人程序员
栈的基本操作,任何用户都可以使用此栈的接口,只要用户在主代码中将USERTYPE型定义即可
汇编程序欣赏 有很多例子
最近有一点空闲,想续写我那本 Lua 源码欣赏。按我心里的计划,还有大约 6 章。虚拟机、字节码持久化、C API 、解释器、GC、库函数。 新添了一章关于虚拟机的,所以重新读了一遍相关源码。发现 Lua 5.2 比上一版...
汉诺塔代码欣赏 及加工修改 汉诺塔在递归中的实现过程
jsp简单的登陆界面代码希望大家欣赏,没有数据库连接
内容概要: 这段浪漫代码是通过HTML、CSS和JavaScript编写的,通过...- 希望有机会学习和欣赏您精心制作的代码,感受到代码的魅力和浪漫的力量。 - 请注意,在某些浏览器中可能需要用户与页面进行交互才能播放烟花声音。
古诗词欣赏学生网页设计制作作业,选修课的作业,适合交作业
欣赏并分析1987年,贝尔实验室的David Korn提交的获奖代码。
单片机课程设计——创意LOVE彩灯欣赏(PROTEUS仿真+代码).zip
VB源代码\国际程序设计大赛的作品欣赏.rarVB源代码\国际程序设计大赛的作品欣赏.rarVB源代码\国际程序设计大赛的作品欣赏.rar
Javascript绝句欣赏 一些经典的js代码.docx
全球DEMO大赛得奖作品欣赏(65K超小超炫的3D作品)