この記事では、参照のためにJavawebショッピングカートケースの特定のコードを共有しています。特定のコンテンツは次のとおりです
1。プロジェクトディレクトリ構造
2。ソースコード
パッケージ - DAOレイヤー:bookdao.java
パッケージcom.dao; import java.util.map; import com.db.db; import com.domain.book; public class bookdao {public map getall(){return db.getall(); } public Book find(string id){return(book)db.getall()。get(id); }}DBパッケージ: DB.Java-シミュレートされたデータベース
パッケージcom.db; import java.util.linkedhashmap; import java.util.map; import com.domain.book; import com.sun.org.apache.internal.generic.new; // database // absed database // static {map.put( "1"、new Book( "1"、 "Javaweb Development"、 "Lao Zhang"、38、 "A Good Book")); map.put( "2"、 "new Book(" 2 "、" JDBC Development "、" lao li "、18、" a good book ")); map.put(" 3 "、new Book(" 3 "、" ajax development "、" lao tong "、328、" a good book ")); map.put(" 4 "、" 4 " book ")); map.put(" 5 "、new Book(" 5 "、" Struts Development "、" Lao Fang "、28、" a good Book ")); map.put(" 6 "、new Book(" 6 "、" Spring Development "、" lao Fang "、98、" a good Book ");ドメインパッケージ:
book.java:本のエンティティクラス
パッケージcom.domain; //本のエンティティクラスパブリッククラスブック{private string id;プライベート文字列名;プライベートストリング著者;プライベートダブル価格。プライベート文字列の説明; public book(){super(); // TODO自動生成コンストラクタースタブ}パブリックブック(文字列ID、文字列名、文字列著者、二重価格、文字列説明){super(); this.id = id; this.name = name; this.author =著者; this.price = price; this.description = description; } public string getId(){return id; } public void setid(string id){this.id = id; } public string getname(){return name; } public void setName(string name){this.name = name; } public string getauthor(){return athor; } public void setauthor(string著者){this.author =著者; } public double getPrice(){return price; } public void setPrice(2倍の価格){this.price = price; } public string getDescription(){return description; } public void setDescription(string description){this.description = description; }}cart.java:ショッピングカートクラス
パッケージcom.domain; import java.util.linkedhashmap;インポートjava.util.map; //ユーザーを表すショッピングカートユーザーパブリッククラスカートを表すショッピングカートプライベートダブル価格。 //ショッピングカート内のすべてのアイテムがどれだけの費用がかかりますか? if(item == null){item = new Cartitem(); item.setbook(book); item.setQuantity(1); map.put(book.getid()、item); } else {item.setQuantity(item.getQuantity()+1); }} public Map <string、cartitem> getMap(){return Map; } public void setMap(map <string、cartitem> map){this.map = map; } public double getPrice(){Double TotalPrice = 0; for(map.entry <string、cartitem> entry:map.entryset()){cartitem item = entry.getValue(); TotalPrice += item.getPrice(); } this.price = totalprice;返品価格。 } public void setPrice(2倍の価格){this.price = price; }}cartitem.java:ショッピングアイテム
パッケージcom.domain; //特定の製品と表示される回数(ショッピングアイテム)を表すために使用されるパブリッククラスcartitem {private book book;プライベートインター数;プライベートダブル価格。パブリックブックgetBook(){return book; } public void setbook(book book){this.book = book; } public int getQuantity(){return Quantion; } public void setquantity(int Quantion){this.quantity = Quanty; this.price = this.book.getPrice() * this.quantity; } public double getPrice(){return price; } public void setPrice(2倍の価格){this.price = price; }}サービスパッケージ:サービスレイヤー
BusinessService.java:
パッケージcom.service; import java.util.map; Import com.dao.bookdao; Import com.domain.book; Import com.domain.cart; Import com.domain.cartitem; // business class、統一された方法ですべてのサービスをパブリッククラスのビジネスセルサービス{private bookdao = new bookdao(パブリックマップgetallbook(){return dao.getall(); } public Book Findbook(String ID){return dao.find(id); } //ショッピングカートでショッピングアイテムを削除しますpublic void deletecartitem(string id、cart cart){cart.getMap()。remove(id); } //ショッピングカートをクリアしますpublic public void clearcart(cart cart){cart.getMap()。clear(); } //ショッピングアイテムの数量を変更しますpublic void changeItemquantity(string id、string momation、cart){cartitem item = cart.getmap()。get(id); item.setquantity(integer.parseint(数量)); }}Webレイヤー:
ListBookservlet.java:すべての本を表示します
パッケージcom.web.controller; import java.io.ioexception; import java.io.printwriter; import java.util.map; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; Import javax.servlet.http.httpservletreqest; javax.servlet.http.httpservletresponse; Import com.service.businessservice; //すべての本を取得する//すべての本を取得するパブリッククラスリストextends httpservlet {public void doget(httpservletrequestリクエスト、httpservletresceptions = httpservletecception = new rebletexextextextexception、Ioeexception、ioeexception BusinessService();マップマップ= service.getallbook(); request.setattribute( "Map"、Map); request.getRequestDispatcher( "/web-inf/jsp/listbook.jsp")。 } public void dopost(httpservletrequest request、httpservletResponse応答)servletexception、ioexception {doget(request、response); }}buyservlet.java:購入リクエストの処理
パッケージcom.web.controller; import java.io.ioexception; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; Import javax.servlet.http.httpservletrequest; import javax.servlet.http.http.http.httpservrelet com.domain.cart; import com.service.businessservice; //完全な本の購入//完全な本の購入パブリッククラスのbuyservlet拡張httpservlet {public void doget(httpservletrequest request、httpservletresponse応答)スローServletexception、ioexception {String id = request.getparameter; BusinessService service = new BusinessService(); book book = service.findbook(id); //ユーザーのショッピングカート=(cart)request.getSession()。getAttribute( "cart"); if(cart == null){cart = new cart(); request.getSession()。setAttribute( "cart"、cart); } //ユーザーのショッピングカートに本を追加し、cart.add(book)の購入を完了します。 //response.sendredirect("/web-inf/jsp/listcart.jsp "); request.getRequestDispatcher( "/web-inf/jsp/listcart.jsp")。 } public void dopost(httpservletrequest request、httpservletResponse応答)servletexception、ioexception {doget(request、response); }}deleteItemservlet.java:特定の製品を削除します
パッケージcom.web.controller; Import java.io.ioexception; Import javax.servlet.servletexception; Import javax.servlet.http.httpservlet; Import javax.servlet.http.httpservletrequest; Import javax.http.http.http.httpservleteSp com.service.businessService; //指定されたショッピングアイテムパブリッククラスを削除するパブリッククラス削除extends httpservlet {public void doget(httpservletrequest request、httpservletrequest Response)servletexception、ioexception、ioexception {string id = request.getParameter( "id");カートカート=(cart)request.getSession()。getAttribute( "cart"); BusinessService service = new BusinessService(); service.deletecartitem(id、cart); // delete resulce.getRequestDispatcher( "/web-inf/jsp/listcart.jsp")。 } public void dopost(httpservletrequest request、httpservletResponse応答)servletexception、ioexception {doget(request、response); }}ClearCartServlet.java:カートをクリアします
パッケージcom.web.controller; import java.io.ioexception; Import java.io.printwriter; Import javax.servlet.servletexception; Import javax.servlet.httpservlet; Import javax.servlet.http.httptprequest; Import; javax.servlet.http.httpservletResponse; Import com.domain.cart; Import com.service.businessservice; //クリアショッピングカートパブリッククラスClearCartServletのクリア{public void doget(httpservletrequest request、httpservletresporrequest responsection) request.getSession()。getAttribute( "cart"); BusinessService service = new BusinessService(); service.clearcart(cart); request.getRequestDispatcher( "/web-inf/jsp/listcart.jsp")。 } public void dopost(httpservletrequest request、httpservletResponse応答)servletexception、ioexception {doget(request、response); }}ChangeQuantityServlet.java:ショッピングカートで指定された製品の量を変更する
パッケージcom.web.controller; import java.io.ioexception; Import java.io.printwriter; Import javax.servlet.servletexception; Import javax.servlet.httpservlet; Import javax.servlet.http.httptprequest; Import; javax.servlet.http.httpservletresponse; Import com.domain.cart; Import com.service.businessservice; //ショッピングカートの本を指定された数量の公開QuantityServlet拡張httpservlet {public void doget(httpserequest request、httpretrequestリクエスト、httpsrequentRequest、 ioException {string id = request.getParameter( "id");文字列数量= request.getParameter( "数量");カートカート=(cart)request.getSession()。getAttribute( "cart"); BusinessService service = new BusinessService(); service.changeitemquantity(id、数量、cart); request.getRequestDispatcher( "/web-inf/jsp/listcart.jsp")。 } public void dopost(httpservletrequest request、httpservletResponse応答)servletexception、ioexception {doget(request、response); }}JSPページ:
Webroot/web-inf/jsp/listbook.jsp:bookリストを表示します
<%@ page Language = "Java" Import = "Java.util。*" pageEncoding = "utf-8"%> <%@ taglib uri = "http://java.sun.com/jsp/jstl/core"プレフィックス= "C"%> < Transitional // en "> <html> <head> <title>ブックリストページ</title> </head> <body style =" text-align:center "> <h1> book list </h1> <table> <ttr> <td> bookタイトル</td> <td>著者著者</td> <td> <c:foreach var = "entry" items = "$ {map}"> <tr> <td> $ {entry.value.name} </td> <td> $ {entry.value.author} </td> <td> $ {entry.value.price} </td> <td> $ {entr.value. </td> </td> </td> href = "$ {pagecontext.request.contextpath}/servlet/buyservlet?id = $ {entry.value.id}" rel = "external nofollow"ターゲット= "_ blank"> </td> </tr> </c:foreach> </table> </body> <Webroot/web-inf/jsp/listcart.jsp:ショッピングカートリストを表示します
<%@ page Language = "Java" Import = "Java.util。*" pageEncoding = "utf-8"%> <%@ taglib uri = "http://java.sun.com/jsp/jstl/core"プレフィックス= "C"%> < Transitional // en "> <html> <head> <title> car list </title> <script type =" text/javascript "> function deleteitem(id){var b = window.confirm(" delete ?? "); if(b){window.location.href = "$ {pagecontext.request.contextpath}/servlet/deleteemservlet?id =" rel = "external nofollow" +id; }} function clearcart(){var b = window.confirm( "クリアを確認しましたか??"); if(b){window.location.href = "$ {pagecontext.request.contextpath}/servlet/clearcartservlet" rel = "external nofollow"; }} function changequantity(input、id、oldvalue){var数量= input.value; // sdfsfsを変更する数値を取得 /* //ユーザー入力の数が数字であるかどうかを確認しますif(isnan(数量)){alert( "number !!"); input.value = oldValue;戻る; } *///ユーザー入力の数が正の整数であるかどうかを確認します(数量<0 ||数量!= parseInt(数量)){alert( "positive integer !!"); input.value = oldValue;戻る; } var b = window.confirm( "本の数が次のように変更されていることを確認してください:" +数量); if(b){window.location.href = "$ {pagecontext.request.contextpath}/servlet/changequantityservlet?id =" rel = "external nofollow" + id + "&quant =" + quanty; }} </script> </head> <body style = "text-align:center"> <h1>ショッピングカートリスト</h1> <c:if test = "$ {empty(cart.map)}">アイテムを購入しませんでした! ! ! </c:if> <c:if test = "$ {!empty(cart.map)}"> <table> <tr> <td> <td> bookタイトル</td> <td>著者</td> <td> unit </td> <td> Quants </td> <td> subtotal </td </td> </td> <項目= "$ {cart.map}"> <tr> <td> $ {entry.value.book.name} </td> <td> $ {entry.value.book.author} </td> <td> $ $ {entry.value.book.price} </td> <td> <入力タイプ= " } "onchange =" changequantity(this、$ {entry.key}、$ {entry.value.quantity}) "> </td> <td> $ {entry.value.price} </td> <td> <a href =" javascript:void(0) " onclick = "deleteitem($ {entry.key})"> delete </a> <! - ハイパーリンクのデフォルト動作を削除 - > </td> </tr> </c:foreach> <ttr> <td colspan = "3">合計価格</td> <td colspan = "2" 2 " colspan = "1"> <a href = "javascript:void(0)" rel = "returation nofollow" rel = "external nofollow" onclick = "clearcart()"> clear </a> </td> </tr> </table> </c:if> </body> </html> </c:上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。