飞道的博客

SpringBoot--网上商城项目(自定义的参数解析器、购物车后台&前台功能、商品详情页)

458人阅读  评论(0)

目录

一、自定义的参数解析器 

 关于Mybatis-plus时间字段代码生成问题

报错信息:Caused by: java.lang.IllegalStateException: No typehandler found for property xxx

二、购物车后台

        三、商品详情页

               四、商品详情页加入购物车

                       五、购物车删除功能

                               六、购物车修改功能 


一、自定义的参数解析器 

footer.html


  
  1. function check () {
  2. $.get( '/shopCar/check',{},function(rs){
  3. if(rs.code!= 200){
  4. alert( '请先登录后再购买商品!');
  5. } else
  6. location.href= '/shopCar/queryShopCar';
  7. }, 'json');
  8. }

ShopCarController

  
  1. package com.xiaokun.spbootpro.controller;
  2. import com.xiaokun.spbootpro.exception.BusinessException;
  3. import com.xiaokun.spbootpro.model.User;
  4. import com.xiaokun.spbootpro.service.IRedisService;
  5. import com.xiaokun.spbootpro.utils.JsonResponseBody;
  6. import com.xiaokun.spbootpro.utils.JsonResponseStatus;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.web.bind.annotation.CookieValue;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. /**
  12. * @author 小坤
  13. * @create 2022-11-09-10:19
  14. */
  15. @RestController
  16. @RequestMapping("/shopCar")
  17. public class ShopCarController {
  18. @Autowired
  19. private IRedisService redisService;
  20. @RequestMapping("/check")
  21. public JsonResponseBody check (@CookieValue("token") String token){
  22. if(token == null)
  23. throw new BusinessException(JsonResponseStatus.TOKEN_EEROR);
  24. User user=redisService.getUserToRedis(token);
  25. if(user == null)
  26. throw new BusinessException(JsonResponseStatus.TOKEN_EEROR);
  27. return new JsonResponseBody();
  28. }
  29. }

 关于Mybatis-plus时间字段代码生成问题

