how2j.cn

下载区
文件名 文件大小
请先登录 438k
增值内容 438k
438k
步骤 1 : 先运行,看到效果,再学习   
步骤 2 : 模仿和排错   
步骤 3 : 界面效果   
步骤 4 : cart.html   
步骤 5 : cartPage.html   
步骤 6 : modal4deleteConfirm.html   
步骤 7 : ForeRESTController.cart()   

步骤 1 :

先运行,看到效果,再学习

edit
增值内容,请先登录
完整的 Springboot 模仿天猫项目,使用 Springboot 、Vue.js、shiro、redis、elasticsearch 等一整套技术栈, 从无到有涵盖全部129个知识点,565个开发步骤, 充实 Springboot 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
增值内容,请先登录
完整的 Springboot 模仿天猫项目,使用 Springboot 、Vue.js、shiro、redis、elasticsearch 等一整套技术栈, 从无到有涵盖全部129个知识点,565个开发步骤, 充实 Springboot 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
增值内容,请先登录
完整的 Springboot 模仿天猫项目,使用 Springboot 、Vue.js、shiro、redis、elasticsearch 等一整套技术栈, 从无到有涵盖全部129个知识点,565个开发步骤, 充实 Springboot 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
界面效果
增值内容,请先登录
完整的 Springboot 模仿天猫项目,使用 Springboot 、Vue.js、shiro、redis、elasticsearch 等一整套技术栈, 从无到有涵盖全部129个知识点,565个开发步骤, 充实 Springboot 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head th:include="include/fore/header::html" ></head> <body> <div id="workingArea"> <div th:replace="include/fore/top::html" ></div> <div th:replace="include/fore/simpleSearch::html" ></div> <div th:replace="include/fore/cart/cartPage::html" ></div> <div th:replace="include/fore/modal4deleteConfirm::html" ></div> <div th:replace="include/fore/footer::html" ></div> </div> </body> </html>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
	<head th:include="include/fore/header::html" ></head>
	<body>
		<div id="workingArea">
			<div th:replace="include/fore/top::html" ></div>	
			<div th:replace="include/fore/simpleSearch::html" ></div>	
			<div th:replace="include/fore/cart/cartPage::html" ></div>	
			<div th:replace="include/fore/modal4deleteConfirm::html" ></div>
			<div th:replace="include/fore/footer::html" ></div>	
		</div>
	</body>
