The application of MVC design pattern in the website of the only product

Source: Internet
Author: User

The application of MVC design pattern in the website of the only product

In the past 4.19, I believe that a lot of people have participated in this activity as well as I do. In today's society, online shopping has become an indispensable part of people's lives, today let us look at the MVC design pattern in the website of the only products.

When we usually shop at the website, click on the product page to add the shopping cart icon, and then the product will be added directly to the shopping cart list, regardless of whether the user is logged in or not. Then we changed a device and found that the product we previously added to a shopping cart on another device does not exist. It can be inferred that the contents of the shopping cart list are not stored in the database but in the cookie. So the shopping cart feature implementation is implemented on the client. The shopping cart features: Show cart list, add items to Cart list, modify the number of items in cart list, delete items from list.

In the Show Cart List feature, because the list of items in the shopping cart is stored in a cookie, obtaining a shopping cart list is a list of items from the cookie. First, the form of the cookie is also the form of key-value, key refers to the name of the save, such as Tt_cart, is a sign. Value is the specific information of the saved commodity, which is saved as a string, and is usually converted to JSON-formatted data, because JSON is stored in the form of a string. A cookie is a session tracking technology that writes a cookie that is written by the server's response, which is the server that issues a noun cookie to the client. So it takes a request to get a cookie.

Service Layer Implementation:

Get a list of shopping carts from a cookie

Private List<cartitem> Getitemlistbycookie (httpservletrequest request)

{

String Cookiejson = cookieutils. Getcookievalue (Request, "Tt_cart",true);

Convert to Product List

if (cookiejson==null)

{

return New arrraylist<> ();

}

The value that is stored in the cookie is also Key-value, and value is a string, which is the JSON -formatted data

List<cartitem>list=jsonutils. Jsontolist (Cookiejson,cartitem. Class);

return list;

}

After obtaining a list of items, the controller layer implementation shows the list:

Show Shopping Cart List

@requestmapping("/cart")

Public String Showcart (httpservletrequest request,model model)

{

List<cartitem>showcartlist=carservice. showcartlist (Request);

Model. AddAttribute ("Cartlist", showcartlist);

return "Cart";

}

When adding a product to a shopping cart, first determine if the item exists in the shopping cart list, add 1 if it exists, add the item to the cart if it does not exist, and re-write the list of items to the cookie. The DAO layer is not involved in this function module. Service layer: Because the shopping cart list has been shown before, it is necessary to traverse the shopping cart list to determine whether the ID of the item in the shopping cart is the same as the ID of the item being added, if the number is the same, and if it is inconsistent, the information from the Commodity information table will need to be obtained. It is necessary to invoke the interface of the service layer to obtain the commodity information based on the ID, i.e. with httpclient.

In the website, the product is added to the shopping cart, display the shopping cart list of items, click on the settlement, then to the pre-submission list of orders, click Submit Order, generate this order, return order number, payment amount, the estimated time of arrival of the order. This is a very important function for the Web site, both mobile and PC-side need this function, so here the order system as a separate service, set aside the interface for the customer to call. The controller needs to pass the object to the service layer, and the parameter that the controller receives is a JSON-formatted string, which means that the client passes in a JSON-formatted string. This involves how the SPRINGMVC receives the JSON string and needs to use the @requestbody annotation. The principle of @ResponseBody annotations is that response can only respond to a string, and when our return value is a Java object, it has a default behavior of using the Jackson package to convert a Java object to a string response. This is a default automatic behavior that does not require human settings, as long as this annotation is available. @RequestBody annotations in the same vein: Use this annotation to tell Springmvc that it is now receiving a JSON string that needs to take the default behavior to convert the JSON string to a Java object using the Jackson package. So the controller layer needs the Pojo of a Java object.

The application of MVC design pattern in the website of the only product

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.