新闻正文

快速用Struts和Hibernate编写的简单shopcart(2)

来源:JAVA天堂  Struts  2007-5-26 01:29:28 网友评论 0 条 字体:[ ] ~我要投稿!
//优化后的getList方法

 public static List getList(HashMap hs) {
  return new ArrayList(hs.values());
 }



 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {

  Integer id = Integer.parseInt(request.getParameter("id"));

  // 将ids_go中点击Id的历史记录中与页面传来的id比较,将相同的元素全delete掉
  List list_check = (List) request.getSession().getAttribute("ids_go");
  int size = list_check.size();
  int h = 0;
  for (int i = 0; i < size; i++) {
   int j = (Integer)list_check.get(h);
   if (j == id) {
    list_check.remove(h);
   }else{
    h++;
   }
  }
  // 覆盖ids_go
  request.getSession().setAttribute("ids_go", list_check);

  // remove容器allIdSumList中和id相等的整个元素,保持delete后继续购物操作的正确性
  List list = (List) request.getSession().getAttribute("allIdSumList");
  for (int i = 0; i < list.size(); i++) {
   int j = ((IdSumPriceNameDTO) list.get(i)).getId();
   if (j == id) {
    list.remove(i);
   }
  }
  // 覆盖allInSumList
  request.getSession().setAttribute("allIdSumList", list);

  // 从session中取出购物的hashmap容器
  HashMap hashmap = (HashMap) request.getSession().getAttribute("shops");
  hashmap.remove(id);
  // 将其remove后的hashmap值覆盖掉上次的hashmap值
  request.getSession().setAttribute("shops", hashmap);
  request.getSession().setAttribute("shop", getList(hashmap));
  
  //执行了delete操作后,将updateAction中的标记flag设置为null
  //delete后,将修改信息的标记去除,还原为无修改操作状态

  String flag = null;
  request.getSession().setAttribute("flags", flag);
  //转向buy.jsp
  return mapping.findForward("b");
 }

}

IdSumPriceNameDTO :(JavaBean)

public class IdSumPriceNameDTO {

 private int id;

 private int sum = 1;

 private float goodsPrice;
 
 private float allPrices;
 
 private String goodsName;

 public IdSumPriceNameDTO() {
  super();
 }
 //id,sum,goodsPrice,goodsName
 public IdSumPriceNameDTO(int id, int sum, float goodsPrice, String goodsName) {
  super();
  this.id = id;
  this.sum = sum;
  this.goodsPrice = goodsPrice;
  this.goodsName = goodsName;
 }

 public String getGoodsName() {
  return goodsName;
 }

 public void setGoodsName(String goodsName) {
  this.goodsName = goodsName;
 }


 public float getAllPrices() {
  return allPrices;
 }
 //注意javabean中set 只有一个参数,定义多个时到jsp页面会出现无法找到getProperty异常
 public void setAllPrices(float goodsPrice) {
  this.allPrices = (float)(this.sum * goodsPrice);
 }
 

 public float getGoodsPrice() {
  return goodsPrice;
 }

 public void setGoodsPrice(float goodsPrice) {
  this.goodsPrice = goodsPrice;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public int getSum() {
  return sum;
 }

 public void setSum(int sum) {
  this.sum = sum;
 }

}

index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="WEB-INF/struts-logic.tld" prefix="logic"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body  bgcolor="#CCCCCC">
<table width="315" border="1">
  <tr>
     <td colspan="2"><img src="1140438022_min.jpg" width="160" height="120"></td>


     <td colspan="2"><img src="1140437663_min.jpg" width="160" height="120"></td>
     <td colspan="2"><img src="1140438022_min.jpg" width="160" height="120"></td>
  </tr>
  <logic:iterate id="list" name="lister">
    <tr>
      <td><bean:write name="list" property="id" /></td>
      <td width="85"><html:link page="/list.do" paramId="id" paramName="list" paramProperty="id" >在线订购</html:link></td>
    </tr>
  </logic:iterate>
</table>

</body>
</html:html>

buy.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<%@ taglib uri="WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="WEB-INF/struts-logic.tld" prefix="logic"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body bgcolor="#CCCCCC">
<html:form action="/update">
<html:submit value="update" />
<table width="302" height="55" border="1">


