Ajax Modify Shopping Cart sample _ajax related

Source: Internet
Author: User

1. Design of Shopping cart class

Shoppingcartitem: The package of books, including title, quantity, price three attributes, and corresponding getter and setter methods.

ShoppingCart: Shopping Cart package, items for map<string, shoppingcartitem>, and add shopping cart, get the total number of books in the shopping cart and the total price of three functions.

2:jsp Join the shopping cart, bring in the title and price of the hyperlink

<body> 
<!--The purpose of joining spans is to locate--> 
<div id= "Cartstatus" > 
you have <span id= 
"BookName" ></span> added to the shopping cart, the shopping cart has 
<span id= "Totalbooknumber" ></span> book, the total Price is 
<span id= " Totalmoney "></span> 
</div> 
<br> 
<br> 
java 
<a 
href=" ${ pagecontext.request.contextpath}/addtocart?id=java&price=100 "> Add Shopping Cart </a> 
<br> 
Ajax 
<a 
href= "${pagecontext.request.contextpath}/addtocart?id=ajax&price=200" > Add Shopping Cart </a> 
<br> 
jquery 
<a 
href= "${pagecontext.request.contextpath}/addtocart?id=jquery& price=300 "> Add Shopping Cart </a> 
<br> 
</body>
<!--${pagecontext.request.contextpath Gets the absolute path to the project-->

Design of 3:addtocart-----servlet

The steps are as follows:

1: Get request parameter ID (bookname), price, is obtained from a hyperlink in the JSP page

2: Get the shopping Cart object in session, if there is no shopping cart in the session property, create a new Cart object to place in the session property

3): Add Shopping cart Operation Shopping.addtocart (BookName, Price);

4): Ajax to pass the JSON object, which includes: {"" BookName "": "Totalbooknumber", "Totalmoney"}, if the JSON object returned from the server side, the property name must use double quotes!!

5: In response to JSON request, Response.getwriter (). print (JSON);

public class Addtocartservlet extends HttpServlet {public void doget (HttpServletRequest request, HttpServletResponse R 
Esponse) throws Servletexception, IOException {this.dopost (request, response); 
public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { 
1: Get request parameter ID (bookname), Price String bookname =request.getparameter ("id"); 
int Price =integer.parseint (request.getparameter ("price")); 
2: Get the Shopping Cart object, in session ShoppingCart sc= (ShoppingCart) request.getsession (). getattribute ("SC"); 
if (sc==null) {sc=new shoppingcart (); 
Request.getsession (). setattribute ("SC", SC); 
//3 the clicked object into the shopping cart Sc.addtocart (bookname, Price); 
4: JSON object for response: {"BookName": "Totalbooknumber", "Totalmoney"}//If the JSON object is returned from the server side, the property name must use double quotes!! 
StringBuilder sbuilder=new StringBuilder (); Sbuilder.append ("{"). Append ("\" bookname\ ": \" "+bookname+" ""). Append (","). Append ("\" Totalbooknumber\ ": \" "+ Sc.gettotalbooknumber () + "\"). Append (","). APPend ("\" totalmoney\ ": \" "+sc.gettotalmoney () +" \ "). Append ("} "); 
Response to JSON request Response.setcontenttype ("Text/javascript"); 
Response.getwriter (). Print (sbuilder.tostring ()); 
The method used to StringBuilder the JSON string in the above can be simplified by third-party Open source Jackson: string jsonstr=null; 
Objectmapper objectmapper=new objectmapper (); Jsonstr=objectmapper.writevalueasstring (SC);

4:ajax accept the parameters from the server {"BookName": "Totalbooknumber", "Totalmoney"}

Steps:

1): Add the click response function for the hyperlink to the shopping cart and cancel the default behavior (return false)

2: Load JSON data via HTTP GET request. $.getjson (URL, [data], [callback])

Prepare the Url.agrs and display the contents of the cart inside the JSP page within the callback function.

3: Through the Hide in jquery (), show () method to determine whether the first use of the shopping cart, if the first time use, the JSP page does not display the shopping cart.

  
 
Related Article

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.