تستمر هذه المقالة في مشاركتك في وحدة عربة التسوق في مكتبة Javaweb للرجوع إليها. المحتوى المحدد كما يلي
تخزين عربة التسوق
حفظ في الجلسة حفظ في ملفات تعريف الارتباط حفظ في قاعدة البيانات
1. إنشاء فصول ذات صلة
هيكل عربة التسوق:
كارتيم: إدخال عربة التسوق ، يحتوي على الكتب والكمية
عربة: عربة التسوق ، بما في ذلك الخريطة
/*** CART CLASS*/CLASS CLASS Public {MAP الخاص <String ، Cartitem> MAP = NEW LinkedHashMap <String ، Cartitem> () ؛ / *** احسب المجموع* regurn*/ public double getTotal () {// total = مجموع المجال الفرعي لجميع الإدخالات totalcimal total = new BigDecimal ("0") ؛ لـ (Cartitem Cartitem: map.values ()) {bigDecimal subtotal = new BigDecimal ("" + cartitem.getSubtotal ()) ؛ المجموع = total.add (المجموع الفرعي) ؛ } إرجاع total.doublevalue () ؛ }/*** أضف إدخالًا إلى السيارة* param cartitem*/public void add (Cartitem Cartitem) {if (map.containskey (cartitem.getbook (). getBid ())) {// ugge armain armists in the caritem _cartitem = map.get (getitem.getbook () entry_cartitem.setCount (_cartitem.getCount () + cartitem.getCount ()) ؛ // قم بتعيين عدد الإدخالات القديمة على رقمه الخاص + عدد الإدخالات الجديدة map.put (cartitem.getbook (). getBid () ، _cartitem) ؛ } آخر {map.put (cartitem.getBook (). getBid () ، cartitem) ؛ }} / *** مسح جميع الإدخالات* / public void clear () {map.clear () ؛ } / *** حذف الإدخالات المحددة* param bid* / public void delete (سلسلة عرض) {map.remove (bid) ؛ } / *** احصل على جميع الإدخالات* regurn* / public collection <Cartitem> getCartItems () {return map.values () ؛ }} /*** فئة إدخال عربة التسوق**/الفئة العامة Cartitem {book book private ؛ // product private int count ؛ // Quantity/*** method Subtotal* @Return* معالجة مشكلة خطأ في التشغيل الثنائية*/Public Double GetUBTOTAL () {// method subtotal ، ولكن لا يوجد لديه عضو مقابل! BigDecimal D1 = جديد BigDecimal (book.getPrice () + "") ؛ BigDecimal D2 = new BigDecimal (count + "") ؛ return d1.multiply (d2) .doublevalue () ؛ } getBook Book Public () {return Book ؛ } public void setbook (book book) {this.book = book ؛ } public int getCount () {return count ؛ } public void setCount (int count) {this.count = count ؛ }}2. أضف إدخال عربة التسوق
<٪@ page language = "java" import = "java.util. uri = "http://java.sun.com/jsp/jstl/fmt Transitional // en "> <html> <head> <title> قائمة عربة التسوق </title> <meta http-equiv =" pragma "content =" no-cache "> <meta http-equiv =" cache-control "content =" no-cache "> <meta http-equiv =" expiers content = "keyword1 ، keyword2 ، keyword3"> <meta http-equiv = "description" content = "this is my page"> <meta http-equiv = "content-type" content = "text/html ؛ charset = utf-8"> <! type = "text/css"> * {font-size: 11pt ؛ } div {margin: 20px ؛ الحدود: رمادي 2 بكسل. العرض: 150 بكسل ؛ الارتفاع: 150 بكسل ؛ محاذاة النص: المركز ؛ } li {margin: 10px ؛ } #buy {background: url (<c: url value = '/images/all.png'/>) no-repeat ؛ العرض: كتلة مضمنة. خلفية الموضع: 0 -902px ؛ الهامش اليساري: 30 بكسل ؛ الارتفاع: 36 بكسل ؛ العرض: 146 بكسل ؛ } #buy: hover {background: url (<c: url value = '/images/all.png'/>) no-repeat ؛ العرض: كتلة مضمنة. خلفية الموضع: 0 -938px ؛ الهامش اليساري: 30 بكسل ؛ الارتفاع: 36 بكسل ؛ العرض: 146 بكسل ؛ } </style> </head> <body> <h1> عربة التسوق </h1> <c: اختر> <٪-إذا لم تكن هناك سيارة ، أو مجموعة المحتوى للسيارة 0-length-٪> <c: in test = "$ {{$ sessionscope.cart أو fn: length (sessionscope.cart.carteems) eq 0} value = '/images/cart.png' /// "/> </c: عندما> <c: خلاف ذلك> <table cellpacing =" 0 "background =" black " Cart </a> </td> </tr> <tr> <th> الصورة </th> عنوان الكتاب </th> <th> المؤلف </th> <th> سعر الوحدة </th> <th> الكمية </th> <th> subtotal </h> <th> العملية </th> </tr> <c: for easion yead = "sationscope var = "cartitem"> <tr> <td> <viv> <img src = "<c: url value = '/$ {cartitem.book.image}' //"/> </fiv> </td> <td> $ {cartitem.book.bname} </td> <td> <td> $ {cartitem.book.price} yuan </td> <td> $ {cartitem.count} </td> <td> $ {cartitem.subtotal} yuan </td> <td> <a href = "c: url value = cartservlet؟ } '/> "> حذف </a> </td> </td> </td> </tr> </c: foreach> <ttr> <td colspan =" 7 "align =" right "style =" font-size: 15pt ؛ Font-Weight: 900 "> المجموع: $ {sessionscope.cart.total} Yuan </td> </tr> <tr> <td colspan =" 7 "align =" right "style =" font-size: 15pt ؛ Font-Weight: 900 "> <a id =" buy "href =" <c: url value = '/orderservlet؟ method = add'/> " يمتد CartServlet من الفئة العامة Baseservlet { /** * إضافة إدخال التسوق * request request * param استجابة * regurn * throws servletexception * throws ioException * /سلسلة عامة add (httpservlequest request ، get the comple. الإدخال إلى السيارة*//** 1. احصل على CAR*/ CART CART = (CART) request.getSession (). getAttribute ("cart") ؛ /** يمرر النموذج فقط العرض والكمية* 2. كتاب كتاب = New Bookservice (). load (BID) ؛ int count = integer.parseint (request.getParameter ("count")) ؛ Cartitem Cartitem = new Cartitem () ؛ cartitem.setbook (كتاب) ؛ Cartitem.setCount (count) ؛ /** 3. أضف الإدخال إلى السيارة*/ cart.add (Cartitem) ؛ إرجاع "f: /jsps/cart/list.jsp" ؛ } / **. (cart) request.getSession (). getAttribute ("cart") ؛ cart.clear () ؛ إرجاع "f: /jsps/cart/list.jsp" ؛ } / ** * حذف إدخالات التسوق * @param طلب * param استجابة * return * throws servleTexception * throws ioException * / سلسلة عامة حذف (httpservletrequest طلب ، httpservletpons (cart) request.getSession (). getAttribute ("cart") ؛ BID string = request.getParameter ("BID") ؛ CART.DELETE (BID) ؛ إرجاع "f: /jsps/cart/list.jsp" ؛ }}3. مسح الإدخال
4. احذف مدخل عربة التسوق
5. عربة التسوق الخاصة بي
يوجد رابط في Top.jsp: عربة التسوق الخاصة بي
تصل العربة الخاصة بي مباشرة/jsps/cart/list.jsp وتعرض جميع إدخالات العربة في الجلسة.
ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.