商品的收藏和取消收藏(MVC)


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 mapToCollectModel(List> data){
		List list = new ArrayList<>();
		CollectCountModel collectionCountModel = null;
		for (Map map : data) {
			collectionCountModel = new CollectCountModel(
					Integer.parseInt(map.getOrDefault("counts", "").toString()));
			list.add(collectionCountModel);
		}
		return list;
	}


3.在进入商品详情的servlet中获取到当前的商品id,进入Model层通过Service查询,在jsp页面中使用标签显示【收藏】和【取消收藏】

product.jsp

   <%
    	CollectCountModel col = (CollectCountModel)request.getAttribute("isCollectionInfo");
    %>
......

  收藏  



  取消收藏  


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







你可能感兴趣的:(web开发,mvc,servlet,jsp,商品,收藏)