この記事では、参照のためにJavaweb Bookstoreのショッピングカートモジュールを引き続き共有しています。特定のコンテンツは次のとおりです
ショッピングカート収納
セッションで保存して、データベースに保存します
1.関連するクラスを作成します
ショッピングカート構造:
Cartitem:本と数量を含むショッピングカートの入り口
カート:地図を含むショッピングカート
/***ショッピングカートクラス*/パブリッククラスカート{プライベートマップ<String、cartitem> map = new linkedhashmap <string、cartitem>(); / ***合計を計算* @return*/ public double gettotal(){//すべてのエントリのサブトータルの合計bigdecimal total = new bigdecimal( "0"); for(cartitem cartitem:map.values()){bigdecimal subtotal = new bigdecimal( "" + cartitem.getsubtotal()); Total = Total.Add(subtotal); } total.doublevalue()を返します。 }/***車へのエントリを追加* @param cartitem*/public void add(cartitem cartitem){if(map.containskey(cartitem.getbook()。getbid()){//元のcartitem _cartitem _cartitem = map.get.getbook()。 entry_cartitem.setcount(_cartitem.getCount() + cartitem.getCount()); //独自の番号に古いエントリの数を設定し、新しいエントリの数を設定します。 } else {map.put(cartitem.getbook()。getBid()、cartitem); }} / ***すべてのエントリをクリア* / public void clear(){map.clear(); } / ***指定されたエントリを削除* @param bid* / public void delete(string bid){map.remove(bid); } / ***すべてのエントリを取得* @return* / public collection <cartitem> getCartitems(){return map.values(); }} /***ショッピングカートエントリクラス**/public class cartitem {private book book; // product private int count; //数量/*** subtotalメソッド* @return*処理されたバイナリ操作エラー問題*/public double getSubtotal(){// subtotalメソッドですが、対応するメンバーはありません! BigDecimal D1 = new BigDecimal(book.getPrice() + ""); BigDecimal D2 = new BigDecimal(count + ""); d1.multiply(d2).doublevalue(); } public Book getBook(){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。*" PageEncoding = "UTF-8"%> <%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core"%> <%@ haglib prefix = "fmt" fmt " uri = "http://java.sun.com/jsp/jstl/fmt"%> <%@ taglib prefix = "fn" uri = "http://java.sun.com/jsp/jstl/functions"%> <!doctype html public - // w3c // dtd html 4.01 Transitional // en "> <html> <head> <title>ショッピングカートリスト</title> <meta http-equiv =" pragma "content =" no-cache "> <meta http-equiv =" cache-control "content =" no-cache "> <meta http-equiv =" equiv = " content = "keyword1、keyword2、keyword3"> <meta http-equiv = "descripting" content = "これは私のページ"> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"> <! - <link rel = "styletheet" Type = "href =" styles. type = "text/css"> * {font-size:11pt; } div {マージン:20px;ボーダー:固体2pxグレー;幅:150px;高さ:150px;テキストアライグ:センター; } li {マージン:10px; } #buy {background:url(<c:url value = '/images/all.png'/>)no-repeat;ディスプレイ:インラインブロック。バックグラウンドポジション:0 -902px;マージン左:30px;高さ:36px;幅:146px; } #buy:hover {background:url(<c:url value = '/images/all.png'/>)no-repeat;ディスプレイ:インラインブロック。バックグラウンドポジション:0 -938px;マージン左:30px;高さ:36px;幅:146px; } </style> </head> <body> <h1>ショッピングカート</h1> <c:> <%を選択します - 車がない場合、または車のコンテンツコレクションが0長い場合 - %> <c: "$" $ {empty sessionscope.cartまたはfn:length(sessionscope.cartiTems) value = '/cart.png' /// "/> </c:when> <c:それ以外の場合は> <テーブルセルペーシング=" 0 "black"> <tr> <td colspan = "7" align = "right" style = "font-weight:900"> cart </a> </td> </tr> <tr> <th>写真</th> <th>本のタイトル</th> <th>著者</th> <th> unit </th> <th> <th> <thotal </th> <th> operation </th> </tr> <c: <tr> <td> <div> <img src = "<c:url value = '/$ {cartitem.book.image}' //" // "/> </div> </td> <td> $ {cartitem.book.book.bname} </td> <td> $ {cartitem.book.author} </td>>>>>>>>>>>> Yuan </td> <td> $ {cartitem.count} </td> <td> $ {cartitem.subtotal} yuan </td> <td> <a href = "<c:url value = '/cartservlet?method = delete&bid = $ {cartitem.book.book.book.book.book.book.book.book.book.book.book.book.book.book.book.book.book.book.book.book.book.book.book.book.book.book.book.book.book.book.book.book.book.book.book.bool </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:> <a id = "buy" href = "<c:url value = '/orderservlet?method = add'/>"> </a> </td> </tr> </table> </c:それ以外のパブリッククラスのcartservletはbaseServletを拡張します{ /** *ショッピングエントリ * @param request * @param Response * @return * @throws servletexception * @throws ioexception * /public string add(httpservletresponse respone、httpservletresponse応答)車への入り*//**1。車を取得*/カートカート=(カート)request.getSession()。getAttribute( "cart"); /**フォームは入札と数量のみを通過します* 2。エントリを取得*>本と数量を取得*>最初に本の入札を取得します。次に、ブックを取得するために入札を介してデータベースを照会する必要があります*> where*/ string bid = request.getParameter( "bid"); book book = new Bookservice()。load(bid); int count = integer.parseint(request.getParameter( "count")); cartitem cartitem = new Cartitem(); cartitem.setbook(book); cartitem.setcount(count); /**3。CARにエントリーを追加*/ cart.add(cartitem); "f:/jsps/cart/list.jsp"を返します。 } / ** *ショッピングエントリをクリア * @param request * @param Response * @return * @throws servletexception * @throws ioexception * / public string clear(httpservletrequest request、httpservletrequest Reponse)スローServletexception、ioexception { / ** * 1。カートを設定します。 (cart)request.getSession()。getAttribute( "cart"); CART.CLEAR(); "f:/jsps/cart/list.jsp"を返します。 } / ** *ショッピングエントリの削除 * @param request * @param Response * @return * @throws servletexception * @throws ioexception * / public string delete(httpservletrequest request、httpservletresponse応答)スローServletexception、ioexception { / * * 1。 (cart)request.getSession()。getAttribute( "cart"); string bid = request.getParameter( "bid"); cart.delete(bid); "f:/jsps/cart/list.jsp"を返します。 }}3.エントリをクリアします
4.ショッピングカートのエントリを削除します
5。私のショッピングカート
top.jspにリンクがあります:私のショッピングカート
私のカートは直接/jsps/cart/list.jspにアクセスし、セッション中のカートのすべてのエントリを表示します。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。