Ajax processing for modifying the number of shopping items books

Source: Internet
Author: User
Tags tojson

First, cart.jsp page

<%@ 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" >
Ii. Modification of 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 ();p rotected 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);//To get such data {"BookName": "Java", "TotalCount": 2} How? String bookname = Book.getbookname (), int totalcount = Cart.gettotalcount ();//Use Gson to get the above type of data, but need a source, this only map and// SRC is a generic object or Map object, which can only be used in the Map object, because if it is a generic object, the need for a class to contain these 2 attributes, but also to redefine the class, it is troublesome//and SRC is a Map object does not need to redefine the class, map<string, Object>map=new hashmap<string, object> () map.put ("BookName", bookname); The time to fetch data is 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"); Not in this way//to return the data type: {count:2,itemprice:20,totalcount:5,totalprice:50}, then how to get it? You can change the Updatecount method so that it returns a map collection of 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");}}

Third, modify the Cartserviceimpl

<span style= "White-space:pre" ></span> @Overridepublic 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 processing for modifying the number of shopping items books

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.