</html>
增值内容,请先登录
完整的 Springboot 模仿天猫项目,使用 Springboot 、Vue.js、shiro、redis、elasticsearch 等一整套技术栈, 从无到有涵盖全部129个知识点,565个开发步骤, 充实 Springboot 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
cartPage.html
<div th:fragment="html"> <script> $(function(){ var data4Vue = { uri:'forecart', orderItems:[] }; //ViewModel var vue = new Vue({ el: '#workingArea', data: data4Vue, mounted:function(){ //mounted 表示这个 Vue 对象加载成功了 this.load(); }, methods: { load:function(){ var url = this.uri; axios.get(url).then(function(response) { vue.orderItems = response.data; vue.$nextTick(function(){ linkDefaultActions(); cartPageRegisterListeners(); }) }); } } }); }) var deleteOrderItem = false; var deleteOrderItemid = 0; function cartPageRegisterListeners(){ $("a.deleteOrderItem").click(function(){ deleteOrderItem = false; var oiid = $(this).attr("oiid") deleteOrderItemid = oiid; $("#deleteConfirmModal").modal('show'); }); $("button.deleteConfirmButton").click(function(){ deleteOrderItem = true; $("#deleteConfirmModal").modal('hide'); }); $('#deleteConfirmModal').on('hidden.bs.modal', function (e) { if(deleteOrderItem){ var url = "foredeleteOrderItem?oiid="+deleteOrderItemid; axios.get(url).then(function(response){ if(0==response.data.code){ $("tr.cartProductItemTR[oiid="+deleteOrderItemid+"]").hide(); } else{ location.href="login"; } }); } }) $("img.cartProductItemIfSelected").click(function(){ var selectit = $(this).attr("selectit") if("selectit"==selectit){ $(this).attr("src","img/site/cartNotSelected.png"); $(this).attr("selectit","false") $(this).parents("tr.cartProductItemTR").css("background-color","#fff"); } else{ $(this).attr("src","img/site/cartSelected.png"); $(this).attr("selectit","selectit") $(this).parents("tr.cartProductItemTR").css("background-color","#FFF8E1"); } syncSelect(); syncCreateOrderButton(); calcCartSumPriceAndNumber(); }); $("img.selectAllItem").click(function(){ var selectit = $(this).attr("selectit") if("selectit"==selectit){ $("img.selectAllItem").attr("src","img/site/cartNotSelected.png"); $("img.selectAllItem").attr("selectit","false") $(".cartProductItemIfSelected").each(function(){ $(this).attr("src","img/site/cartNotSelected.png"); $(this).attr("selectit","false"); $(this).parents("tr.cartProductItemTR").css("background-color","#fff"); }); } else{ $("img.selectAllItem").attr("src","img/site/cartSelected.png"); $("img.selectAllItem").attr("selectit","selectit") $(".cartProductItemIfSelected").each(function(){ $(this).attr("src","img/site/cartSelected.png"); $(this).attr("selectit","selectit"); $(this).parents("tr.cartProductItemTR").css("background-color","#FFF8E1"); }); } syncCreateOrderButton(); calcCartSumPriceAndNumber(); }); $(".orderItemNumberSetting").keyup(function(){ var pid=$(this).attr("pid"); var stock= $("span.orderItemStock[pid="+pid+"]").text(); var price= $("span.orderItemPromotePrice[pid="+pid+"]").text(); var num= $(".orderItemNumberSetting[pid="+pid+"]").val(); num = parseInt(num); if(isNaN(num)) num= 1; if(num<=0) num = 1; if(num>stock) num = stock; syncPrice(pid,num,price); }); $(".numberPlus").click(function(){ var pid=$(this).attr("pid"); var stock= $("span.orderItemStock[pid="+pid+"]").text(); var price= $("span.orderItemPromotePrice[pid="+pid+"]").text(); var num= $(".orderItemNumberSetting[pid="+pid+"]").val(); num++; if(num>stock) num = stock; syncPrice(pid,num,price); }); $(".numberMinus").click(function(){ var pid=$(this).attr("pid"); var stock= $("span.orderItemStock[pid="+pid+"]").text(); var price= $("span.orderItemPromotePrice[pid="+pid+"]").text(); var num= $(".orderItemNumberSetting[pid="+pid+"]").val(); --num; if(num<=0) num=1; syncPrice(pid,num,price); }); $("button.createOrderButton").click(function(){ var params = ""; $(".cartProductItemIfSelected").each(function(){ if("selectit"==$(this).attr("selectit")){ var oiid = $(this).attr("oiid"); params += "&oiid="+oiid; } }); params = params.substring(1); location.href="buy?"+params; }); } function syncCreateOrderButton(){ var selectAny = false; $(".cartProductItemIfSelected").each(function(){ if("selectit"==$(this).attr("selectit")){ selectAny = true; } }); if(selectAny){ $("button.createOrderButton").css("background-color","#C40000"); $("button.createOrderButton").removeAttr("disabled"); } else{ $("button.createOrderButton").css("background-color","#AAAAAA"); $("button.createOrderButton").attr("disabled","disabled"); } } function syncSelect(){ var selectAll = true; $(".cartProductItemIfSelected").each(function(){ if("false"==$(this).attr("selectit")){ selectAll = false; } }); if(selectAll) $("img.selectAllItem").attr("src","img/site/cartSelected.png"); else $("img.selectAllItem").attr("src","img/site/cartNotSelected.png"); } function calcCartSumPriceAndNumber(){ var sum = 0; var totalNumber = 0; $("img.cartProductItemIfSelected[selectit='selectit']").each(function(){ var oiid = $(this).attr("oiid"); var price =$(".cartProductItemSmallSumPrice[oiid="+oiid+"]").text(); price = price.replace(/,/g, ""); price = price.replace(/¥/g, ""); sum += new Number(price); var num =$(".orderItemNumberSetting[oiid="+oiid+"]").val(); totalNumber += new Number(num); }); $("span.cartSumPrice").html("¥"+formatMoney(sum)); $("span.cartTitlePrice").html("¥"+formatMoney(sum)); $("span.cartSumNumber").html(totalNumber); } function syncPrice(pid,num,price){ $(".orderItemNumberSetting[pid="+pid+"]").val(num); var cartProductItemSmallSumPrice = formatMoney(num*price); $(".cartProductItemSmallSumPrice[pid="+pid+"]").html("¥"+cartProductItemSmallSumPrice); calcCartSumPriceAndNumber(); var url = "forechangeOrderItem?pid="+pid+"&num="+num; axios.get(url).then(function(response){ if(0!=response.data.code){ location.href="login"; } }); } </script> <title>购物车</title> <div class="cartDiv"> <div class="cartTitle pull-right"> <span>已选商品 (不含运费)</span> <span class="cartTitlePrice">¥0.00</span> <button class="createOrderButton" disabled="disabled">结 算</button> </div> <div class="cartProductList"> <table class="cartProductTable"> <thead> <tr> <th class="selectAndImage"> <img selectit="false" class="selectAllItem" src="img/site/cartNotSelected.png"> 全选 </th> <th>商品信息</th> <th>单价</th> <th>数量</th> <th width="120px">金额</th> <th class="operation">操作</th> </tr> </thead> <tbody> <tr :oiid="oi.id" class="cartProductItemTR" v-for="oi in orderItems"> <td> <img selectit="false" :oiid="oi.id" class="cartProductItemIfSelected" src="img/site/cartNotSelected.png"> <a style="display:none" href="#nowhere"><img src="img/site/cartSelected.png"></a> <img class="cartProductImg" :src="'img/productSingle_middle/'+oi.product.firstProductImage.id+'.jpg'"> </td> <td> <div class="cartProductLinkOutDiv"> <a :href="'product?pid='+oi.product.id" class="cartProductLink">{{oi.product.name}}</a> <div class="cartProductLinkInnerDiv"> <img src="img/site/creditcard.png" title="支持信用卡支付"> <img src="img/site/7day.png" title="消费者保障服务,承诺7天退货"> <img src="img/site/promise.png" title="消费者保障服务,承诺如实描述"> </div> </div> </td> <td> <span class="cartProductItemOringalPrice">¥{{oi.product.originalPrice|formatMoneyFilter}}</span> <span class="cartProductItemPromotionPrice">¥{{oi.product.promotePrice|formatMoneyFilter}}</span> </td> <td> <div class="cartProductChangeNumberDiv"> <span class="hidden orderItemStock " :pid="oi.product.id">{{oi.product.stock}}</span> <span class="hidden orderItemPromotePrice " :pid="oi.product.id">{{oi.product.promotePrice}}</span> <a :pid="oi.product.id" class="numberMinus" href="#nowhere">-</a> <input :pid="oi.product.id" :oiid="oi.id" class="orderItemNumberSetting" autocomplete="off" :value="oi.number"> <a :stock="oi.product.stock" :pid="oi.product.id" class="numberPlus" href="#nowhere">+</a> </div> </td> <td > <span class="cartProductItemSmallSumPrice" :oiid="oi.id" :pid="oi.product.id" > ¥ {{oi.product.promotePrice*oi.number|formatMoneyFilter}} </span> </td> <td> <a class="deleteOrderItem" :oiid="oi.id" href="#nowhere">删除</a> </td> </tr> </tbody> </table> </div> <div class="cartFoot"> <img selectit="false" class="selectAllItem" src="img/site/cartNotSelected.png"> <span>全选</span> <!-- <a href="#">删除</a> --> <div class="pull-right"> <span>已选商品 <span class="cartSumNumber" >0</span> 件</span> <span>合计 (不含运费): </span> <span class="cartSumPrice" >¥0.00</span> <button class="createOrderButton" disabled="disabled" >结 算</button> </div> </div> </div> </div>
步骤 6 :

modal4deleteConfirm.html

edit
增值内容,请先登录
完整的 Springboot 模仿天猫项目,使用 Springboot 、Vue.js、shiro、redis、elasticsearch 等一整套技术栈, 从无到有涵盖全部129个知识点,565个开发步骤, 充实 Springboot 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
<div th:fragment="html" > <div class="modal" id="deleteConfirmModal" tabindex="-1" role="dialog" > <div class="modal-dialog deleteConfirmModalDiv"> <div class="modal-content"> <div class="modal-header"> <button data-dismiss="modal" class="close" type="button"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button> <h4 class="modal-title">确认删除?</h4> </div> <div class="modal-footer"> <button data-dismiss="modal" class="btn btn-default" type="button">关闭</button> <button class="btn btn-primary deleteConfirmButton" id="submit" type="button">确认</button> </div> </div> </div> </div> </div>
<div th:fragment="html" >
	<div class="modal" id="deleteConfirmModal" tabindex="-1" role="dialog" >
			<div class="modal-dialog deleteConfirmModalDiv">
				<div class="modal-content">
					<div class="modal-header">
						<button data-dismiss="modal" class="close" type="button"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
						<h4 class="modal-title">确认删除?</h4>
					</div>
					<div class="modal-footer">
						<button data-dismiss="modal" class="btn btn-default" type="button">关闭</button>
						<button class="btn btn-primary deleteConfirmButton" id="submit" type="button">确认</button>
					</div>
				</div>
			</div>
		</div>
</div>
步骤 7 :

ForeRESTController.cart()

edit
增值内容,请先登录
完整的 Springboot 模仿天猫项目,使用 Springboot 、Vue.js、shiro、redis、elasticsearch 等一整套技术栈, 从无到有涵盖全部129个知识点,565个开发步骤, 充实 Springboot 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
@GetMapping("forecart") public Object cart(HttpSession session) { User user =(User) session.getAttribute("user"); List<OrderItem> ois = orderItemService.listByUser(user); productImageService.setFirstProdutImagesOnOrderItems(ois); return ois; }
package com.how2java.tmall.web; import com.how2java.tmall.comparator.*; import com.how2java.tmall.pojo.*; import com.how2java.tmall.service.*; import com.how2java.tmall.util.Result; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.util.HtmlUtils; import javax.servlet.http.HttpSession; import java.util.*; @RestController public class ForeRESTController { @Autowired CategoryService categoryService; @Autowired ProductService productService; @Autowired UserService userService; @Autowired ProductImageService productImageService; @Autowired PropertyValueService propertyValueService; @Autowired OrderItemService orderItemService; @Autowired ReviewService reviewService; @GetMapping("/forehome") public Object home() { List<Category> cs= categoryService.list(); productService.fill(cs); productService.fillByRow(cs); categoryService.removeCategoryFromProduct(cs); return cs; } @PostMapping("/foreregister") public Object register(@RequestBody User user) { String name = user.getName(); String password = user.getPassword(); name = HtmlUtils.htmlEscape(name); user.setName(name); boolean exist = userService.isExist(name); if(exist){ String message ="用户名已经被使用,不能使用"; return Result.fail(message); } user.setPassword(password); userService.add(user); return Result.success(); } @PostMapping("/forelogin") public Object login(@RequestBody User userParam, HttpSession session) { String name = userParam.getName(); name = HtmlUtils.htmlEscape(name); User user =userService.get(name,userParam.getPassword()); if(null==user){ String message ="账号密码错误"; return Result.fail(message); } else{ session.setAttribute("user", user); return Result.success(); } } @GetMapping("/foreproduct/{pid}") public Object product(@PathVariable("pid") int pid) { Product product = productService.get(pid); List<ProductImage> productSingleImages = productImageService.listSingleProductImages(product); List<ProductImage> productDetailImages = productImageService.listDetailProductImages(product); product.setProductSingleImages(productSingleImages); product.setProductDetailImages(productDetailImages); List<PropertyValue> pvs = propertyValueService.list(product); List<Review> reviews = reviewService.list(product); productService.setSaleAndReviewNumber(product); productImageService.setFirstProdutImage(product); Map<String,Object> map= new HashMap<>(); map.put("product", product); map.put("pvs", pvs); map.put("reviews", reviews); return Result.success(map); } @GetMapping("forecheckLogin") public Object checkLogin( HttpSession session) { User user =(User) session.getAttribute("user"); if(null!=user) return Result.success(); return Result.fail("未登录"); } @GetMapping("forecategory/{cid}") public Object category(@PathVariable int cid,String sort) { Category c = categoryService.get(cid); productService.fill(c); productService.setSaleAndReviewNumber(c.getProducts()); categoryService.removeCategoryFromProduct(c); if(null!=sort){ switch(sort){ case "review": Collections.sort(c.getProducts(),new ProductReviewComparator()); break; case "date" : Collections.sort(c.getProducts(),new ProductDateComparator()); break; case "saleCount" : Collections.sort(c.getProducts(),new ProductSaleCountComparator()); break; case "price": Collections.sort(c.getProducts(),new ProductPriceComparator()); break; case "all": Collections.sort(c.getProducts(),new ProductAllComparator()); break; } } return c; } @PostMapping("foresearch") public Object search( String keyword){ if(null==keyword) keyword = ""; List<Product> ps= productService.search(keyword,0,20); productImageService.setFirstProdutImages(ps); productService.setSaleAndReviewNumber(ps); return ps; } @GetMapping("forebuyone") public Object buyone(int pid, int num, HttpSession session) { return buyoneAndAddCart(pid,num,session); } private int buyoneAndAddCart(int pid, int num, HttpSession session) { Product product = productService.get(pid); int oiid = 0; User user =(User) session.getAttribute("user"); boolean found = false; List<OrderItem> ois = orderItemService.listByUser(user); for (OrderItem oi : ois) { if(oi.getProduct().getId()==product.getId()){ oi.setNumber(oi.getNumber()+num); orderItemService.update(oi); found = true; oiid = oi.getId(); break; } } if(!found){ OrderItem oi = new OrderItem(); oi.setUser(user); oi.setProduct(product); oi.setNumber(num); orderItemService.add(oi); oiid = oi.getId(); } return oiid; } @GetMapping("forebuy") public Object buy(String[] oiid,HttpSession session){ List<OrderItem> orderItems = new ArrayList<>(); float total = 0; for (String strid : oiid) { int id = Integer.parseInt(strid); OrderItem oi= orderItemService.get(id); total +=oi.getProduct().getPromotePrice()*oi.getNumber(); orderItems.add(oi); } productImageService.setFirstProdutImagesOnOrderItems(orderItems); session.setAttribute("ois", orderItems); Map<String,Object> map = new HashMap<>(); map.put("orderItems", orderItems); map.put("total", total); return Result.success(map); } @GetMapping("foreaddCart") public Object addCart(int pid, int num, HttpSession session) { buyoneAndAddCart(pid,num,session); return Result.success(); } @GetMapping("forecart") public Object cart(HttpSession session) { User user =(User) session.getAttribute("user"); List<OrderItem> ois = orderItemService.listByUser(user); productImageService.setFirstProdutImagesOnOrderItems(ois); return ois; } }


HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。


问答区域    
2022-07-05 产品数量在购物车更改以后,在购买界面的数量还是修改之前的数量
dyf137

产品数量在购物车更改以后,点击结算,在购买界面的数量还是修改之前的数量,这是不是不太对啊。购物车数量的修改不应该实时更新数据库订单项数量字段吗?




1 个答案

how2j
答案时间:2022-08-01
只能通过刷新页面罗,不可能实时更新,不然服务器压力得多大啊



回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到




2020-08-18 关于restful接口的问题
紫叶兰

站长,我想问一下为什么这里订单项修改和删除不用restful风格了呢?




1 个答案

how2j
答案时间:2020-08-19
活学活用嘛, restful 风格并不能提供所有的业务支持,有些还是要自己写。



回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到




2020-04-10 请问控制器为什么有时候返回result类包装后的数据,有时候直接返回数据呢?
2020-01-08 获取uesr为什么不能用requesbody
2019-10-01 会不会存在安全问题?


提问太多,页面渲染太慢,为了加快渲染速度,本页最多只显示几条提问。还有 2 条以前的提问,请 点击查看

提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
关于 实践项目-天猫整站Springboot-查看购物车页面 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 578362961
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: https://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图