首页 > 代码库 > 修改购物项图书数量的Ajax处理

修改购物项图书数量的Ajax处理

一、cart.jsp页面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<base href=http://www.mamicode.com/"http://${pageContext.request.serverName }:${pageContext.request.serverPort }${pageContext.request.contextPath }/" />>
二、修改CartServlet

package com.atguigu.bookstore.servlet.client;

import com.atguigu.bookstore.bean.Book;
import com.atguigu.bookstore.fun.Cart;
import com.atguigu.bookstore.service.impl.BookServiceImpl;
import com.atguigu.bookstore.service.impl.CartServiceImpl;
import com.atguigu.bookstore.service.inter.BookService;
import com.atguigu.bookstore.service.inter.CartService;
import com.atguigu.bookstore.servlet.BaseServlet;
import com.atguigu.bookstore.utils.WebUtils;
import com.google.gson.Gson;

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

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

public class CartServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	CartService cartService=new CartServiceImpl();
	BookService bookService=new BookServiceImpl();
	
	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("add....");
		String bookid = request.getParameter("bookid");
		Book book = bookService.getBookById(bookid);
		Cart cart = WebUtils.getCart(request);
		cartService.add(book, cart);

		//要得到这样的数据{"bookName":"java", "totalCount": 2}	怎么得?	
		String bookName = book.getBookName();
		int totalCount = cart.getTotalCount();
		
		
		//使用Gson可以得到以上类型的数据,但是需要一个源,这个只有map和
		//src是一个一般对象或map对象,在这里只能用map对象,因为如果是一般对象,所需要一个类中包含不了这2种属性,还得重新定义类,挺麻烦的	
		//而src是map对象时就不需要在重新定义类了,
		
		Map<String, Object>map=new HashMap<String, Object>();	
		map.put("bookName", bookName); //取数据的时候是data.bookName;
		map.put("totalCount", totalCount);	
		
		String jsonStr=new Gson().toJson(map);
		
		response.setContentType("text/json;charset=utf-8");
		response.getWriter().write(jsonStr);
			
		
	}
	
	protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("delete...");
		String bookid = request.getParameter("bookid");
		Cart cart = WebUtils.getCart(request);
		cartService.deleteItem(Integer.parseInt(bookid), cart);
		WebUtils.myForward(request, response, "/client/book/cart.jsp");
		
	}
	protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("update....");
		String count = request.getParameter("count");
		String bookId = request.getParameter("bookId");
		
		Map<String, Object>map=cartService.updateCount(Integer.parseInt(bookId),
				Integer.parseInt(count), WebUtils.getCart(request));
		
//		WebUtils.myForward(request, response, "/client/book/cart.jsp"); //不用这种方式了
		
	//要返回的数据类型:{count:2,itemPrice:20,totalCount:5,totalPrice:50},那么如何得到呢?可以更改updateCount方法,使其返回一个map集合
		
		
		String json = new Gson().toJson(map);		
		response.setContentType("text/json;charset=utf-8");
		response.getWriter().write(json);
		
		
		
		
		
	}
	
	protected void clear(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {	
		System.out.println("clear....");
		cartService.clear(WebUtils.getCart(request));
		WebUtils.myForward(request, response, "/client/book/cart.jsp");
	
	}

}

三、修改CartServiceImpl

<span style="white-space:pre">	</span>@Override
	public Map<String, Object> updateCount(int BookId, int count, Cart cart) {
		Map<String, Object>map=new HashMap<String, Object>();
		CartItem cartItem = cart.getMap().get(BookId);
		cartItem.setCount(count);
		//{count:2,itemPrice:20,totalCount:5,totalPrice:50},
		map.put("count", count);
		map.put("itemPrice", cartItem.getItemPrice());
		map.put("totalCount", cart.getTotalCount());
		map.put("totalPrice", cart.getTotalPrice());

		return map;
	}


修改购物项图书数量的Ajax处理