商品的收藏和取消收藏(MVC)
来源:互联网 发布:php wsdl 调用 编辑:程序博客网 时间:2024/05/20 18:15
1.首先要说明的一点是在进行商品的收藏和取消收藏之前要先写好商品遍历的Servlet
2.将遍历的结果显示在商品浏览的jsp页面中3.点击任意一件商品后可进入商品详情页面
4.此时可进行商品的收藏和取消收藏
以下为商品收藏和取消收藏的简单demo
1.在进入商品详情页面之前首先要通过获取到的商品ID去数据库中查询该商品是否已经存在于当前用户的收藏夹中
DetailsProductServlet:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String p_id = request.getParameter("p_id");ProductModel productModel = new ProductModel(Integer.parseInt(p_id));CollectionModel collectionModel = new CollectionModel(Integer.parseInt(p_id));CollectionService collectionService = new CollectionService();request.setAttribute("isCollectionInfo",collectionService.isCollection(collectionModel));ProductService productService = new ProductService();request.setAttribute("ProductDetails", productService.queryOne(productModel));request.getRequestDispatcher("/front/JSP/product.jsp").forward(request, response);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}
2.Service用于判断是否已收藏
//用于判断是否已收藏public CollectCountModel isCollection(CollectionModel col){sql = "SELECT COUNT(*)AS counts FROM collection WHERE p_id = ?";data = db.query(sql, col.getP_id());return mapToCollectModel(data).get(0);}
//泛型转换public List<CollectCountModel> mapToCollectModel(List<Map<String,Object>> data){List<CollectCountModel> list = new ArrayList<>();CollectCountModel collectionCountModel = null;for (Map<String,Object> map : data) {collectionCountModel = new CollectCountModel(Integer.parseInt(map.getOrDefault("counts", "").toString()));list.add(collectionCountModel);}return list;}
3.在进入商品详情的servlet中获取到当前的商品id,进入Model层通过Service查询,在jsp页面中使用<c:if></c:if>标签显示【收藏】和【取消收藏】
product.jsp
<% CollectCountModel col = (CollectCountModel)request.getAttribute("isCollectionInfo"); %>
......
<c:if test="<%=col.getCounts() <= 0%>"><a href="<%=request.getContextPath() %>/AddCollectionServlet?p_id=<%=pro.getP_id()%>"> 收藏 </a></c:if><c:if test="<%=col.getCounts() > 0%>"><a href="<%=request.getContextPath() %>/DeleteCollectionServlet?p_id=<%=pro.getP_id()%>"> 取消收藏 </a></c:if>AddCollectionServlet.javaprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String p_id = request.getParameter("p_id");String u_id="5";CollectionModel collectionModel = new CollectionModel(Integer.parseInt(u_id),Integer.parseInt(p_id));CollectionService collectionService = new CollectionService();if(collectionService.addCollection(collectionModel)){System.out.println("添加成功");request.setAttribute("Info", p_id);response.sendRedirect("./DetailsProductServlet?p_id="+p_id);}}
DeleteCollectionServlet.javaprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String p_id = request.getParameter("p_id");CollectionModel collectionModel = new CollectionModel(Integer.parseInt(p_id));CollectionService collectionService = new CollectionService();if (collectionService.deleteCollection(collectionModel)) {request.setAttribute("DeleteCollectonInfo", "1");response.sendRedirect("./DetailsProductServlet?p_id="+p_id);}}测试的时候从遍历所有商品的Servlet开始即可。
阅读全文
0 0
- 商品的收藏和取消收藏(MVC)
- 商品详情页面实现点击收藏,取消收藏
- sqlite实现新闻收藏和取消收藏
- 收藏、点赞按钮 怎么由当前的状态(收藏/取消收藏)通过点击设置为取消收藏(收藏)
- Spring MVC的收藏
- 商品收藏模块的功能设计实现
- Android收藏商品/关注商品
- asp.net mvc 收藏的一些参考网址和优缺点
- 堆和堆栈的区别(收藏)
- TextView和EditText的总结 (收藏)
- 用户中心的收藏列表里显示商品缩略图
- Ecshop用户中心的收藏列表里显示商品缩略图
- html超级简单实现点赞(收藏)和取消赞效果
- ShareSDK 取消分享到微信收藏
- ShareSDK 取消分享到微信收藏
- MVC模式(收藏,以后了解)
- mvc网站收藏
- 收藏的blog和论坛
- VB6的一个奇技淫巧
- BigDecimal除法
- 使用java发送邮箱
- 条形码组件VintaSoftBarcode.NET SDK v10.0发布,增强条码识别、生成功能
- 最最最最基本算法(一)--
- 商品的收藏和取消收藏(MVC)
- 3、C程序 数组排序之[鸡尾酒排序]
- 不同数据类型的数据在列表中排序的问题
- 报错处理:could not be resolved as a collection/array/map/enumeration/iterator type
- 字典法
- Springboot入门第一篇之身世介绍
- Spring技术内幕 (二):Spring AOP的实现
- mat---Memory Monitor检测内存泄露
- Python之道(2)---Python装饰器