Javaweb Project--Online shop (6-3)

Source: Internet
Author: User

Day3

1. Solution to the coupling idea with the configuration file

<?xml version= "1.0" encoding= "UTF-8"?><beans> <bean id= "Productdao"class= "Com.itheima.dao.impl.ProductDaoImpl"/> <!--<bean id= "Productdao"class= "Com.itheima.dao.impl.ProductDaoHibImpl"/>--<bean id= "Userdao"class= "Com.itheima.dao.impl.UserDaoImpl"/> <bean id= "CategoryDao"class= "Com.itheima.dao.impl.CategoryDaoImpl"/> <bean id= "Orderdao"class= "Com.itheima.dao.impl.OrderDaoImpl"/> <bean id= "Productservice"class= "Com.itheima.service.impl.ProductServiceImpl"/> <bean id= "UserService"class= "Com.itheima.service.impl.UserServiceImpl"/> <bean id= "Categoryservice"class= "Com.itheima.service.impl.CategoryServiceImpl"/> <bean id= "OrderService"class= "Com.itheima.service.impl.OrderServiceImpl"/></beans>
Beans.xml
 Packagecom.itheima.utils;Importorg.dom4j.Document;Importorg.dom4j.Element;ImportOrg.dom4j.io.SAXReader;/*** Get JavaBean's factory *@authorAdministrator **/ Public classBeanfactory { Public StaticObject Getbean (String id) {Try {            //1. Get the Document ObjectDocument doc=NewSaxreader (). Read (beanfactory.class. getClassLoader (). getResourceAsStream ("Beans.xml")); //2. Call API selectSingleNode (expression)Element beanele= (Element) Doc.selectsinglenode ("//bean[@id = '" +id+ "']"); //3. Get the class attribute of an elementString Classvalue = Beanele.attributevalue ("Class"); //4. Returning an object that implements a class by reflectionObject newinstance =Class.forName (Classvalue). newinstance (); returnnewinstance; }  Catch(Exception e) {e.printstacktrace (); System.out.println ("Get Bean Failed"); }        return NULL; }         Public Static voidMain (string[] args)throwsException {System.out.println (Getbean ("ProductDao1")); }}
Beanfactory get JavaBean's factory

Case 1-Add to Cart
Demand:
On the Product Details page, enter the number of purchases, click Add to Cart, the item is added to the shopping cart
Technical Analysis:
Session
Entities involved:
Cart (CART)
Property:
Collection of shopping items (product purchase information)
Map<string pid,cartitem>
Gross amount (total)
Method:
Add to Shopping Cart
Add2cart (Cartitem Item) {
1. Determine if the shopping cart has the product
Yes:
Modify Purchase Quantity (original quantity +item.getcount)
Modify the total amount (Original Amount +item.getsubtotal ())
None: Put in directly to modify the total amount (Original Amount +item.getsubtotal ())
}
Remove from your shopping cart
Removefromcart (String pid) {
Remove a specified shopping item from the map
Modify Total Amount
}
Empty shopping Cart
Clearcart () {
Clear Map
Modify Total Amount =0.0
}
/////////////////////////////////////////////
/////////////////////////////////////////////
Shopping items (cartitem)
Property:
Commodity Object (product)
Purchase Quantity (count)
Subtotal (subtotal)
Method:
Re-write
Getsubtotal () {
Return Product.getshop_price*count
}
Step Analysis:
1. Modify the Product Details page (product_info.jsp)
Add a form:
Method to execute: Method=add2cart
Pid:pid=xxx of goods
Number of items purchased: count=123123
Click "Add to Cart" to submit the form/store/cart
2. Writing Cartservlet inheritance Baseservlet writing Add2cart
Get the product's PID and Count
Call Productservice to get the product via PID

Package Cartitem
New Cartitem (Product,count)
Add Cartitem to your shopping cart
Get shopping cart (get in session)
You can call the Add2cart method of the shopping cart

Redirect to Cart.jsp

Javaweb Project--Online shop (6-3)

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.