目標(biāo):
l 掌握購物車的信息如何存儲(chǔ);
l 掌握購物車常用功能的實(shí)現(xiàn)。
主要內(nèi)容:
l 首先分析用戶上網(wǎng)購物的一般過程;
l 介紹采用什么樣的數(shù)據(jù)結(jié)構(gòu)存儲(chǔ)購物信息;
l 編寫購物界面;
l 完成向購物車添加物品的功能。
1、 用戶上網(wǎng)購物的一般過程
在瀏覽物品的過程中如果對某件物品感興趣,會(huì)添加到購物車(購物籃)中,隨時(shí)可以查看購物車中的信息,如果不想要某件物品的話,可以刪除,或者修改某種物品的數(shù)量,或者整個(gè)清空購物車,可以繼續(xù)選擇物品向購物車中添加。最后用戶可以購物這些物品,經(jīng)過輸入個(gè)人的送貨地址信息和設(shè)定交易方式之后,可以生成訂單。網(wǎng)站的管理員可以對訂單進(jìn)行管理。
本實(shí)例模擬這個(gè)過程,但是進(jìn)行了簡化:只能在物品列表中選擇物品向購物車中添加。確定購買后,不需要設(shè)置交易方式以及付款等。實(shí)際處理過程,可以使用我們前面介紹的功能完成。
2、 購物車信息組織
因?yàn)樵谟脩粼L問網(wǎng)站的整個(gè)過程中都可以訪問購物車信息,所以購物車對象應(yīng)該存放在session中。
因?yàn)橛脩糍徺I的物品的種類和數(shù)量都不確定,所以需要使用一個(gè)合適的數(shù)據(jù)結(jié)構(gòu)存儲(chǔ),我們選擇ArrayList。
每一種物品都涉及數(shù)量,需要進(jìn)行封裝,把物品和數(shù)量封裝成購物項(xiàng),使用Item,每個(gè)Item對應(yīng)一種物品以及該種物品的數(shù)量。
需要編寫物品類表示物品的基本信息。
參考代碼如下:
2.1 物品類
該類中包含兩個(gè)與分頁顯示相關(guān)的方法。其中用到的DBBean是前面介紹的。
package javabean;
import java.util.ArrayList;
import java.sql.*;
public class Goods
{
private String goodsid;
private String goodsname;
private float price;
// 物品編號(hào)
public void setGoodsid(String goodsid)
{
this.goodsid = goodsid;
}
public String getGoodsid()
{
return goodsid;
}
// 物品名稱
public void setGoodsname(String goodsname)
{
this.goodsname = goodsname;
}
public String getGoodsname()
{
return goodsname;
}
// 物品價(jià)格
public void setPrice(float price)
{
this.price = price;
}
public float getPrice()
{
return price;
}
public ArrayList getGoodsByPage(int pageNo) {
int number = 10;
// 每一頁顯示的記錄數(shù)
int begin = (pageNo * number) - 9;
int end = pageNo * number;
int index = 1;
DBBean db = new DBBean();
// 要返回的結(jié)果對象
ArrayList goods = new ArrayList();
String sql = "select * from goods";
ResultSet rs;
try{
rs = db.executeQuery(sql,null);
while (rs.next()) {
// 在begin之前的記錄是不顯示的
if (index < begin) {
index++;
continue;
}
// 在end之后的記錄也不顯示
if (index > end)
break;
index++;
String goodsid = rs.getString(1);
String goodsname = rs.getString(2);
float price = rs.getFloat(3);
Goods g = new Goods();
g.setGoodsid(goodsid);
g.setGoodsname(goodsname);
g.setPrice(price);
goods.add(g);
}
}catch(Exception e){
e.printStackTrace();
}finally{
db.close();
}
return goods;
}
public Goods findGoodsById(String goodsid)
{
try {
// 編寫查詢數(shù)據(jù)庫信息的SQL語句
String sql = "select * from goods where goodsid=?";
DBBean db = new DBBean();
ArrayList params = new ArrayList();
params.add(goodsid);
ResultSet rs = db.executeQuery(sql,params);
if(rs.next())
{
//String goodsid =rs.getString(1);
String goodsname = rs.getString(2);
float price = rs.getFloat(3);
Goods temp = new Goods();
temp.setGoodsid(goodsid);
temp.setGoodsname(goodsname);
temp.setPrice(price);
db.close();
return temp;
}else{
return null;
}
} catch (Exception e) {
System.out.println(e.toString());
return null;
}
}
public int getPageCount() {
try {
// 編寫查詢數(shù)據(jù)庫信息的SQL語句
String sql = "select count(*) from goods";
DBBean db = new DBBean();
ResultSet rs=db.executeQuery(sql,null);
int number=0;
if(rs.next())
number = rs.getInt(1);
db.close();
return (number - 1) / 10 + 1;
} catch (Exception e) {
return 0;
}
}
}
2.2 Item類
package javabean;
// 購物項(xiàng)
public class Item
{
private Goods goods;
private int quantity;
public Item(Goods d,int quantity)
{
this.goods = d;
this.quantity = quantity;
}
public void setGoods(Goods goods){
this.goods = goods;
}
public Goods getGoods()
{
return goods;
}
public void setQuantity(int quantity)
{
this.quantity = quantity;
}
public int getQuantity()
{
return quantity;
}
}
3、 物品信息顯示功能
采用MVC模式,考慮視圖部分,不需要輸入界面,只需要顯示信息的界面。
模型部分,在前面的代碼中已經(jīng)實(shí)現(xiàn)。
控制器部分,需要編寫GetAllGoods.java。
參考代碼分別如下:
3.1 界面代碼
文件名:goodslist.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:if test="${pageNo!=1}">
<a href="getAllGoods?pageNo=1">第一頁</a>
<a href="getAllGoods?pageNo=${pageNo-1}">上一頁</a>
</c:if>
<c:if test="${pageNo!=pageCounter}">
<a href="getAllGoods?pageNo=${pageNo+1}">下一頁</a>
<a href="getAllGoods?pageNo=${pageCounter}">最后一頁</a>
</c:if>
<br>
<table width="200" border="1" height="56">
<tbody>
<tr>
<td>
物品編號(hào)
</td>
<td>
物品名稱
</td>
<td>
物品價(jià)格
</td>
</tr>
<c:forEach var="g" items="${goods}">
<tr>
<td>
${g.goodsid}
</td>
<td>
${g.goodsname}
</td>
<td>
${g.price}
</td>
<td>
<a href="addToCart?goodsid=${g.goodsid}">添加到購物車</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
3.2 控制器代
package servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javabean.*;
import java.util.*;
public class GetAllGoods extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException
{
//response.setContentType("text/html;charset=gb2312");
//PrintWriter out = response.getWriter();
// 第一步:獲取用戶的輸入信息
String pageNo=request.getParameter("pageNo");
int iPageNo=1;
if(pageNo!=null)
{
iPageNo = Integer.parseInt(pageNo);
}
// 第二步:調(diào)用JavaBean
Goods g = new Goods();
ArrayList goods=null;
goods = g.getGoodsByPage(iPageNo);
int pageCount=g.getPageCount();
// out.println("記錄數(shù):"+users.size());
// out.println("當(dāng)前頁碼:"+iPageNo);
// out.println("總頁碼:"+pageCount);
// 第三步:傳值
request.setAttribute("goods",goods);
request.setAttribute("pageNo",new Integer(iPageNo));
request.setAttribute("pageCounter",new Integer(pageCount));
// 第四步:選擇一個(gè)界面對用戶進(jìn)行響應(yīng)
String forward="goodslist.jsp";
RequestDispatcher rd = request.getRequestDispatcher(forward);
rd.forward(request,response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException
{
doGet(request,response);
}
}
4、 顯示購物車中信息
該功能直接從session中獲取購物車信息,所以不需要控制器和模型部分,只需要編寫顯示購物車信息的JSP文件即可,文件名為cart.jsp,參考代碼如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
購物車中的信息<br>
<table border=1>
<tr>
<td>物品編號(hào)</td>
<td>物品名稱</td>
<td>價(jià)格</td>
<td>數(shù)量</td>
</tr>
<c:forEach var="item" items="${cart}">
<tr>
<td>${item.goods.goodsid}</td>
<td>${item.goods.goodsname}</td>
<td>${item.goods.price}</td>
<td>${item.quantity}</td>
</c:forEach>
</table>