一、添加商品到购物车开发步骤:1) 创建数据库表及进行反向工程2) 创建购物车模型3) 编写DAO 层4) 编写Service 层5) 编写Action 层6) 编写struts 和Spring 配置文件7) 编写JSP 页面具体操作如下:1. 创建数据库表订单表(orders),包含3 个字段:orderid(订单编号),userid(用户表的关联字段),orderdate(订单日期)订单项表(orderitem),包含4 个字段:orderitemid(订单项编号),orderid(与订单表关联字段),bookid(与图书表关联字段),quantity(数量)进行两张表的反向工程,生成响应的POJO 和hbm.xml 文件。
2. 创建购物车模型创建购物车模型购物车模型CartCart.java 代码如下:package com.sxjiang.bookstore.model;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import com.sxjiang.bookstore.vo.Orderitem;public final class Cart {protected Map<Integer,Orderitem> items;public Cart(){if(items==null) {items=new HashMap<Integer,Orderitem>();}}public void addBook(Integer bookid,Orderitem orderitem){ if(items.containsKey("bookid")){Orderitem _orderitem=items.get(bookid);_orderitem.setQuantity(_orderitem.getOrderitemid()+orderite m.getQuantity());items.put(bookid, _orderitem);}else{items.put(bookid, orderitem);}}public void updateCart(Integer bookid,int quantity){ Orderitem orderitem=items.get(bookid);orderitem.setQuantity(quantity);items.put(bookid, orderitem);}public int getTotalPrice(){int totalPrice=0;for(Iterator it=items.values().iterator();it.hasNext();){ Orderitem orderitem=(Orderitem)it.next();Book book=orderitem.getBook();int quantity=orderitem.getQuantity();totalPrice+=book.getPrice()*quantity;}return totalPrice;}public Map<Integer, Orderitem> getItems() {return items;}public void setItems(Map<Integer, Orderitem> items) {this.items = items;}}3. DAODAO 层主要的类为BaseDAO 类,IBookDAO 接口和BookDAO 类。
在IBookDAO 中定义了getBookById()方法,通过这个方法可以根据id 号,得到书的信息。
BookDAO 具体实现了这个接口。
修改IBookDAO.java,加上如下代码:public Book getBookById(Integer bookid);//根据图书号得到图书修改BookDAO.java 实现getBookById()方法public Book getBookById(Integer bookid) {// TODO Auto-generated method stubSession session=getSession();Book book=(Book)session.get(Book.class, bookid);session.close();return book;}4. Service在IBookService 接口中定义getBookById(),根据图书号获得图书,在BookService中实现此方法。
修改IBookService.java,加上getBookById()的定义://根据图书号得到图书public Book getBookById(Integer bookid);修改BookService.java,实现getBookById()方法。
public Book getBookById(Integer bookid) {// TODO Auto-generated method stubreturn bookDAO.getBookById(bookid);}5. Action创建ShoppingAction,通过addToCart()方法,完成将图书放入购物车中。
ShoppingAction.java 代码如下:package com.sxjiang.bookstore.action;import java.util.Map;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import com.sxjiang.bookstore.model.Cart;import com.sxjiang.bookstore.service.IBookService; import com.sxjiang.bookstore.vo.Book;import com.sxjiang.bookstore.vo.Orderitem;public class ShoppingAction extends ActionSupport { private Integer bookid;private int quantity;private IBookService bookService;public String addToCart() throws Exception{Book book=bookService.getBookById(bookid); Orderitem orderitem=new Orderitem();orderitem.setBookid(book.getBookid());orderitem.setQuantity(quantity);Map session=ActionContext.getContext().getSession(); Cart cart=(Cart)session.get("cart");if(cart==null){cart=new Cart();}cart.addBook(bookid, orderitem);session.put("cart",cart);return SUCCESS;}public String updateCart() throws Exception{Map session=ActionContext.getContext().getSession(); Cart cart=(Cart)session.get("cart");cart.updateCart(bookid, quantity);session.put("cart", cart);return SUCCESS;}public Integer getBookid() {return bookid;}public void setBookid(Integer bookid) {this.bookid = bookid;}public int getQuantity() {return quantity;}public void setQuantity(int quantity) {this.quantity = quantity;}public IBookService getBookService() {return bookService;}public void setBookService(IBookService bookService) { this.bookService=bookService;}}6. 配置struts.xml 和applicationContext.xml在struts.xml 中增加名为:addToCart 的Action 代码如下:<action name="addToCart" class="shoppingAction"method="addToCart"><result name="success">/addToCart_result.jsp</result></action>在applicationContext.xml 中增加shoppringAction 的bean 的配置<bean id="shoppingAction"class="com.sxjiang.bookstore.action.ShoppingAction"><property name="bookService" ref="bookService"></property></bean>7. 编写JSP 页面修改browseBookPaging.jsp,代码如下:<%@ page language="java" contentType="text/html;charset=gb2312"pageEncoding="gb2312"%><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"><title>Insert title here</title></head><body><s:iterator value="#request['books']" id="book"><img src="/bookstore/picture/<s:propertyvalue="#book.picture"/>"width="100">书名:<s:property value="#book.bookname"/>价格:<s:property value="#book.price"/>元<form action="addToCart.action" method=""post">数量:<input type="text" name="quantity" value="0"size="4"/><input type="hidden" value="<s:propertyvalue="#book.bookid"/>" name="bookid"><input type="image" name="submit"src="/bookstore/picture/buy.gif"/></form><br></s:iterator><s:set name="pager" value="#request.pager"/><s:if test="#pager.hasFirst"><a href="browseBookPaging.action?currentPage=1">首页</a> </s:if><s:if test="#pager.hasPrevious"><a href="browseBookPaging.action?currentPage=<s:property value="#pager.currentPage-1"/>">上一页</a></s:if><s:if test="#pager.hasNext"><a href="browseBookPaging.action?currentPage=<s:property value="#pager.currentPage+1"/>">下一页</a></s:if><s:if test="#pager.hasLast"><a href="browseBookPaging.action?currentPage=<s:property value="#pager.totalPage"/>">尾页</a></s:if>当前第<s:property value="#pager.currentPage"/>页,总共<s:propertyvalue="#pager.totalPage"/>页</body></html>二、显示购物车开发步骤:ModelActionStruts.xml applicationContext.xmlJSP1. ModelCart 模型中的updateCart 方法用于更新购物车。