业务逻辑图,简单版要写各个Servlet


//ChangeCartCountServlet 使用ajax实现数量,增加或减少;

package com.cart.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cart.dao.BookDao;
import com.cart.dao.impl.BookDaoImpl;
import com.cart.entity.Book;
import com.cart.entity.CartItem;
import com.cart.entity.Userinfo;
import com.cart.service.CartService;

public class ChangeCartCountServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		resp.setContentType("text/html;charset=utf-8");
		PrintWriter out = resp.getWriter();
		
		int bookid = Integer.parseInt(req.getParameter("bookid"));
		int count = Integer.parseInt(req.getParameter("count"));
		CartService cartService=new CartService();
		Userinfo userinfo=(Userinfo)req.getSession().getAttribute("userinfo");
		int uid=userinfo.getId();//获得当前用户id编号;
		
		CartItem cartItem=cartService.findCartItemById(uid,bookid);
		if(cartItem.getCount()<count){//如果根据总数量小于你增加的总数量,则不能购物,业务正确;
			out.print("false");
			return;
		}
		
		Map<Integer,CartItem> cart = (Map<Integer,CartItem>)req.getSession().getAttribute("cart");
		int result=-1;
		if(cart!=null){
			cartItem = cart.get(bookid); //获得购物车项		
			cartItem.setCount(count);
			cart.put(bookid, cartItem);
			
			result=cartService.updateCartItemCount(cartItem);
		}
		if(result>0){
			out.print("true");
		}else{
			out.print("false");
		}
		
		out.flush();
		out.close();
	}
}

package com.cart.web;


import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.cart.entity.CartItem;
import com.cart.entity.Userinfo;
import com.cart.service.CartService;

public class ShowBookServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession session=req.getSession();
		Userinfo user=(Userinfo)session.getAttribute("userinfo");
		int uid=user.getId();
		CartService cartService=new CartService();
		Map<Integer,CartItem> cart = cartService.getCartItem(uid);
		
		session.setAttribute("cart", cart); //加入session用jstl读取;
		resp.sendRedirect("show_cart.jsp");
		
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req, resp);
	}
}

package com.cart.web;


import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.cart.entity.Book;
import com.cart.entity.CartItem;
import com.cart.entity.Userinfo;
import com.cart.service.CartService;

public class DeleteCartServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession session=req.getSession();
		Userinfo user=(Userinfo)session.getAttribute("userinfo");
		int uid=user.getId();
		int bookid = Integer.parseInt(req.getParameter("bookid"));
				
				Map<Integer,CartItem> cart = (Map<Integer,CartItem>)req.getSession().getAttribute("cart");
				// 根据key(bookid)删除
				cart.remove(bookid);
				//需要根据carService进行操作;
				CartService cartService=new CartService();
				cartService.deletecartItem(uid, bookid);
				
				req.getSession().setAttribute("cart", cart);
				
				resp.sendRedirect("show_cart.jsp");
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req, resp);
	}
}


posted on 2016-12-09 14:11  波波嘟  阅读(230)  评论(0编辑  收藏  举报