org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Cannot construct instance of `java.time.LocalDateTime` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
 at [Source: (byte[])"{"@class":"com.zking.testspbootpro.model.User","nickname":"小胖","password":"6502cbf0ac7d357831536b119ff26d28","salt":"7ceff545c6944e5cb7da355ae6243939","registerDate":{"month":"DECEMBER","year":2021,"dayOfMonth":11,"hour":2,"minute":36,"monthValue":12,"nano":0,"second":56,"dayOfWeek":"SATURDAY","dayOfYear":345,"chronology":{"@class":"java.time.chrono.IsoChronology","id":"ISO","calendarType":"iso8601"}},"lastLoginDate":null,"loginCount":0}"; line: 1, column: 172] (through reference chain: com.zking.testspbootpro.model.User["registerDate"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.LocalDateTime` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
 at [Source: (byte[])"{"@class":"com.zking.testspbootpro.model.User","nickname":"小胖","password":"6502cbf0ac7d357831536b119ff26d28","salt":"7ceff545c6944e5cb7da355ae6243939","registerDate":{"month":"DECEMBER","year":2021,"dayOfMonth":11,"hour":2,"minute":36,"monthValue":12,"nano":0,"second":56,"dayOfWeek":"SATURDAY","dayOfYear":345,"chronology":{"@class":"java.time.chrono.IsoChronology","id":"ISO","calendarType":"iso8601"}},"lastLoginDate":null,"loginCount":0}"; line: 1, column: 172] (through reference chain: com.zking.testspbootpro.model.User["registerDate"])

出现上述错误,原因是使用了lastLoginDate,改成java.util.Date;改完之后为了避免干扰将redis中的数据以及cookie(浏览器缓存)中的数据清空,再次登录测试;  

小编还遇到了一个错,可以看一下

报错信息:Caused by: java.lang.IllegalStateException: No typehandler found for property xxx

原因:

在相应的xml里的resultMap标签里的result标签里的property属性的值没有在实体类里找到,即property的值没有和实体类的属性名相对应,原因可能是写错了

解决办法:

进入相应的xml文件里,把该报错的值与实体类的属性对照,看是否写错了,如果写错则改成实体类有的属性

ShopCarController使用参数解析器之前的做法弊端
在每一个需要登录之后才能操作的功能,都需要做用户登录验证,即以下代码都需要写一遍

更改后的ShopCarController


  
  1. package com.xiaokun.spbootpro.controller;
  2. import com.xiaokun.spbootpro.exception.BusinessException;
  3. import com.xiaokun.spbootpro.model.User;
  4. import com.xiaokun.spbootpro.service.IRedisService;
  5. import com.xiaokun.spbootpro.utils.JsonResponseBody;
  6. import com.xiaokun.spbootpro.utils.JsonResponseStatus;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.web.bind.annotation.CookieValue;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. /**
  12. * @author 小坤
  13. * @create 2022-11-09-10:19
  14. */
  15. @RestController
  16. @RequestMapping("/shopCar")
  17. public class ShopCarController {
  18. @Autowired
  19. private IRedisService redisService;
  20. /**
  21. * 使用参数解析器之前的做法弊端
  22. * 在每一个需要登录之后才能操作的功能,都需要做用户登录验证,即以下代码都需要写一遍
  23. * @param token
  24. * @return
  25. */
  26. // @RequestMapping("/check")
  27. // public JsonResponseBody check(@CookieValue("token") String token){
  28. // if(token == null)
  29. // throw new BusinessException(JsonResponseStatus.TOKEN_EEROR);
  30. // User user=redisService.getUserToRedis(token);
  31. // if(user == null)
  32. // throw new BusinessException(JsonResponseStatus.TOKEN_EEROR);
  33. // return new JsonResponseBody();
  34. // }
  35. /**
  36. * 带了user,会自动进入参数解析器supportsParameter
  37. * @param user
  38. * @return
  39. */
  40. @RequestMapping("/check")
  41. public JsonResponseBody check (User user){
  42. return new JsonResponseBody();
  43. }
  44. }
参数解析器类


  
  1. package com.xiaokun.spbootpro.config;
  2. import com.xiaokun.spbootpro.exception.BusinessException;
  3. import com.xiaokun.spbootpro.model.User;
  4. import com.xiaokun.spbootpro.service.IRedisService;
  5. import com.xiaokun.spbootpro.utils.CookieUtils;
  6. import com.xiaokun.spbootpro.utils.JsonResponseStatus;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.core.MethodParameter;
  9. import org.springframework.stereotype.Component;
  10. import org.springframework.stereotype.Controller;
  11. import org.springframework.web.bind.support.WebDataBinderFactory;
  12. import org.springframework.web.context.request.NativeWebRequest;
  13. import org.springframework.web.method.support.HandlerMethodArgumentResolver;
  14. import org.springframework.web.method.support.ModelAndViewContainer;
  15. import org.springframework.web.servlet.HandlerExceptionResolver;
  16. import javax.servlet.http.HttpServletRequest;
  17. /**
  18. * @author 小坤
  19. * @create 2022-11-09-16:14
  20. *
  21. * 凡是实现HandlerMethodArgumentResolver接口的类都是参数解析器类
  22. */
  23. @Component
  24. public class UserArgumentResovler implements HandlerMethodArgumentResolver {
  25. @Autowired
  26. private IRedisService redisService;
  27. /**
  28. * supportsParameter的方法的返回值,true:则会调用下面resolveArgument
  29. * false:不调用
  30. * @param methodParameter
  31. * @return
  32. */
  33. @Override
  34. public boolean supportsParameter (MethodParameter methodParameter) {
  35. return methodParameter.getParameterType() == User.class;
  36. }
  37. /**
  38. * resolveArgument:具体的业务代码处理
  39. * @param methodParameter
  40. * @param modelAndViewContainer
  41. * @param nativeWebRequest
  42. * @param webDataBinderFactory
  43. * @return
  44. * @throws Exception
  45. */
  46. @Override
  47. public Object resolveArgument (MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
  48. HttpServletRequest request=(HttpServletRequest)nativeWebRequest.getNativeRequest();
  49. String token=CookieUtils.getCookieValue(request, "token");
  50. if(token == null)
  51. throw new BusinessException(JsonResponseStatus.TOKEN_EEROR);
  52. User user=redisService.getUserToRedis(token);
  53. if(user == null)
  54. throw new BusinessException(JsonResponseStatus.TOKEN_EEROR);
  55. return user;
  56. }
  57. }
WebMvcConfigurer 资源映射器、相当于web.xml  

WebMvcConfigurer添加之后,会覆盖application.yml中的静态资源映射⬇⬇

所以需要重新去添加配置,固定配置,按需拷贝咯

  
  1. package com.xiaokun.spbootpro.config;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.web.method.support.HandlerMethodArgumentResolver;
  5. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  6. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  7. import java.util.List;
  8. /**
  9. * @author 小坤
  10. * @create 2022-11-09-16:41
  11. *
  12. * WebMvcConfigurer添加之后,会覆盖application.yml中的静态资源映射
  13. */
  14. @Configuration
  15. public class WebConfig implements WebMvcConfigurer {
  16. @Autowired
  17. private UserArgumentResovler userArgumentResovler;
  18. /**
  19. * 配置静态资源访问映射,使用了WebMvcConfigurer会覆盖原有的application.yml文件中的静态资源配置
  20. * @param registry
  21. */
  22. @Override
  23. public void addResourceHandlers (ResourceHandlerRegistry registry) {
  24. registry.addResourceHandler( "/static/**")
  25. .addResourceLocations( "classpath:/static/");
  26. }
  27. /**
  28. * 添加自定义的参数解析器
  29. * @param resolvers
  30. */
  31. @Override
  32. public void addArgumentResolvers (List<HandlerMethodArgumentResolver> resolvers) {
  33. resolvers.add(userArgumentResovler);
  34. }
  35. }

凡是controller中的方法中包含参数User,都会进参数解析器UserArgumentResovler中的resolveArgument方法;这样一定程度下可以减少用户信息登录检验;

当然,我们也可以通过拦截器、过滤器、aop等方式,来解决这一类问题

修改配置类后建议重启一下项目 !!!

我在user中加了一个id字段

 二、购物车后台

定义购物车对象ShopCar

 1.1购物车中商品集合
        定义购物车商品详情对象ShopCarItem
        商品ID/商品名称/商品单价/商品图片/商品数量/小计计算方法
1.2加入购物车
1.3删除购物车中指定商品
1.4更新购物车中商品数量
1.5清空购物车
1.6总价计算


  
  1. package com.xiaokun.spbootpro.model.vo;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.ListIterator;
  6. /**
  7. * @author 小坤
  8. * @create 2022-11-10-14:49
  9. *
  10. * 购物车对象 vo:view object
  11. *
  12. */
  13. public class ShopCar {
  14. // 1.1购物车中商品集合
  15. private List<ShopCarItem> items= new ArrayList<>();
  16. //set get方法
  17. public List<ShopCarItem> getItems () {
  18. return items;
  19. }
  20. public void setItems (List<ShopCarItem> items) {
  21. this.items = items;
  22. }
  23. //增加
  24. //1.2加入购物车
  25. public void add (ShopCarItem shopCarItem){
  26. //循环遍历购物车集合
  27. for (ShopCarItem item : items) {
  28. //判断加入购物车中的商品ID与购物车中的商品ID是否一致
  29. if (item.getGid().equals(shopCarItem.getGid())) {
  30. //获取购物车中原有商品的数量,再进行+1
  31. Integer num = item.getQuantity();
  32. item.setQuantity(num + 1);
  33. return;
  34. }
  35. }
  36. //加入购物车
  37. items.add(shopCarItem);
  38. }
  39. //删除
  40. //1.3删除购物车中指定商品
  41. public void delete (String gids) {
  42. //将gids分割后转换成List集合
  43. List<String> ids = Arrays.asList(gids.split( ","));
  44. //获取商品集合迭代器对象
  45. ListIterator<ShopCarItem> it = items.listIterator();
  46. //循环遍历迭代器
  47. while (it.hasNext()) {
  48. //获取迭代器元素并移动下标
  49. ShopCarItem shopCarItem = it.next();
  50. //判断购物车中的商品ID是否在被删除商品的ID集合中
  51. if (ids.contains(shopCarItem.getGid() + "")) {
  52. //删除商品
  53. it.remove();
  54. }
  55. }
  56. }
  57. //修改
  58. //1.4更新购物车中商品数量
  59. public void update (ShopCarItem shopCarItem){
  60. //循环遍历购物车集合
  61. for (ShopCarItem item : items) {
  62. if (shopCarItem.getGid().equals(item.getGid())){
  63. item.setQuantity(shopCarItem.getQuantity());
  64. }
  65. }
  66. }
  67. // 1.5清空购物车
  68. public void clear () {
  69. items.clear();
  70. }
  71. // 1.6总价计算
  72. // public BigDecimal total() {
  73. // BigDecimal total = new BigDecimal(0);
  74. // for (ShopCarItem item : items) {
  75. // total = total.add(item.smalltotal());
  76. // }
  77. // return total;
  78. // }
  79. }


  
  1. package com.xiaokun.spbootpro.model.vo;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. import java.awt.*;
  6. import java.math.BigDecimal;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. /**
  10. * @author 小坤
  11. * @create 2022-11-10-14:50
  12. *
  13. * 购物车明细
  14. */
  15. @Data
  16. @NoArgsConstructor
  17. @AllArgsConstructor
  18. public class ShopCarItem {
  19. //商品id、商品名称、商品单价、商品数量、商品图片
  20. private Long gid;
  21. private String goodsName;
  22. private String goodsImg;
  23. private BigDecimal goodsPrice;
  24. private Integer quantity;
  25. /**
  26. * 这是个虚拟方法,用于计算商品的小计
  27. * 公式:商品的单价*数量=小计
  28. * @return
  29. */
  30. public BigDecimal smalltotal (){
  31. BigDecimal num= new BigDecimal(quantity);
  32. return goodsPrice.multiply(num);
  33. }
  34. }
web层定义 ShopCarController

1) 从session中获取购物车对象ShopCar
        注:根据当前登陆用户ID绑定购物车,确保一人一车
2) 加入购物车方法
3) 查询购物车商品方法
4) 删除购物车指定商品方法
5) 更新购物车商品数量方法


  
  1. package com.xiaokun.spbootpro.controller;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.xiaokun.spbootpro.exception.BusinessException;
  4. import com.xiaokun.spbootpro.model.Goods;
  5. import com.xiaokun.spbootpro.model.User;
  6. import com.xiaokun.spbootpro.model.vo.ShopCar;
  7. import com.xiaokun.spbootpro.model.vo.ShopCarItem;
  8. import com.xiaokun.spbootpro.service.IGoodsService;
  9. import com.xiaokun.spbootpro.service.IRedisService;
  10. import com.xiaokun.spbootpro.utils.JsonResponseBody;
  11. import com.xiaokun.spbootpro.utils.JsonResponseStatus;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.stereotype.Controller;
  14. import org.springframework.web.bind.annotation.CookieValue;
  15. import org.springframework.web.bind.annotation.RequestMapping;
  16. import org.springframework.web.bind.annotation.ResponseBody;
  17. import org.springframework.web.bind.annotation.RestController;
  18. import org.springframework.web.servlet.ModelAndView;
  19. import javax.servlet.http.HttpServletRequest;
  20. import javax.servlet.http.HttpServletResponse;
  21. import javax.servlet.http.HttpSession;
  22. /**
  23. * @author 小坤
  24. * @create 2022-11-09-10:19
  25. */
  26. @Controller
  27. @RequestMapping("/shopCar")
  28. public class ShopCarController {
  29. @Autowired
  30. private IRedisService redisService;
  31. @Autowired
  32. private IGoodsService goodsService;
  33. /**
  34. * 使用参数解析器之前的做法弊端
  35. * 在每一个需要登录之后才能操作的功能,都需要做用户登录验证,即以下代码都需要写一遍
  36. * @param token
  37. * @return
  38. */
  39. // @RequestMapping("/check")
  40. // public JsonResponseBody check(@CookieValue("token") String token){
  41. // if(token == null)
  42. // throw new BusinessException(JsonResponseStatus.TOKEN_EEROR);
  43. // User user=redisService.getUserToRedis(token);
  44. // if(user == null)
  45. // throw new BusinessException(JsonResponseStatus.TOKEN_EEROR);
  46. // return new JsonResponseBody();
  47. // }
  48. /**
  49. * 带了user,会自动进入参数解析器supportsParameter
  50. * @param user
  51. * @return
  52. */
  53. @RequestMapping("/check")
  54. @ResponseBody
  55. public JsonResponseBody check (User user){
  56. return new JsonResponseBody();
  57. }
  58. //私有方法 从session获取购物对象
  59. private ShopCar getShopCar (User user,HttpServletRequest request){
  60. HttpSession session=request.getSession();
  61. ShopCar shopCar = (ShopCar) session.getAttribute(user.getId() + "_shopCar");
  62. if (shopCar== null){
  63. shopCar= new ShopCar();
  64. session.setAttribute(user.getId() + "_shopCar",shopCar);
  65. }
  66. return shopCar;
  67. }
  68. //查询
  69. @RequestMapping("/queryShopCar")
  70. public ModelAndView queryShopCar (User user,
  71. HttpServletRequest request,
  72. HttpServletResponse response){
  73. ModelAndView mv= new ModelAndView();
  74. ShopCar shopCar=getShopCar(user,request);
  75. mv.addObject( "shopCar",shopCar);
  76. mv.setViewName( "cart.html");
  77. return mv;
  78. }
  79. //增加
  80. @RequestMapping("/add")
  81. @ResponseBody
  82. public JsonResponseBody add (User user,
  83. HttpServletRequest request,
  84. HttpServletResponse response,long gid){
  85. ModelAndView mv= new ModelAndView();
  86. ShopCar shopCar=getShopCar(user,request);
  87. Goods goods = goodsService.getOne( new QueryWrapper<Goods>().eq( "gid", gid));
  88. //初始化商品详情ShopCarItem
  89. ShopCarItem item= new ShopCarItem();
  90. item.setQuantity( 1);
  91. item.setGid(goods.getGid());
  92. item.setGoodsImg(goods.getGoodsImg());
  93. item.setGoodsName(goods.getGoodsName());
  94. item.setGoodsPrice(goods.getGoodsPrice());
  95. //加入购物车
  96. shopCar.add(item);
  97. return new JsonResponseBody();
  98. }
  99. //修改
  100. @RequestMapping("/update")
  101. @ResponseBody
  102. public JsonResponseBody update (User user,
  103. HttpServletRequest request,
  104. HttpServletResponse response,ShopCarItem shopCarItem){
  105. ModelAndView mv= new ModelAndView();
  106. ShopCar shopCar=getShopCar(user,request);
  107. shopCar.update(shopCarItem);
  108. return new JsonResponseBody();
  109. }
  110. //删除
  111. @RequestMapping("/delete")
  112. @ResponseBody
  113. public JsonResponseBody delete (User user,
  114. HttpServletRequest request,
  115. HttpServletResponse response,String gids){
  116. ModelAndView mv= new ModelAndView();
  117. ShopCar shopCar=getShopCar(user,request);
  118. shopCar.delete(gids);
  119. return new JsonResponseBody();
  120. }
  121. }

三、商品详情页

修改index.html

商品详情页的模板跳转地址如下

${ctx}/page/proDetail.html  先使用这个跳转地址,看是否可以跳转

index.html 


  
  1. <!DOCTYPE html>
  2. <html>
  3. <head lang="en">
  4. <#include "common/head.html">
  5. <link rel="stylesheet" type="text/css" href="css/public.css"/>
  6. <link rel="stylesheet" type="text/css" href="css/index.css" />
  7. </head>
  8. <body>
  9. <!------------------------------head------------------------------>
  10. <#include "common/top.html">
  11. <!-------------------------banner--------------------------->
  12. <div class="block_home_slider">
  13. <div id="home_slider" class="flexslider">
  14. <ul class="slides">
  15. <li>
  16. <div class="slide">
  17. <img src="img/banner2.jpg"/>
  18. </div>
  19. </li>
  20. <li>
  21. <div class="slide">
  22. <img src="img/banner1.jpg"/>
  23. </div>
  24. </li>
  25. </ul>
  26. </div>
  27. </div>
  28. <!------------------------------thImg------------------------------>
  29. <div class="thImg">
  30. <div class="clearfix">
  31. <a href="${ctx}/page/vase_proList.html"> <img src="img/i1.jpg"/> </a>
  32. <a href="${ctx}/page/proList.html"> <img src="img/i2.jpg"/> </a>
  33. <a href="#2"> <img src="img/i3.jpg"/> </a>
  34. </div>
  35. </div>
  36. <!------------------------------news------------------------------>
  37. <div class="news">
  38. <div class="wrapper">
  39. <h2> <img src="img/ih1.jpg"/> </h2>
  40. <div class="top clearfix">
  41. <a href="${ctx}/page/proDetail.html"> <img src="img/n1.jpg"/> <p> </p> </a>
  42. <a href="${ctx}/page/proDetail.html"> <img src="img/n2.jpg"/> <p> </p> </a>
  43. <a href="${ctx}/page/proDetail.html"> <img src="img/n3.jpg"/> <p> </p> </a>
  44. </div>
  45. <div class="bott clearfix">
  46. <a href="${ctx}/page/proDetail.html"> <img src="img/n4.jpg"/> <p> </p> </a>
  47. <a href="${ctx}/page/proDetail.html"> <img src="img/n5.jpg"/> <p> </p> </a>
  48. <a href="${ctx}/page/proDetail.html"> <img src="img/n6.jpg"/> <p> </p> </a>
  49. </div>
  50. <h2> <img src="img/ih2.jpg"/> </h2>
  51. <#if gt01?? && gt01?size gt 0>
  52. <#list gt01?keys as key>
  53. <div class="flower clearfix tran">
  54. <!--遍历gt01中所有的key,是为了该key中的数组对象-->
  55. <#list gt01[key] as g>
  56. <a href="${ctx}/page/proDetail.html" class="clearfix">
  57. <dl>
  58. <dt>
  59. <span class="abl"> </span>
  60. <img src="${g.goodsImg}"/>
  61. <span class="abr"> </span>
  62. </dt>
  63. <dd>${g.goodsName} </dd>
  64. <dd> <span>¥ ${g.goodsPrice} </span> </dd>
  65. </dl>
  66. </a>
  67. </#list>
  68. </div>
  69. </#list>
  70. </#if>
  71. </div>
  72. </div>
  73. <!------------------------------ad------------------------------>
  74. <a href="#" class="ad"> <img src="img/ib1.jpg"/> </a>
  75. <!------------------------------people------------------------------>
  76. <div class="people">
  77. <div class="wrapper">
  78. <h2> <img src="img/ih3.jpg"/> </h2>
  79. <#if gt07?? && gt07?size gt 0>
  80. <#list gt07?keys as key>
  81. <div class="pList clearfix tran">
  82. <#list gt07[key] as g>
  83. <a href="${ctx}/page/proDetail.html">
  84. <dl>
  85. <dt>
  86. <span class="abl"> </span>
  87. <img src="${g.goodsImg}"/>
  88. <span class="abr"> </span>
  89. </dt>
  90. <dd>${g.goodsName} </dd>
  91. <dd> <span>¥${g.goodsPrice} </span> </dd>
  92. </dl>
  93. </a>
  94. </#list>
  95. </div>
  96. </#list>
  97. </#if>
  98. </div>
  99. </div>
  100. <#include "common/footer.html"/>
  101. <script src="js/public.js" type="text/javascript" charset="utf-8"> </script>
  102. <script src="js/nav.js" type="text/javascript" charset="utf-8"> </script>
  103. <script src="js/jquery.flexslider-min.js" type="text/javascript" charset="utf-8"> </script>
  104. <script type="text/javascript">
  105. $( function( ) {
  106. $( '#home_slider'). flexslider({
  107. animation: 'slide',
  108. controlNav: true,
  109. directionNav: true,
  110. animationLoop: true,
  111. slideshow: true,
  112. slideshowSpeed: 2000,
  113. useCSS: false
  114. });
  115. });
  116. </script>
  117. </body>
  118. </html>

可以跳转后,我们需要跳转后台controller加载数据

将对应controller层的代码编写好后,修改index.html中详情页的模板跳转地址${ctx}/goods/detail/${g.gid}

controller代码在下面


  
  1. <#if gt01?? && gt01?size gt 0>
  2. <#list gt01?keys as key>
  3. <div class="flower clearfix tran">
  4. <#list gt01[key] as g>
  5. <a href="${ctx}/goods/detail/${g.gid}" class="clearfix">
  6. <dl>
  7. <dt>
  8. <span class="abl"> </span>
  9. <img src="${g.goodsImg}"/>
  10. <span class="abr"> </span>
  11. </dt>
  12. <dd>${g.goodsName} </dd>
  13. <dd> <span>¥ ${g.goodsPrice} </span> </dd>
  14. </dl>
  15. </a>
  16. </#list>
  17. </div>
  18. </#list>
  19. </#if>

  
  1. <#if gt07?? && gt01?size gt 0>
  2. <#list gt07?keys as key>
  3. <div class="pList clearfix tran">
  4. <#list gt07[key] as g>
  5. <a href="${ctx}/goods/detail/${g.gid}">
  6. <dl>
  7. <dt>
  8. <span class="abl"> </span>
  9. <img src="${g.goodsImg}"/>
  10. <span class="abr"> </span>
  11. </dt>
  12. <dd>${g.goodsName} </dd>
  13. <dd> <span>¥${g.goodsPrice} </span> </dd>
  14. </dl>
  15. </a>
  16. </#list>
  17. </div>
  18. </#list>
  19. </#if>

 proDetail.html 商品详情页

跳转详情页不能返回RestController 使用ModelAndView所以必须换成Controller

GoodsController

  
  1. package com.xiaokun.spbootpro.controller;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.xiaokun.spbootpro.model.Goods;
  4. import com.xiaokun.spbootpro.service.IGoodsService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.web.bind.annotation.PathVariable;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import org.springframework.web.servlet.ModelAndView;
  11. /**
  12. * <p>
  13. * 商品信息表 前端控制器
  14. * </p>
  15. *
  16. * @author xiaokun
  17. * @since 2022-11-09
  18. */
  19. @Controller
  20. @RequestMapping("/goods")
  21. public class GoodsController {
  22. @Autowired
  23. private IGoodsService goodsService;
  24. @RequestMapping("/detail/{gid}")
  25. public ModelAndView detail (@PathVariable("gid") long gid){
  26. //根据商品ID查询单个商品信息
  27. Goods goods = goodsService.getOne( new QueryWrapper<Goods>().eq( "gid", gid));
  28. //将商品存入model带入前端展示
  29. ModelAndView mv= new ModelAndView();
  30. mv.addObject( "goods",goods);
  31. //设置跳转页面,商品详情页
  32. mv.setViewName( "proDetail.html");
  33. return mv;
  34. }
  35. }

 四、商品详情页加入购物车

点击加入购物车,商品详情页跳转到购物车页面并商品显示在购物车中 

加入并展示购物车


  
  1. $(function () {
  2. $( ".cart").click(function () {
  3. $.get( '/shopCar/add',{gid:$( "#gids").val()},function(rs){
  4. if(rs.code!= 200){
  5. alert( '请先登录后再加购商品!');
  6. } else
  7. location.href= '/page/cart.html';
  8. }, 'json');
  9. });
  10. })

 添加这行

这样点击商品,就跳转到购物车了,接下来把加入购物车中的商品变活


  
  1. <#if shopCar??>
  2. <#list shopCar.items as g>
  3. <div class="th">
  4. <div class="pro clearfix">
  5. <label class="fl">
  6. <input type="hidden" value="${g.gid!}"/>
  7. <input type="checkbox"/>
  8. <span> </span>
  9. </label>
  10. <a class="fl" href="#">
  11. <dl class="clearfix">
  12. <dt class="fl"> <img style="width: 120px;height: 120px;" src="${g.goodsImg}"> </dt>
  13. <dd class="fl">
  14. <p>${g.goodsName} </p>
  15. <p>颜色分类: </p>
  16. <p>白色瓷瓶+白色串枚 </p>
  17. </dd>
  18. </dl>
  19. </a>
  20. </div>
  21. <div class="price">¥${g.goodsPrice} </div>
  22. <div class="number">
  23. <p class="num clearfix">
  24. <img class="fl sub" src="img/temp/sub.jpg">
  25. <span class="fl">${g.quantity} </span>
  26. <img class="fl add" src="img/temp/add.jpg">
  27. </p>
  28. </div>
  29. <div class="price sAll">¥${g.smalltotal()} </div>
  30. <div class="price"> <a class="del" href="javascript:void(0)">删除 </a> </div>
  31. </div>
  32. </#list>
  33. </#if>

 即增加又查询

 这是我刚刚加入购物车的商品,数据变活ok

五、购物车删除功能

删除购物车中指定商品

1)点击删除按钮删除单个商品
2)全选商品删除部分或者全部商品

 原cart.js


  
  1. $(function(){
  2. /**************数量加减***************/
  3. $( ".num .sub").click(function(){
  4. var num = parseInt($( this).siblings( "span").text());
  5. if(num<= 1){
  6. $( this).attr( "disabled", "disabled");
  7. } else{
  8. num--;
  9. $( this).siblings( "span").text(num);
  10. //获取除了货币符号以外的数字
  11. var price = $( this).parents( ".number").prev().text().substring( 1);
  12. //单价和数量相乘并保留两位小数
  13. $( this).parents( ".th").find( ".sAll").text( '¥'+(num*price).toFixed( 2));
  14. jisuan();
  15. zg();
  16. }
  17. });
  18. $( ".num .add").click(function(){
  19. var num = parseInt($( this).siblings( "span").text());
  20. if(num>= 5){
  21. confirm( "限购5件");
  22. } else{
  23. num++;
  24. $( this).siblings( "span").text(num);
  25. var price = $( this).parents( ".number").prev().text().substring( 1);
  26. $( this).parents( ".th").find( ".sAll").text( '¥'+(num*price).toFixed( 2));
  27. jisuan();
  28. zg();
  29. }
  30. });
  31. //计算总价
  32. function jisuan (){
  33. var all= 0;
  34. var len =$( ".th input[type='checkbox']:checked").length;
  35. if(len== 0){
  36. $( "#all").text( '¥'+parseFloat( 0).toFixed( 2));
  37. } else{
  38. $( ".th input[type='checkbox']:checked").each(function(){
  39. //获取小计里的数值
  40. var sAll = $( this).parents( ".pro").siblings( '.sAll').text().substring( 1);
  41. //累加
  42. all+=parseFloat(sAll);
  43. //赋值
  44. $( "#all").text( '¥'+all.toFixed( 2));
  45. })
  46. }
  47. }
  48. //计算总共几件商品
  49. function zg (){
  50. var zsl = 0;
  51. var index = $( ".th input[type='checkbox']:checked").parents( ".th").find( ".num span");
  52. var len =index.length;
  53. if(len== 0){
  54. $( "#sl").text( 0);
  55. } else{
  56. index.each(function(){
  57. zsl+=parseInt($( this).text());
  58. $( "#sl").text(zsl);
  59. })
  60. }
  61. if($( "#sl").text()> 0){
  62. $( ".count").css( "background", "#c10000");
  63. } else{
  64. $( ".count").css( "background", "#8e8e8e");
  65. }
  66. }
  67. /*****************商品全选***********************/
  68. $( "input[type='checkbox']").on( 'click',function(){
  69. var sf = $( this).is( ":checked");
  70. var sc= $( this).hasClass( "checkAll");
  71. if(sf){
  72. if(sc){
  73. $( "input[type='checkbox']").each(function(){
  74. this.checked= true;
  75. });
  76. zg();
  77. jisuan();
  78. } else{
  79. $( this).checked= true;
  80. var len = $( "input[type='checkbox']:checked").length;
  81. var len1 = $( "input").length- 1;
  82. if(len==len1){
  83. $( "input[type='checkbox']").each(function(){
  84. this.checked= true;
  85. });
  86. }
  87. zg();
  88. jisuan();
  89. }
  90. } else{
  91. if(sc){
  92. $( "input[type='checkbox']").each(function(){
  93. this.checked= false;
  94. });
  95. zg();
  96. jisuan();
  97. } else{
  98. $( this).checked= false;
  99. var len = $( ".th input[type='checkbox']:checked").length;
  100. var len1 = $( "input").length- 1;
  101. if(len<len1){
  102. $( '.checkAll').attr( "checked", false);
  103. }
  104. zg();
  105. jisuan();
  106. }
  107. }
  108. });
  109. /****************************proDetail 加入购物车*******************************/
  110. $( ".btns .cart").click(function(){
  111. if($( ".categ p").hasClass( "on")){
  112. var num = parseInt($( ".num span").text());
  113. var num1 = parseInt($( ".goCart span").text());
  114. $( ".goCart span").text(num+num1);
  115. }
  116. });
  117. //删除购物车商品
  118. $( '.del').click(function(){
  119. //单个删除
  120. if($( this).parent().parent().hasClass( "th")){
  121. $( ".mask").show();
  122. $( ".tipDel").show();
  123. index = $( this).parents( ".th").index()- 1;
  124. $( '.cer').click(function(){
  125. $( ".mask").hide();
  126. $( ".tipDel").hide();
  127. $( ".th").eq(index).remove();
  128. $( '.cer').off( 'click');
  129. if($( ".th").length== 0){
  130. $( ".table .goOn").show();
  131. }
  132. })
  133. } else{
  134. //选中多个一起删除
  135. if($( ".th input[type='checkbox']:checked").length== 0){
  136. $( ".mask").show();
  137. $( ".pleaseC").show();
  138. }
  139. else{
  140. $( ".mask").show();
  141. $( ".tipDel").show();
  142. $( '.cer').click(function(){
  143. $( ".th input[type='checkbox']:checked").each(function(j){
  144. index = $( this).parents( '.th').index()- 1;
  145. $( ".th").eq(index).remove();
  146. if($( ".th").length== 0){
  147. $( ".table .goOn").show();
  148. }
  149. })
  150. $( ".mask").hide();
  151. $( ".tipDel").hide();
  152. zg();
  153. jisuan();
  154. })
  155. }
  156. }
  157. })
  158. $( '.cancel').click(function(){
  159. $( ".mask").hide();
  160. $( ".tipDel").hide();
  161. })
  162. //改变商品规格
  163. // $(".pro dd").hover(function(){
  164. // var html='';
  165. // html='<span class="edit">修改</span>';
  166. // $(this).addClass("on").append(html).parents(".th").siblings(".th").find(".pro dd").removeClass("on").find('.edit').remove();
  167. // $(".edit").each(function(i){
  168. // $(this).attr("id",'edit'+i);
  169. // $("#edit"+i).click(function(){
  170. // $(".proDets").show();
  171. // $(".mask").show();
  172. // $(".changeBtn .buy").attr("data-id",i);
  173. // })
  174. // })
  175. // },function(){
  176. // $(this).removeClass("on");
  177. // })
  178. // $(".changeBtn .buy").click(function(){
  179. // var index = $(this).attr("data-id");
  180. // var result = $(".smallImg .on").find("img").attr("alt");
  181. // $("#edit"+index).prev().text(result);
  182. // $(".proDets").hide();
  183. // $(".mask").hide();
  184. // $("#edit"+index).parent("dd").removeClass("on").find(".edit").remove();
  185. // });
  186. // $(".changeBtn .cart").click(function(){
  187. // $(".proDets").hide();
  188. // $(".mask").hide();
  189. // })
  190. })

cart.js进行整改


  
  1. //删除购物车商品
  2. $( '.del').click(function(){
  3. let gids = "";
  4. //单个删除
  5. if($( this).parent().parent().hasClass( "th")){
  6. $( ".mask").show();
  7. $( ".tipDel").show();
  8. index = $( this).parents( ".th").index()- 1;
  9. //TODO 获取当前的checkbox中设置的隐藏域(包含了商品ID)
  10. gids=$( ".th").eq(index).find( 'div:eq(0) input[type=hidden]').val();
  11. console.log(gids);
  12. $( '.cer').click(function(){
  13. $( ".mask").hide();
  14. $( ".tipDel").hide();
  15. $( ".th").eq(index).remove();
  16. $( '.cer').off( 'click');
  17. if($( ".th").length== 0){
  18. $( ".table .goOn").show();
  19. }
  20. //TODO
  21. del(gids);
  22. })
  23. } else{
  24. //选中多个一起删除
  25. if($( ".th input[type='checkbox']:checked").length== 0){
  26. $( ".mask").show();
  27. $( ".pleaseC").show();
  28. }
  29. else{
  30. $( ".mask").show();
  31. $( ".tipDel").show();
  32. //TODO 先获取所有即将被删除的商品ID
  33. let gidArr = new Array();
  34. $( ".th input[type='checkbox']:checked").each(function(j) {
  35. index = $( this).parents( '.th').index() - 1;
  36. //在这里需要获取当前行商品的商品ID
  37. gidArr.push($( ".th").eq(index).find( "div:eq(0) input[type='hidden']").val());
  38. });
  39. gids = gidArr.join( ",");
  40. console.log(gids);
  41. $( '.cer').click(function(){
  42. $( ".th input[type='checkbox']:checked").each(function(j){
  43. index = $( this).parents( '.th').index()- 1;
  44. $( ".th").eq(index).remove();
  45. if($( ".th").length== 0){
  46. $( ".table .goOn").show();
  47. }
  48. })
  49. $( ".mask").hide();
  50. $( ".tipDel").hide();
  51. zg();
  52. jisuan();
  53. //TODO
  54. del(gids);
  55. })
  56. }
  57. }
  58. })
  59. #在页面初始化事件外添加删除方法
  60. //删除商品
  61. function del (gids){
  62. $.post( '/shopCar/delete',{
  63. 'gids':gids
  64. },function(rs){
  65. if(rs.code!= 200)
  66. alert(rs.msg);
  67. else
  68. location.href= '/shopCar/queryShopCar';
  69. }, 'json');
  70. }

六、购物车修改功能 

修改购物车中商品数量

 cart.js进行整改


  
  1. $( ".num .sub").click(function(){
  2. var num = parseInt($( this).siblings( "span").text());
  3. if(num<= 1){
  4. $( this).attr( "disabled", "disabled");
  5. } else{
  6. num--;
  7. $( this).siblings( "span").text(num);
  8. //获取除了货币符号以外的数字
  9. var price = $( this).parents( ".number").prev().text().substring( 1);
  10. //单价和数量相乘并保留两位小数
  11. $( this).parents( ".th").find( ".sAll").text( '¥'+(num*price).toFixed( 2));
  12. jisuan();
  13. zg();
  14. //TODO 获取当前行的行索引
  15. let index = $( this).parents( ".th").index()- 1;
  16. //获取当前的checkbox中设置的隐藏域(包含了商品ID)
  17. let gid=$( ".th").eq(index).find( 'div:eq(0) input[type=hidden]').val();
  18. update(num,gid);
  19. }
  20. });
  21. $( ".num .add").click(function(){
  22. var num = parseInt($( this).siblings( "span").text());
  23. if(num>= 5){
  24. confirm( "限购5件");
  25. } else{
  26. num++;
  27. $( this).siblings( "span").text(num);
  28. var price = $( this).parents( ".number").prev().text().substring( 1);
  29. $( this).parents( ".th").find( ".sAll").text( '¥'+(num*price).toFixed( 2));
  30. jisuan();
  31. zg();
  32. //TODO 获取当前行的行索引
  33. let index = $( this).parents( ".th").index()- 1;
  34. //获取当前的checkbox中设置的隐藏域(包含了商品ID)
  35. let gid=$( ".th").eq(index).find( 'div:eq(0) input[type=hidden]').val();
  36. update(num,gid);
  37. }
  38. });
  39. #在页面初始化事件外添加修改方法
  40. //更新商品数量
  41. function update (num,gid){
  42. $.post( '/shopCar/update',{
  43. 'gid':gid,
  44. 'quantity':num
  45. },function(rs){
  46. if(rs.code!= 200)
  47. alert(rs.msg);
  48. else
  49. location.href= '/shopCar/queryShopCar';
  50. }, 'json');
  51. }

最终版


  
  1. $(function(){
  2. /**************数量加减***************/
  3. $( ".num .sub").click(function(){
  4. var num = parseInt($( this).siblings( "span").text());
  5. if(num<= 1){
  6. $( this).attr( "disabled", "disabled");
  7. } else{
  8. num--;
  9. $( this).siblings( "span").text(num);
  10. //获取除了货币符号以外的数字
  11. var price = $( this).parents( ".number").prev().text().substring( 1);
  12. //单价和数量相乘并保留两位小数
  13. $( this).parents( ".th").find( ".sAll").text( '¥'+(num*price).toFixed( 2));
  14. jisuan();
  15. zg();
  16. //TODO 获取当前行的行索引
  17. let index = $( this).parents( ".th").index()- 1;
  18. //获取当前的checkbox中设置的隐藏域(包含了商品ID)
  19. let gid=$( ".th").eq(index).find( 'div:eq(0) input[type=hidden]').val();
  20. update(num,gid);
  21. }
  22. });
  23. $( ".num .add").click(function(){
  24. var num = parseInt($( this).siblings( "span").text());
  25. if(num>= 5){
  26. confirm( "限购5件");
  27. } else{
  28. num++;
  29. $( this).siblings( "span").text(num);
  30. var price = $( this).parents( ".number").prev().text().substring( 1);
  31. $( this).parents( ".th").find( ".sAll").text( '¥'+(num*price).toFixed( 2));
  32. jisuan();
  33. zg();
  34. //TODO 获取当前行的行索引
  35. let index = $( this).parents( ".th").index()- 1;
  36. //获取当前的checkbox中设置的隐藏域(包含了商品ID)
  37. let gid=$( ".th").eq(index).find( 'div:eq(0) input[type=hidden]').val();
  38. update(num,gid);
  39. }
  40. });
  41. //计算总价
  42. function jisuan (){
  43. var all= 0;
  44. var len =$( ".th input[type='checkbox']:checked").length;
  45. if(len== 0){
  46. $( "#all").text( '¥'+parseFloat( 0).toFixed( 2));
  47. } else{
  48. $( ".th input[type='checkbox']:checked").each(function(){
  49. //获取小计里的数值
  50. var sAll = $( this).parents( ".pro").siblings( '.sAll').text().substring( 1);
  51. //累加
  52. all+=parseFloat(sAll);
  53. //赋值
  54. $( "#all").text( '¥'+all.toFixed( 2));
  55. })
  56. }
  57. }
  58. //计算总共几件商品
  59. function zg (){
  60. var zsl = 0;
  61. var index = $( ".th input[type='checkbox']:checked").parents( ".th").find( ".num span");
  62. var len =index.length;
  63. if(len== 0){
  64. $( "#sl").text( 0);
  65. } else{
  66. index.each(function(){
  67. zsl+=parseInt($( this).text());
  68. $( "#sl").text(zsl);
  69. })
  70. }
  71. if($( "#sl").text()> 0){
  72. $( ".count").css( "background", "#c10000");
  73. } else{
  74. $( ".count").css( "background", "#8e8e8e");
  75. }
  76. }
  77. /*****************商品全选***********************/
  78. $( "input[type='checkbox']").on( 'click',function(){
  79. var sf = $( this).is( ":checked");
  80. var sc= $( this).hasClass( "checkAll");
  81. if(sf){
  82. if(sc){
  83. $( "input[type='checkbox']").each(function(){
  84. this.checked= true;
  85. });
  86. zg();
  87. jisuan();
  88. } else{
  89. $( this).checked= true;
  90. var len = $( "input[type='checkbox']:checked").length;
  91. var len1 = $( "input").length- 1;
  92. if(len==len1){
  93. $( "input[type='checkbox']").each(function(){
  94. this.checked= true;
  95. });
  96. }
  97. zg();
  98. jisuan();
  99. }
  100. } else{
  101. if(sc){
  102. $( "input[type='checkbox']").each(function(){
  103. this.checked= false;
  104. });
  105. zg();
  106. jisuan();
  107. } else{
  108. $( this).checked= false;
  109. var len = $( ".th input[type='checkbox']:checked").length;
  110. var len1 = $( "input").length- 1;
  111. if(len<len1){
  112. $( '.checkAll').attr( "checked", false);
  113. }
  114. zg();
  115. jisuan();
  116. }
  117. }
  118. });
  119. /****************************proDetail 加入购物车*******************************/
  120. $( ".btns .cart").click(function(){
  121. if($( ".categ p").hasClass( "on")){
  122. var num = parseInt($( ".num span").text());
  123. var num1 = parseInt($( ".goCart span").text());
  124. $( ".goCart span").text(num+num1);
  125. }
  126. });
  127. //删除购物车商品
  128. $( '.del').click(function(){
  129. //定义商品id 比如:1,2,3,4,5
  130. let gids = "";
  131. //单个删除
  132. if($( this).parent().parent().hasClass( "th")){
  133. $( ".mask").show();
  134. $( ".tipDel").show();
  135. index = $( this).parents( ".th").index()- 1;
  136. //TODO 获取当前的checkbox中设置的隐藏域(包含了商品ID)
  137. gids=$( ".th").eq(index).find( 'div:eq(0) input[type=hidden]').val();
  138. console.log(gids);
  139. $( '.cer').click(function(){
  140. $( ".mask").hide();
  141. $( ".tipDel").hide();
  142. $( ".th").eq(index).remove();
  143. $( '.cer').off( 'click');
  144. if($( ".th").length== 0){
  145. $( ".table .goOn").show();
  146. }
  147. del(gids);
  148. })
  149. } else{
  150. //选中多个一起删除
  151. if($( ".th input[type='checkbox']:checked").length== 0){
  152. $( ".mask").show();
  153. $( ".pleaseC").show();
  154. }
  155. else{
  156. $( ".mask").show();
  157. $( ".tipDel").show();
  158. //TODO 先获取所有即将被删除的商品ID
  159. let gidarr = new Array();
  160. $( ".th input[type='checkbox']:checked").each(function(j){
  161. gidarr.push($( ".th").eq(index).find( 'div:eq(0) input[type=hidden]').val());
  162. // gids += $(".th").eq(index).find('div:eq(0) input[type=hidden]').val(); 很容易报数据下标越界异常
  163. });
  164. gids = gidarr.join( ",");
  165. $( '.cer').click(function(){
  166. $( ".th input[type='checkbox']:checked").each(function(j){
  167. index = $( this).parents( '.th').index()- 1;
  168. $( ".th").eq(index).remove();
  169. if($( ".th").length== 0){
  170. $( ".table .goOn").show();
  171. }
  172. })
  173. $( ".mask").hide();
  174. $( ".tipDel").hide();
  175. zg();
  176. jisuan();
  177. del(gids);
  178. })
  179. }
  180. }
  181. })
  182. $( '.cancel').click(function(){
  183. $( ".mask").hide();
  184. $( ".tipDel").hide();
  185. })
  186. //改变商品规格
  187. // $(".pro dd").hover(function(){
  188. // var html='';
  189. // html='<span class="edit">修改</span>';
  190. // $(this).addClass("on").append(html).parents(".th").siblings(".th").find(".pro dd").removeClass("on").find('.edit').remove();
  191. // $(".edit").each(function(i){
  192. // $(this).attr("id",'edit'+i);
  193. // $("#edit"+i).click(function(){
  194. // $(".proDets").show();
  195. // $(".mask").show();
  196. // $(".changeBtn .buy").attr("data-id",i);
  197. // })
  198. // })
  199. // },function(){
  200. // $(this).removeClass("on");
  201. // })
  202. // $(".changeBtn .buy").click(function(){
  203. // var index = $(this).attr("data-id");
  204. // var result = $(".smallImg .on").find("img").attr("alt");
  205. // $("#edit"+index).prev().text(result);
  206. // $(".proDets").hide();
  207. // $(".mask").hide();
  208. // $("#edit"+index).parent("dd").removeClass("on").find(".edit").remove();
  209. // });
  210. // $(".changeBtn .cart").click(function(){
  211. // $(".proDets").hide();
  212. // $(".mask").hide();
  213. // })
  214. })
  215. //删除商品
  216. function del (gids) {
  217. $.post( '/shopCar/delete',{
  218. 'gids':gids
  219. },function(rs){
  220. if(rs.code!= 200)
  221. alert(rs.msg);
  222. else
  223. location.href= '/shopCar/queryShopCar';
  224. }, 'json');
  225. }
  226. //修改商品数量
  227. function update (num,gid){
  228. $.post( '/shopCar/update',{
  229. 'gid':gid,
  230. 'quantity':num
  231. },function(rs){
  232. if(rs.code!= 200)
  233. alert(rs.msg);
  234. else
  235. location.href= '/shopCar/queryShopCar';
  236. }, 'json');
  237. }


转载:https://blog.csdn.net/weixin_67450855/article/details/127765226
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场