商品的收藏和取消收藏(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.java

protected 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.java

protected 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开始即可。







原创粉丝点击