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>
</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">