javaweb购物车案列学习开发
程序员文章站
2023-12-18 22:12:58
本文实例为大家分享了javaweb购物车案列的具体代码,供大家参考,具体内容如下
一、项目目录结构
二、源代码
dao包——dao层:bo...
本文实例为大家分享了javaweb购物车案列的具体代码,供大家参考,具体内容如下
一、项目目录结构
二、源代码
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——模拟数据库
package com.db; import java.util.linkedhashmap; import java.util.map; import com.domain.book; import com.sun.org.apache.bcel.internal.generic.new; //代表数据库 //代表数据库 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; } }
domain包:
book.java:书的实体类
package com.domain; //书的实体类 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:购物车类
package com.domain; import java.util.linkedhashmap; import java.util.map; //代表用户的购物车 //代表用户的购物车 public class cart { private map<string,cartitem> 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<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:购物项
package com.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() { 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; //业务类,统一对web层提供所有服务 public class businessservice { private bookdao dao = new bookdao(); public map getallbook(){ return dao.getall(); } public book findbook(string id){ return dao.find(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)); } }
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:处理购买请求
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; //完成书籍购买 //完成书籍购买 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("/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; //删除指定的购物项 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:清空购物车
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; //清空购物车 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:修改购物车中指定商品的数量
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; //把购物车中的书修改为指定数量 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页面:
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>书籍列表页面</title> </head> <body style="text-align: center"> <h1>书籍列表</h1> <table width="70%" border="1"> <tr> <td>书名</td> <td>作者</td> <td>售价</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">购买</a> </td> </tr> </c:foreach> </table> </body>
webroot/web-inf/jsp/listcart.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>购物车列表</title> <script type="text/javascript"> function deleteitem(id){ var b = window.confirm("您确认删除吗??"); if(b){ window.location.href="${pagecontext.request.contextpath }/servlet/deleteitemservlet?id=" rel="external nofollow" +id; } } function clearcart(){ var b = window.confirm("您确认清空吗??"); if(b){ window.location.href="${pagecontext.request.contextpath}/servlet/clearcartservlet" rel="external nofollow" ; } } function changequantity(input,id,oldvalue){ var quantity = input.value; //得到要修改的数量 sdfsfs /* //检查用户输入的数量是不是一个数字 if(isnan(quantity)){ alert("请输入数字!!"); input.value = oldvalue; return; } */ //检查用户输入的数量是不是一个正整数 if(quantity<0 || quantity!=parseint(quantity)){ alert("请输入正整数!!"); input.value = oldvalue; return; } var b = window.confirm("您确认把书的数量修改为:" + 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>购物车列表</h1> <c:if test="${empty(cart.map)}"> 您没有购买任何商品!!! </c:if> <c:if test="${!empty(cart.map)}"> <table width="70%" border="1"> <tr> <td>书名</td> <td>作者</td> <td>单价</td> <td>数量 </td> <td>小计</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> <!-- 去掉超链接默认行为 --> </td> </tr> </c:foreach> <tr> <td colspan="3">总价</td> <td colspan="2">${cart.price }元</td> <td colspan="1"> <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" onclick="clearcart()">清空购物车</a> </td> </tr> </table> </c:if> </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。