您的位置:首页 > 编程语言 > Java开发

SSH(struts+spring+hibernate)迅速开发--第八章 浏览和选购商品(5)

2008-01-24 14:37 579 查看
i) shopCar.jsp页面主要实现购物车中商品列表的显示、改变购物车中某商品的数量、删除购物车中的某类商品、重新进入商品列表进行选购以及转入下单界面下单,代码清单如下:
<%@ page language="java" pageEncoding="UTF-8"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><bean:message bundle="shop" key="shop.car"/></title>
<script type="text/javascript">
function selectMore(){
document.location.href="shop.do?method=find";
}
function toDelete(url){
document.location.href=url;
}
function toChange(url,obj){
//alert(obj.value);
document.location.href=url + obj.value;
}
function toSubmitOrder(){
document.location.href="order.do?method=toInitPage";
}
</script>
</head>

<body>
<html:form action="/shop.do">
<table width="80%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td> </td>
</tr>
<tr>
<td><table width="98%" border="0" align="center">
<tr>
<td colspan="6"> </td>
</tr>
<tr>
<td width="11%"><div align="center"><strong><bean:message bundle="shop" key="shop.seq.no"/></strong></div></td>
<td width="29%"><div align="center"><strong><bean:message bundle="shop" key="shop.name"/></strong></div></td>
<td width="14%"><div align="center"><strong><bean:message bundle="shop" key="shop.price"/></strong></div></td>
<td width="17%"><div align="center"><strong><bean:message bundle="shop" key="shop.amount"/></strong></div></td>
<td width="17%"><div align="center"><strong><bean:message bundle="shop" key="shop.total.price"/></strong></div></td>
<td width="12%"><div align="center"><strong><bean:message bundle="shop" key="shop.operate"/></strong></div></td>
</tr>
<logic:notEmpty name="shopForm" property="shops">
<logic:iterate id="shop" name="shopForm" property="shops" indexId="index">
<tr>
<td>${index+1}</td>
<td><bean:write name="shop" property="shopName"/></td>
<td><bean:write name="shop" property="price"/></td>
<td><div align="center">
<input name="amout${index+1}" type="text" size="6" value='<bean:write name="shop" property="amount"/>' onChange="toChange('shop.do?method=changeShopAmount&shopId=${shop.shopId}&shopAmount=',this)">
</div></td>
<td><bean:write name="shop" property="totalPrice"/></td>
<td><div align="center">
<input type="button" name="delete${index+1}" value='<bean:message bundle="shop" key="shop.delete"/>' onClick="toDelete('shop.do?method=deleteShop&shopId=${shop.shopId}')">
</div></td>
</tr>
</logic:iterate>
</logic:notEmpty>
<tr>
<td colspan="6"><div align="center">
<input type="button" name="submitOrder" value='<bean:message bundle="shop" key="shop.submit.order"/>' onclick="toSubmitOrder()">
<input type="button" value='<bean:message bundle="shop" key="shop.select.shop"/>' onclick="selectMore()">
</div></td>
</tr>
</table></td>
</tr>
<tr>
<td> </td>
</tr>
</table>
</html:form>
<br>
</body>
</html>

1. 其它用到的类
购物车类清单:
/**
*
*/
package cn.com.book.demo.services;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
* @author Noble.Yang
*
*/
public class ShoppingCar {
//购物车中保存的是商品编号和对应的商品数量
private Map shops = new HashMap();

/**
* 往购物车中添加一个商品(编号)
* */
public void addShop(int shopId) {
this.addShop(shopId, 1);
}

/**
* 往购物车中添加一定数量的某个商品
* */
public void addShop(int shopId, int amount) {
//System.out.println("*****************");
String shopKey = String.valueOf(shopId);
Object objAmount = this.shops.get(shopKey);
Integer shopAmount = null;
if (objAmount != null) {
shopAmount = new Integer(((Integer) objAmount).intValue() + amount);
} else {
shopAmount = new Integer(amount);
}
//System.out.println("shop:" + shopKey + " amount:" + shopAmount);
this.shops.put(shopKey, shopAmount);
}

/**
* 更改购物车中某商品的数量
* */
public void change(int shopId, int amount){
String shopKey = String.valueOf(shopId);
this.shops.put(shopKey, new Integer(amount));
}

/**
* 全部清除某个商品
* */
public void removeShop(int shopId) {
this.shops.remove(String.valueOf(shopId));
}

/**
* 减少购物车中某个商品的数量
* */
public void removeShop(int shopId, int amount) {
String shopKey = String.valueOf(shopId);
Object objAmount = this.shops.get(shopKey);
int shopAmount = 0;
if (objAmount != null) {
shopAmount = ((Integer) objAmount).intValue() - amount;
if (shopAmount <= 0) {
this.shops.remove(shopKey);
} else {
this.shops.put(shopKey, new Integer(shopAmount));
}
}
}

/**
* 清空购物车
* */
public void clear(){
this.shops.clear();
}

/**
* 获取购物车中所有的商品以及对应的数量
* */
public int[] getShops() {
int[] shops = new int[this.shops.size() * 2];
if (this.shops.size() > 0) {
Set keys = this.shops.keySet();
Iterator iterator = keys.iterator();
if (iterator != null) {
int index = 0;
String key = null;
while(iterator.hasNext()){
key = (String)iterator.next();
shops[index] = Integer.parseInt(key);
index++;
shops[index] = ((Integer)this.shops.get(key)).intValue();
index++;
}
}
}
return shops;
}

public int getAmount(int shopId){
int amount = 0;

String shopKey = String.valueOf(shopId);
Object amountObj = this.shops.get(shopKey);
if(amountObj != null){
amount = ((Integer)amountObj).intValue();
}

return amount;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