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

本文介绍了一个基于Java的购物车功能实现方案,包括商品列表展示、数量调整、删除操作及下单流程。通过Struts标签库和JSP技术实现前端交互。

 

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>&nbsp;</td>

      </tr>

      <tr>

        <td><table width="98%"  border="0" align="center">

          <tr>

            <td colspan="6">&nbsp;</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>&nbsp;</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;

       }

}

 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值