javaweb購(gòu)物車案列學(xué)習(xí)開發(fā)
本文實(shí)例為大家分享了javaweb購(gòu)物車案列的具體代碼,供大家參考,具體內(nèi)容如下
一、項(xiàng)目目錄結(jié)構(gòu)

二、源代碼
dao包——dao層:BookDao.java
package com.dao;
import java.util.Map;
import com.DB.DB;
import com.domain.Book;
public class BookDao {
public Map getAll(){
return DB.getAll();
}
public Book find(String id){
return (Book) DB.getAll().get(id);
}
}
DB包:DB.java——模擬數(shù)據(jù)庫(kù)
package com.DB;
import java.util.LinkedHashMap;
import java.util.Map;
import com.domain.Book;
import com.sun.org.apache.bcel.internal.generic.NEW;
//代表數(shù)據(jù)庫(kù)
//代表數(shù)據(jù)庫(kù)
public class DB {
private static Map map = new LinkedHashMap();
static{
map.put("1", new Book("1","javaweb開發(fā)","老張",38,"一本好書"));
map.put("2", new Book("2","jdbc開發(fā)","老黎",18,"一本好書"));
map.put("3", new Book("3","ajax開發(fā)","老佟",328,"一本好書"));
map.put("4", new Book("4","jbpm開發(fā)","老畢",58,"一本好書"));
map.put("5", new Book("5","struts開發(fā)","老方",28,"一本好書"));
map.put("6", new Book("6","spring開發(fā)","老方",98,"一本好書"));
}
public static Map getAll(){
return map;
}
}
domain包:
Book.java:書的實(shí)體類
package com.domain;
//書的實(shí)體類
public class Book {
private String id;
private String name;
private String author;
private double price;
private String description;
public Book() {
super();
// TODO Auto-generated constructor stub
}
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;
}
}
Cart.java:購(gòu)物車類
package com.domain;
import java.util.LinkedHashMap;
import java.util.Map;
//代表用戶的購(gòu)物車
//代表用戶的購(gòu)物車
public class Cart {
private Map<String,CartItem> map = new LinkedHashMap();
private double price; //記住購(gòu)物車所有商品多少錢
public void add(Book book){
//看購(gòu)物車中有沒(méi)有,要添加的書對(duì)應(yīng)的購(gòu)物項(xiàng)
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<String, CartItem> getMap() {
return map;
}
public void setMap(Map<String, CartItem> map) {
this.map = map;
}
public double getPrice() {
double totalprice = 0;
for(Map.Entry<String, CartItem> entry : map.entrySet()){
CartItem item = entry.getValue();
totalprice += item.getPrice();
}
this.price = totalprice;
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
CartItem.java:購(gòu)物項(xiàng)
package com.domain;
//用于代表某個(gè)商品,以及商品出現(xiàn)的次數(shù)(購(gòu)物項(xiàng))
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() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
this.price = this.book.getPrice() * this.quantity;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
service包:service層
BusinessService.java:
package com.service;
import java.util.Map;
import com.dao.BookDao;
import com.domain.Book;
import com.domain.Cart;
import com.domain.CartItem;
//業(yè)務(wù)類,統(tǒng)一對(duì)web層提供所有服務(wù)
public class BusinessService {
private BookDao dao = new BookDao();
public Map getAllBook(){
return dao.getAll();
}
public Book findBook(String id){
return dao.find(id);
}
//刪除購(gòu)物車中的購(gòu)物項(xiàng)
public void deleteCartItem(String id, Cart cart) {
cart.getMap().remove(id);
}
//清空購(gòu)物車
public void clearCart(Cart cart) {
cart.getMap().clear();
}
//改變購(gòu)物項(xiàng)的數(shù)量
public void changeItemQuantity(String id, String quantity, Cart cart) {
CartItem item = cart.getMap().get(id);
item.setQuantity(Integer.parseInt(quantity));
}
}
web層:
ListBookServlet.java:顯示所有書籍
package com.web.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.service.BusinessService;
//獲取所有書籍
//獲取所有書
public class ListBookServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
BusinessService service = new BusinessService();
Map map = service.getAllBook();
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);
}
}
BuyServlet.java:處理購(gòu)買請(qǐng)求
package com.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 com.domain.Book;
import com.domain.Cart;
import com.service.BusinessService;
//完成書籍購(gòu)買
//完成書籍購(gòu)買
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);
//得到用戶的購(gòu)物車
Cart cart = (Cart) request.getSession().getAttribute("cart");
if(cart==null){
cart = new Cart();
request.getSession().setAttribute("cart", cart);
}
//把書加到用戶購(gòu)物車中,完成購(gòu)買
cart.add(book);
//response.sendRedirect("/WEB-INF/jsp/listcart.jsp");
request.getRequestDispatcher("/WEB-INF/jsp/listcart.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
DeleteItemServlet.java:刪除某一種商品
package com.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 com.domain.Cart;
import com.service.BusinessService;
//刪除指定的購(gòu)物項(xiàng)
public class DeleteItemServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
Cart cart = (Cart) request.getSession().getAttribute("cart");
BusinessService service = new BusinessService();
service.deleteCartItem(id,cart);
//刪除成功
request.getRequestDispatcher("/WEB-INF/jsp/listcart.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
ClearCartServlet.java:清空購(gòu)物車
package com.web.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.domain.Cart;
import com.service.BusinessService;
//清空購(gòu)物車
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);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
ChangeQuantityServlet.java:修改購(gòu)物車中指定商品的數(shù)量
package com.web.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.domain.Cart;
import com.service.BusinessService;
//把購(gòu)物車中的書修改為指定數(shù)量
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);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
jsp頁(yè)面:
WebRoot/WEB-INF/jsp/listbook.jsp:顯示書籍列表
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>書籍列表頁(yè)面</title>
</head>
<body style="text-align: center">
<h1>書籍列表</h1>
<table width="70%" border="1">
<tr>
<td>書名</td>
<td>作者</td>
<td>售價(jià)</td>
<td>描述 </td>
<td>操作</td>
</tr>
<c:forEach var="entry" items="${map}">
<tr>
<td>${entry.value.name }</td>
<td>${entry.value.author }</td>
<td>${entry.value.price }</td>
<td>${entry.value.description } </td>
<td>
<a href="${pageContext.request.contextPath }/servlet/BuyServlet?id=${entry.value.id }" rel="external nofollow" target="_blank">購(gòu)買</a>
</td>
</tr>
</c:forEach>
</table>
</body>
WebRoot/WEB-INF/jsp/listcart.jsp:顯示購(gòu)物車列表
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>購(gòu)物車列表</title>
<script type="text/javascript">
function deleteitem(id){
var b = window.confirm("您確認(rèn)刪除嗎??");
if(b){
window.location.href="${pageContext.request.contextPath }/servlet/DeleteItemServlet?id=" rel="external nofollow" +id;
}
}
function clearcart(){
var b = window.confirm("您確認(rèn)清空嗎??");
if(b){
window.location.href="${pageContext.request.contextPath}/servlet/ClearCartServlet" rel="external nofollow" ;
}
}
function changeQuantity(input,id,oldvalue){
var quantity = input.value; //得到要修改的數(shù)量 sdfsfs
/*
//檢查用戶輸入的數(shù)量是不是一個(gè)數(shù)字
if(isNaN(quantity)){
alert("請(qǐng)輸入數(shù)字??!");
input.value = oldvalue;
return;
}
*/
//檢查用戶輸入的數(shù)量是不是一個(gè)正整數(shù)
if(quantity<0 || quantity!=parseInt(quantity)){
alert("請(qǐng)輸入正整數(shù)!!");
input.value = oldvalue;
return;
}
var b = window.confirm("您確認(rèn)把書的數(shù)量修改為:" + quantity);
if(b){
window.location.href="${pageContext.request.contextPath}/servlet/ChangeQuantityServlet?id=" rel="external nofollow" + id + "&quantity=" + quantity;
}
}
</script>
</head>
<body style="text-align: center">
<h1>購(gòu)物車列表</h1>
<c:if test="${empty(cart.map)}">
您沒(méi)有購(gòu)買任何商品?。?!
</c:if>
<c:if test="${!empty(cart.map)}">
<table width="70%" border="1">
<tr>
<td>書名</td>
<td>作者</td>
<td>單價(jià)</td>
<td>數(shù)量 </td>
<td>小計(jì)</td>
<td>操作</td>
</tr>
<c:forEach var="entry" items="${cart.map}">
<tr>
<td>${entry.value.book.name }</td>
<td>${entry.value.book.author }</td>
<td>${entry.value.book.price }</td>
<td>
<input type="text" name="quantity" value="${entry.value.quantity }" style="width:35px" onchange="changeQuantity(this,${entry.key},${entry.value.quantity})">
</td>
<td>${entry.value.price }</td>
<td>
<a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" onclick="deleteitem(${entry.key })">刪除</a> <!-- 去掉超鏈接默認(rèn)行為 -->
</td>
</tr>
</c:forEach>
<tr>
<td colspan="3">總價(jià)</td>
<td colspan="2">${cart.price }元</td>
<td colspan="1">
<a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" onclick="clearcart()">清空購(gòu)物車</a>
</td>
</tr>
</table>
</c:if>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JavaWeb購(gòu)物車項(xiàng)目開發(fā)實(shí)戰(zhàn)指南
- JavaWeb后臺(tái)購(gòu)物車類實(shí)現(xiàn)代碼詳解
- eclipse的web項(xiàng)目實(shí)現(xiàn)Javaweb購(gòu)物車的方法
- java web開發(fā)之購(gòu)物車功能實(shí)現(xiàn)示例代碼
- javaweb圖書商城設(shè)計(jì)之購(gòu)物車模塊(3)
- java web開發(fā)之實(shí)現(xiàn)購(gòu)物車功能
- java商城項(xiàng)目實(shí)戰(zhàn)之購(gòu)物車功能實(shí)現(xiàn)
- JAVAEE中用Session簡(jiǎn)單實(shí)現(xiàn)購(gòu)物車功能示例代碼
- java實(shí)現(xiàn)網(wǎng)上購(gòu)物車程序
- Java?web實(shí)現(xiàn)購(gòu)物車案例
相關(guān)文章
Java實(shí)現(xiàn)按權(quán)重隨機(jī)數(shù)
這篇文章主要介紹了Java實(shí)現(xiàn)按權(quán)重隨機(jī)數(shù),本文給出了提出問(wèn)題、分析問(wèn)題、解決問(wèn)題三個(gè)步驟,需要的朋友可以參考下2015-04-04
MyBatis傳入?yún)?shù)為L(zhǎng)ist對(duì)象的實(shí)現(xiàn)
這篇文章主要介紹了MyBatis傳入?yún)?shù)為L(zhǎng)ist對(duì)象的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Java web過(guò)濾器驗(yàn)證登錄防止未登錄進(jìn)入界面
這篇文章主要介紹了Java web過(guò)濾器驗(yàn)證登錄防止未登錄進(jìn)入界面,在一些系統(tǒng)中經(jīng)??梢杂玫酱斯δ?,對(duì)java web 驗(yàn)證登錄知識(shí)感興趣的朋友一起看下吧2016-08-08
SpringSceurity實(shí)現(xiàn)短信驗(yàn)證碼登陸
這篇文章主要介紹了SpringSceurity實(shí)現(xiàn)短信驗(yàn)證碼登陸,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
SpringBoot使用MockMvc測(cè)試get和post接口的示例代碼
Spring Boot MockMvc是一個(gè)用于單元測(cè)試的模塊,它是Spring框架的一部分,專注于簡(jiǎn)化Web應(yīng)用程序的測(cè)試,MockMvc主要用來(lái)模擬一個(gè)完整的HTTP請(qǐng)求-響應(yīng)生命周期,本文給大家介紹了SpringBoot使用MockMvc測(cè)試get和post接口,需要的朋友可以參考下2024-06-06

