一、首页浏览书籍的链接 index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
首页
浏览书籍
二、创建DB,用Map集合来代表数据库
package cn.itcast.DB;
import java.util.LinkedHashMap;
import java.util.Map;
import cn.itcast.domain.Book;
public class DB {
private static Map map = new LinkedHashMap();
static {
map.put("1", new Book("1", "javaweb开发", "老张", 38, "一本好书"));
map.put("2", new Book("2", "jdbc开发", "老黎", 18, "一本好书"));
map.put("3", new Book("3", "ajax开发", "老佟", 328, "一本好书"));
map.put("4", new Book("4", "jbpm开发", "老毕", 58, "一本好书"));
map.put("5", new Book("5", "struts开发", "老方", 28, "一本好书"));
map.put("6", new Book("6", "spring开发", "老方", 98, "一本好书"));
}
public static Map getAll() {
return map;
}
}
三、开发实例类Bean Book.java
package cn.itcast.domain;
public class Book {
private String id;
private String name;
private String author;
private double price;
private String description;
public Book() {
super();
}
public Book(String id, String name, String author, double price,
String description) {
super();
this.id = id;
this.name = name;
this.author = author;
this.price = price;
this.description = description;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
四、用JavaBean建立一个购物车对象
package cn.itcast.domain;
//代表某个商品,以及商品出现的次数(购物项)
public class CartItem {
private Book book;
private int quantity;
private double price;
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public int getQuantity() {
price = book.getPrice() * quantity;
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
五、代表用户的购物车 Cart
package cn.itcast.domain;
import java.util.LinkedHashMap;
import java.util.Map;
//代表用户的购物车
public class Cart {
private Map map = new LinkedHashMap();
private double price;//总价
public void add(Book book) {
//看购物车中有没有要添加书的购物项
CartItem item = map.get(book.getId());
if (item == null) {
item = new CartItem();
item.setBook(book);
item.setQuantity(1);
map.put(book.getId(), item);
} else {
item.setQuantity(item.getQuantity() + 1);
}
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public double getPrice() {
double totalprice = 0;
for (Map.Entry entry : map.entrySet()) {
CartItem item = entry.getValue();
totalprice += item.getPrice();
}
this.price = totalprice;
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
六、开发DAO
package cn.itcast.dao;
import java.util.Map;
import cn.itcast.DB.DB;
import cn.itcast.domain.Book;
public class BookDao {
public Map getAll() {
return DB.getAll();
}
public Book find(String id) {
return (Book) DB.getAll().get(id);
}
}
七、开发 BusinessService
package cn.itcast.service;
import java.util.Map;
import cn.itcast.domain.Book;
import cn.itcast.dao.BookDao;
import cn.itcast.domain.Cart;
import cn.itcast.domain.CartItem;
//业务层,统一对web层提供所有服务
public class BusinessService {
private BookDao dao = new BookDao();
public Map getAll() {
return dao.getAll();
}
public Book findBook(String id) {
return (Book) dao.getAll().get(id);
}
//删除购物车中的购物项
public void deleteCartItem(String id, Cart cart) {
cart.getMap().remove(id);
}
//清空购物车
public void clearCart(Cart cart) {
cart.getMap().clear();
}
//改变购物数量
public void changeItemQuantity(String id, String quantity, Cart cart) {
CartItem item = cart.getMap().get(id);
item.setQuantity(Integer.parseInt(quantity));
}
}
八、获得图书商品列表 ListBookServlet
package cn.itcast.web.controller;
import java.io.IOException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.itcast.service.BusinessService;
public class ListBookServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
BusinessService service = new BusinessService();
Map map = service.getAll();
request.setAttribute("map", map);
request.getRequestDispatcher("/WEB-INF/jsp/listbook.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
九、数据转发到显示界面 listbook.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
书籍列表页面
书籍列表
书名
作者
售价
描述
操作
${entry.value.name }
${entry.value.author }
${entry.value.price }
${entry.value.description }
购买
十、购买处理 BuyServlet
package cn.itcast.web.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.itcast.domain.Book;
import cn.itcast.domain.Cart;
import cn.itcast.service.BusinessService;
public class BuyServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
BusinessService service = new BusinessService();
Book book = service.findBook(id);
//得到用户的购物车
Cart cart = (Cart) request.getSession().getAttribute("cart");
if (cart == null) {
cart = new Cart();
request.getSession().setAttribute("cart", cart);
}
//把书加到用户的购物车中
cart.add(book);
response.sendRedirect(request.getContextPath() + "/servlet/ListcartUIServlet");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
十一、进入购物车列表,写一个处理列表显示的 ListcartUIServlet
和 listcart.jsp
① ListcartUIServlet
package cn.itcast.web.UI;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ListcartUIServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("/WEB-INF/jsp/listcart.jsp").forward(request, response);
}
}
② listcart.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
购物车列表
购物车列表
你没有购买任何商品
书名
作者
单价
数量
小计
操作
${entry.value.book.name }
${entry.value.book.author }
${entry.value.book.price }
${entry.value.price }
删除
总价计
${cart.price }元
清空购物车
十二、更新购物车
① 删除指定的购物项 DeleteItemServlet
package cn.itcast.web.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.itcast.domain.Cart;
import cn.itcast.service.BusinessService;
//删除指定的购物项
public class DeleteItemServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
BusinessService service = new BusinessService();
Cart cart = (Cart) request.getSession().getAttribute("cart");
service.deleteCartItem(id, cart);
request.getRequestDispatcher("/WEB-INF/jsp/listcart.jsp").forward(request, response);
}
}
② 清空购物车 ClearCartServlet
package cn.itcast.web.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.itcast.domain.Cart;
import cn.itcast.service.BusinessService;
//清空购物车
public class ClearCartServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Cart cart=(Cart) request.getSession().getAttribute("cart");
BusinessService service = new BusinessService();
service.clearCart(cart);
request.getRequestDispatcher("/WEB-INF/jsp/listcart.jsp").forward(request, response);
}
}
③ 改变购物项的数量 ChangeQuantityServlet
package cn.itcast.web.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.itcast.domain.Cart;
import cn.itcast.service.BusinessService;
//把购物车中的书修改为指定数量
public class ChangeQuantityServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id=request.getParameter("id");
String quantity=request.getParameter("quantity");
Cart cart=(Cart)request.getSession().getAttribute("cart");
BusinessService service=new BusinessService();
service.changeItemQuantity(id,quantity,cart);
request.getRequestDispatcher("/WEB-INF/jsp/listcart.jsp").forward(request, response);
}
}
源代码:https://github.com/yvettee36/MVC_Cart