首页 > 代码库 > 修改购物项图书数量的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 }/" />>
二、修改CartServletpackage 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处理
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。