jsp电子商务 购物车实现之三 购物车
CartServlet参考代码 :
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String bookids[] = req.getParameterValues("bookId"); //id数组;
Map<Integer,Book> cart = (Map<Integer,Book>)req.getSession().getAttribute("cart");
//System.out.println(cart.size()); null对
/*
* Integer Book
* id book (count)
* 7 book (count=2)
* 8 book ( count=5)
*
* */
BookDao bd = new BookDaoImpl();
//如果购物车为null,则new出来一个HashMap对象
if(cart==null){
cart = new HashMap<Integer,Book>();//id以及book对象
req.getSession().setAttribute("cart", cart);
}
for(String bookid:bookids){
Book book = cart.get(Integer.parseInt(bookid));
if(book==null){
book = bd.findById(Integer.parseInt(bookid));//根据id获得书籍
book.setCount(1);//数量为1
}
else{
// 判断是否已经是最后一本书;数量大于获得数量,其他的不能再加数量了
if(book.getStock()>book.getCount()){
book.setCount(book.getCount()+1);
}
}
cart.put(Integer.parseInt(bookid), book);
//需要放入到数据库中,那么购物车表的字段是什么呢?
//图书id 书名 图片名 数量 合计价格
}
req.getSession().setAttribute("cart", cart);
resp.sendRedirect("cart.jsp");
}
在该Servlet中利用了session进行存放值,就是开篇我们的方法之一,但是这种方法有什么问题呢?大家好好思考下!!!
下面是购物车代码参考:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'cart.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" href="css/style.css" type="text/css"></link>
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
function jian(id){
if($("#count"+id).val()==1){
//采用淘宝模式,如果低于1,则不用提示,直接不可用;
$("#count"+id).prev().attribute("disabled","disabled");
return;
}
$.ajax({
url:'ChangeCartCountServlet',
type:'post',
dataType:'text',
data:{
bookid:id,
count:parseInt($("#count"+id).val())-1 // -1
},
success:function(data){
var price = $("#price"+id).html();
$("#count"+id).val(parseInt($("#count"+id).val())-1);
$("#sum"+id).val("¥"+price*$("#count"+id).val());
calcTotal();
}
});
}
function add(id){
$.ajax({
url:'ChangeCartCountServlet',
type:'post',
dataType:'text',
data:{
bookid:id,
count:parseInt($("#count"+id).val())+1
},
success:function(data){
if(data=="false"){
alert("库存不足!!!!");
}
else{
var price = $("#price"+id).html();
$("#count"+id).val(parseInt($("#count"+id).val())+1);
$("#sum"+id).val("¥"+price*$("#count"+id).val());
calcTotal();
}
}
});
}
function calcTotal(){
// input...
var counts = $("input[id^=count]").toArray();
var prices = $("div[id^=price]").toArray();
var total = 0;
for(var i=0;i<prices.length;i++){
total += prices[i].innerHTML*counts[i].value;
}
$("#total").val("¥"+total);
}
</script>
</head>
<body>
<div id="header" class="wrap">
<div id="banner"></div>
<div id="navbar">
<div class="userMenu">
<ul>
<li class="current"><font color="BLACK">欢迎您,<strong>andy</strong></font> </li>
<li><a href="index.html">首页</a></li>
<li><a href="orderlist.html">我的订单</a></li>
<li><a href="cart.html">购物车</a></li>
<li><a href="logout.jsp">注销</a></li>
</ul>
</div>
</div>
</div>
<div id="content" class="wrap">
<div class="list bookList">
<form method="post" name="shoping" action="BuyServlet">
<table>
<tr class="title">
<th class="view">图片预览</th>
<th>书名</th>
<th class="nums">数量</th>
<th class="price">价格</th>
<th class="nums">合计</th>
<th class="nums">操作</th>
</tr>
<c:set var="total" value="0"></c:set>
<c:forEach items="${cart}" var="book">
<tr>
<td class="thumb">
<img src="images/book/${book.value.image}" /></td>
<td class="title">${book.value.bookname}</td>
<td>
<img src="images/edit_jian.png" width="12" height="12"
onclick="jian(${book.value.id})"/>
<input id="count${book.value.id}" readonly="readonly"
value="${book.value.count}" size="2"/>
<img src="images/edit_add.png" width="12" height="12"
onclick="add(${book.value.id})"/>
</td>
<td>¥
<div id="price${book.value.id}" >${book.value.price}</div>
</td>
<td>
<input id="sum${book.value.id}"
value='<fmt:formatNumber
value="${book.value.count*book.value.price}"
type="currency"></fmt:formatNumber>'
/>
<c:set var="total"
value=
"${total+book.value.count*book.value.price}"></c:set>
<input type="hidden" name="items" value="10:2:31.6"/>
</td>
<td>
<a href="DeleteCartItemServlet?bookid=${book.value.id}">删除</a>
</td>
</tr>
</c:forEach>
<tr>
<td>
地址
</td>
<td colspan="5">
<input name="shipaddress">
</td>
</tr>
<tr>
<td>
电话
</td>
<td colspan="5">
<input name="contactphone">
</td>
</tr>
<tr><td colspan="5">
<div class="button">
<h4>总价:
<input id="total"
value='<fmt:formatNumber value="${total}" type="currency"></fmt:formatNumber>'
/>
元</h4>
<input type="hidden" id="hidden_total_price" name="hidden_total_price"/>
<input class="input-chart" type="submit" name="submit" value="" />
</div>
</td></tr>
</table>
</form>
</div>
</div>
</body>
<div id="footer" class="wrap">
网上书城 © 版权所有
</div>
</html>
</html>
根据id从购物车中删除的Servlet代码参考如下:
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
int bookid = Integer.parseInt(req.getParameter("bookid"));
Map<Integer,Book> cart = (Map<Integer,Book>)
req.getSession().getAttribute("cart");
// 根据key(bookid)删除
cart.remove(bookid);
req.getSession().setAttribute("cart", cart);
resp.sendRedirect("cart.jsp");
}
jsp电子商务 购物车实现之三 购物车的更多相关文章
- taotao购物车2 解决购物车本地cookie和服务器redis不同步的问题
下面的思路逻辑一定要理清楚,比较绕 思路; 前面已经实现了在cookie本地维护购物车的功能, 这次加入和服务器同步功能, 因为 购物车 操作比较频繁,所以,后台服务器 用redis存储用户的购物车信 ...
- "首页添加至购物车,TabBar显示购物车的数量"实现
今天学习别人的项目源码的时候,看到这样的一种实现功能:首页添加至购物车,TabBar显示购物车的数量....想到以前没有做过,这里学习了,记录一下: 实现的效果图如下: 当点击首页添加至购物的操作的时 ...
- day83:luffy:添加购物车&导航栏购物车数字显示&购物车页面展示
目录 1.添加购物车+验证登录状态 2.右上方购物车图标的小红圆圈数字 3.Vuex 4.购物车页面展示-后端接口 5.购物车页面展示-前端 6.解决一个购物车数量显示混乱的bug 1.添加购物车+验 ...
- jsp电子商务购物车之五 数据库存储篇2
业务逻辑图,简单版要写各个Servlet //ChangeCartCountServlet 使用ajax实现数量,增加或减少; package com.cart.web; import java.io ...
- jsp电子商务购物车之四 数据库存储篇
为了方便用户下次登录,仍然可以看到自己的购物车内容,所以,需要在数据库存储相应的购物车项目,本处增加购物车项表;uid和bid是复合主键. package com.cart.entity; //购物车 ...
- jsp电子商务 购物车实现之二 登录和分页篇
登录页面核心代码 <div id="login"> <h2>用户登陆</h2> <form method="post" ...
- jsp电子商务 购物车实现之一 设计篇
购物车的功能实现. 查询的资料,找到三种方法: 1.用cookie实现购物车: 2.用session实现购物车: 3.用cookie和数据库(购物车信息持久化)实现购物车: ============= ...
- 使用MongoDB和JSP实现一个简单的购物车系统
目录 1 问题描述 2 解决方案 2.1 实现功能 2.2 最终运行效果图 2.3 系统功能框架示意图 2.4 有关MongoDB简介及系统环境配置 2.5 核心功能代码讲解 ...
- ASP.NET之电子商务系统开发-2(购物车功能)
一.前言 继上次的首页数据列表后,这是第二篇.记录一下购物车这个比较庞大的功能,可能实现的方法跟其他人有点不一样,不过原理都差不多,是将cookie存数据库里面的. 二.开始 首先看一下购物车流程及对 ...
随机推荐
- MySQL数据操作(DML)
表结构准备: mysql> CREATE TABLE student( -> sid INT PRIMARY KEY AUTO_INCREMENT, ), -> age INT, ) ...
- 爬虫之爬取斗鱼官网LOL部分主播的状态
一个爬虫小程序 爬取主播的排名及观看人数 import re import requests import request class Spider(): url = 'https://www.dou ...
- linux 热替换so文件
http://www.zhaoch.top/操作系统/linux/热替换so文件.html 热替换so文件 www.zhaoch.top > 操作系统 > linux 发现nginx的动态 ...
- sort函数
做项目的时候,排序是一种经常要用到的操作.如果每次都自己写个冒泡之类的O(n^2)排序,不但程序容易超时,而且浪费宝贵的时间,还很有可能写错.STL里面有个sort函数,可以直接对数组排序,复杂度为n ...
- Matplotlib 基本图表的绘制
图表类别:线形图.柱状图.密度图,以横纵坐标两个维度为主 同时可延展出多种其他图表样式 plt.plot(kind='line', ax=None, figsize=None, use_index=T ...
- C#的委托Delegate
一.委托基础 1.什么是委托 委托(Delegate) 是存有对某个方法的引用的一种引用类型变量,用关键字delegate申明,实现相同返回值和参数的函数的动态调用,提供了对方法的抽象. 委托(Del ...
- 【Keras案例学习】 sklearn包装器使用示范(mnist_sklearn_wrapper)
import numpy as np from keras.datasets import mnist from keras.models import Sequential from keras.l ...
- Migrating from MapReduce 1 (MRv1) to MapReduce 2 (MRv2, YARN)...
This is a guide to migrating from Apache MapReduce 1 (MRv1) to the Next Generation MapReduce (MRv2 o ...
- js面向对象过程
var a = new b(); 等价于 var a={}; a=b.prototype; b.call(a);
- OpenCV入门:(一:安装与配置)
看到的不是自己的,只有写下来的才是自己的,上次接触OpenCV实在三个月前,亢奋的看完了OpenCV自带的入门文档,觉得对图形处理有了一点点了解,现在三个月过去了,由于学习需要,想深入了解OpenCV ...