 <tr>
     <td width="59">DELETE</td>
  <td width="68">NAME</td>
  <td width="77">PRICE</td>
  <td width="77">NUM</td>
  <td width="77">ALLPRICE</td>
 </tr>
 <logic:iterate id="sh" name="shop">
 <tr>
    <html:hidden  name="sh"  property="id" />
    <html:hidden  name="sh"  property="goodsPrice" />
    <html:hidden  name="sh"  property="goodsName" />
    <td><html:link  page="/delete.do" paramId="id" paramName="sh" paramProperty="id" >delete</html:link></td>
    <td><bean:write name="sh" property="goodsName" /></td>


    <td><bean:write name="sh" property="goodsPrice" /></td>
    <td><html:text  name="sh"  property="sum" /></td>
    <td><bean:write name="sh" property="allPrices" /></td>
 </tr>  
 </logic:iterate>
</table>
</html:form>
<html:link page="/shop.do">去收银台</html:link><html:link page="/index.do">继续购物</html:link>

</body>
</html:html>





模块主要是围绕hashmap容器和list容器编程,并用session会话保持整个操作的记录,其中需要注意的一些问题总结如下:
       
        (1)用户没有进行修改操作时,点击一件商品后,继续购物,点击相同商品时sum++控制。 

        (2)用户没有进行修改操作时,点击一件商品后,继续购物,点击新商品时sum=1控制。

         (3)用户没有进行修改操作时,delete某件商品信息清单时,注意将allIdSumList容器(在session范围内)中元素的id属性对应的 整个元素删除操作,并将“ids_go”(session范围内)中和此次delete操作带的商品id比较,将相同的id在“ids_go”(list 容器)中全部删除,保证用户成功执行delete操作,返回继续购物时还原为初始状态。

        (4)用户进行修改操作时,将 allIdSumList容器中的元素属性sum和allprices保持同步更新,并设置一个标志位flag="update",标志是经过修改数量的 操作,同时当修改操作后继续在进行修改操作时将idSum_list中的数据clear(),保证下次修改时是全新的数据,主要原因是考虑到list对于 重复的元素没有覆盖功能。



        (5)用户进行删除操作后,将updateAction中的标记flag设置为null(flag用来标记用户是否有过update操作,session范围内有效)


备注:对于ListAction中的 test_ListCheck方法的一点想法
 public static List test_ListCheck(List list, int check_id, float _p,String _n) {
  for (int i = 0; i < list.size(); i++) {
   int j = ((IdSumPriceNameDTO) list.get(i)).getId();
   int k = ((IdSumPriceNameDTO) list.get(i)).getSum();
   if (j == check_id) {
    k++;
    list.remove(i);
    IdSumPriceNameDTO idSumDTO = new IdSumPriceNameDTO(j, k, _p,_n);
    list.add(i, idSumDTO);
   }
  }
  return list;
 }

对 于j == check_id相等的条件时,java中好象没办法实现直接对((IdSumPriceNameDTO) list.get(i)).getSum()的值进行++操作,这里想到了c++的话就可以用指针来实现对其++操作了!目前c++代码正在构思,大家也 可以一起想想c++的实现!高手们可以提出好的意见哦!
 

收藏到ViVi   收藏此页到365Key
上一篇:快速用Struts和Hibernate编写的简单shopcart
下一篇:J2EE三剑客:JSF1.2 +Spring2.0 +Hibernate3.2开发应用(1)
用户名:新注册) 密码: 匿名评论 [所有评论]
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
本栏搜索
  • Google
   网站首页 -  网站地图 -  技术学习 -  网站投稿 -  帮助中心
Copyright 2003-2008 www.javah.net All Rights Reserved
2008 如果你喜欢本站 请收藏本站 并推荐给你的朋友一起分享