Javaeeでは、Model1モデルはJSSPページに集中しています。 JSSPは、ブラウザの要求を論理的に処理するだけでなく(Javabeanを使用)、データベースにアクセスするときに関連するページを表示する必要があります。
Model1モデルには、サーブレットはありません。
Model1の結果図は次のとおりです。
Model1の保守性とスケーラビリティが低く、小規模プロジェクトにのみ適しています。
最初に結果を実行します
goods.jsp
<%@page Import = "Entity.Items"%> <%@page Import = "dao.itemsdao"%> <%@page Language = "java" import = "java.util。*" pageencoding = "utf-8"% string basepath = request.getscheme() + "://" + request.getServername() + ":" + request.getServerport() + path + "/"; %> <!doctype html public " - // w3c // dtd html 4.01 transitional // en"> <html> <head> <base href = "<%= basepath%>"> <title> my jsp 'index.jsp'スタートページ</title> <meta http-equiv = "pragma"> <meta " http-equiv = "cache-control" content = "no-cache"> <meta http-equiv = "content =" 0 "> <meta http-equiv =" keyword1、keyword3 "keyword3"> <meta http-equiv = "content =" type = "text/css" href = "styles.css"> - > <style type = "text/css"> div {float:left;マージン:10px; } div dd {マージン:0px;フォントサイズ:10pt; } div dd.dd_name {color:blue; } div dd.dd_city {color:#000; } </style> </head> <body> <center> <h1>製品ディスプレイ</h1> <hr> <table cellpadding = "0" cellpacing = "0"> <tr> <td> <%itemsdao dao = new itemsdao(); arrayList <items> list = new ArrayList <items>(); //すべての製品をDAOから取得し、それらをリストコレクションリスト= dao.getAllitems()に保存します。 if(list!= null && list.size()> 0){//コレクションをループして(int i = 0; i <list.size(); i ++){item item = list.get(i); %> <div> <dl> <dt> <a href = "details.jsp?id = <%= item.getid()%>"> <img src = "image/<%= item.getpicture()%> </a> </dt> <dd> <%= item.getName() <%= item.getPrice()%> </dd> </dl> </div> <%}}%> </td> </tr </table> </table> </center> </body> </html>コード内の製品を表す画像
<SPAN STYLE = "White-Space:pre"> </span> <a href = "details.jsp?id = <%= item.getid()%>"> <img src = "images/<%= item.getpicture()%>"/> </a>
製品画像をクリックして、現在の製品のIDを詳細ページに渡す
details.jsp製品のIDを介して詳細な製品を表示し、閲覧履歴はCookieによって維持されます
<%@page Import = "org.apache.taglibs.standard.tandard.tag.common.xml.foreachtag"%> <%@page Import = "%> <%@page Import =" dao.itemsdao "%> <%@page Language =" Java.util。 request.getContextPath(); string basepath = request.getscheme() + "://" + request.getServername() + ":" + request.getServerport() + path + "/"; %> <!doctype html public " - // w3c // dtd html 4.01 transitional // en"> <html> <head> <base href = "<%= basepath%>"> <title> my jsp 'index.jsp'スタートページ</title> <meta http-equiv = "pragma"> <meta " http-equiv = "cache-control" content = "no-cache"> <meta http-equiv = "content =" 0 "> <meta http-equiv =" content = "keyword1、keyword3、keyword3"> <meta http-equiv = "content =" {border:1;背景:#eaeaee; } #HistoryView TD {font-size:10px; } </style> </head> <body> <center> <h1>製品の詳細</h1> <hr> <table cellpadding = "0" cellpacing = "0"> <tr> <td> <center> <table> <%itemsdao dao = new itemsdao(); //リクエスト項目から送信された製品IDに従ってDAOで対応する製品オブジェクトを取得します= dao.getItembyId(integer.parseint(request .getParameter( "id"))); if(item!= null){%> <tr> <td rowspan = "5"> <img src = "images/<%= item.getpicture()%>"> </td> </tr> <tr> <td> <b> <%= item.getName()%> </b> <%= item.getCity()%> </td> </tr> <tr> <td id = "pricename">価格:<%= item.getPrice()%> ¥ </td> </tr> <tr> <td id = "pricename">価格:<%= item.getprice()% request.getCookies();文字列historystr = ""; for(cookie c:cookie){if(c.getname()。equals( "history")){historystr = c.getValue(); }} historystr + = item.getId() + "、"; Cookie C = new Cookie( "History"、HistoryStr); // Cookies Response.AddCookie(c)をリセットします。 %> </table> </center> </td> <td valign = "top" id = "historyview"> <center> <table> <table> <tr> <tr> <td> <b>閲覧した製品</b> </td> <// dao.gethistoryview(historystr); if(HistoryItems!= null && HistoryItems.size()> 0){//(項目historyitem:historyitems){%> <tr> <td> <td> <a hrefs.jsp?id = <%= historyitem.getid()%> "> <img src =" images/<centem.getem.getem.getem.getem.getem.getem.getem.getem.getem.getem.getem. </tr> <tr> <td> <b> <%= HistoryItem.getName()%> </b> </td> </tr> <tr> <td> ogirion:<%= historyitem.getCity()%> </td> </tr <%}}}%> </table> </tr> DAOレイヤーは、データベース内の製品のクエリ操作を担当します
パッケージダオ; Java.sql.Connectionをインポートします。 Java.sql.preparedStatementをインポートします。 java.sql.resultsetをインポートします。 java.sql.sqlexceptionをインポートします。 java.util.arraylistをインポートします。 util.dbhelperをインポートします。 Import Entity.Items; // business logic of product class public class etermao {//すべての製品情報を取得public arraylist <items> getAllitems(){//製品コレクションArrayList <items> list = new ArrayList <items>();接続conn = null; represedStatement PS = null;結果rs = null; try {conn = dbhelper.getConnection(); string sql = "select * fromアイテム"; // sqlステートメントps = conn.preparestatement(sql); rs = ps.executequery(); //クエリの結果をコレクションに順番に追加します(rs.next()){item item = new item(); item.setId(rs.getint( "id")); item.setName(rs.getString( "name")); item.setCity(rs.getString( "City")); item.setprice(rs.getDouble( "price")); item.setPicture(rs.getString( "picture")); item.setNumber(rs.getint( "number")); list.add(item); }} catch(sqlexception e){e.printstacktrace(); }最後に{//リソースを閉じるif(rs!= null){try {rs.close(); } catch(sqlexception e){// todo auto-enerated catch block e.printstacktrace(); }} if(ps!= null){try {ps.close(); } catch(sqlexception e){// todo auto-enerated catch block e.printstacktrace(); }}} return list; } //製品番号に基づいて製品情報を取得する公開項目getItembyId(int id){item item = new item();接続con = null; represedStatement PS = null;結果rs = null; string sql = "select * from item where id =?"; try {con = dbhelper.getConnection(); ps = con.preparestatement(sql); ps.setint(1、id); rs = ps.executequery(); // IDが見つかった場合、アイテムオブジェクトのIDの初期化if(rs.next()){item.setId(rs.getint( "id")); item.setName(rs.getString( "name")); item.setCity(rs.getString( "City")); item.setprice(rs.getDouble( "price")); item.setPicture(rs.getString( "picture")); item.setPicture(rs.getString( "picture")); item.setNumber(rs.getint( "number")); }} catch(sqlexception e){// todo auto-feenated catch block e.printstacktrace(); }最後に{//リソースを閉じるif(rs!= null){try {rs.close(); } catch(sqlexception e){// todo auto-enerated catch block e.printstacktrace(); }} if(ps!= null){try {ps.close(); } catch(sqlexception e){// todo auto-enerated catch block e.printstacktrace(); }}} return item; } // Cookiesに基づいてブラウジングされた最後の3つのアイテムを取得しますpublic arraylist <items> gethistoryView(string cookie){arraylist <items> list = new arraylist <items>(); string ids [] = cookie.split( "、"); int counts = 3; //最後の3つのレコードは(ids!= null && ids.length> 0){for(int i = ids.length -1; i> = 0 && i> ids.length -1; i-){for(int i = ids.length -1; i--){iteme item = getItembyid(integer.parseint(ids [i]); / * *最初に、現在のアイテムがコレクションに存在するかどうかを判断します。 +1が存在する場合は、もう一度読み取ります(リストコレクションに3つのオブジェクトがあることが保証されています)このアイテムを追加しないでください*/ if(list.contains(item)){counts ++;続く; } list.add(item); }} return list; }}エンティティカテゴリの項目
パッケージエンティティ。パブリッククラス項目{private int id;プライベート文字列名;プライベートストリングシティ;プライベートダブル価格。プライベートINT番号;プライベートストリング画像; public int getid(){return id; } public void setid(int id){this.id = id; } public string getname(){return name; } public void setName(string name){this.name = name; } public string getCity(){return City; } public void setCity(String City){this.city = city; } public double getPrice(){return price; } public void setPrice(2倍の価格){this.price = price; } public int getNumber(){return number; } public void setnumber(int number){this.number = number; } public string getPicture(){return picture; } public void setPicture(string picture){this.picture = picture; } @Override public int hashcode(){// todo auto-eneratedメソッドスタブthis.getid()+this.getname()。hashcode(); } @Override public boolean equals(object obj){if(this == obj){return true; } else {if(obj instanceofアイテム){item item =(items)obj; if(this.getId()== item.getId()&& this.getName()。equals(item.getName())){return true; }}} falseを返します。 }}ここでは、HasCodeを書き直し、比較メソッドを変更する方法に等しくなります(すべてのアイテムは新しいオブジェクトです。2つの製品の内容が同じであっても、それらは等しくありません。したがって、比較方法を変更する必要があります)
閲覧履歴のために、現在の製品ブラウジング履歴を更新することはできず、すべての製品が製品であるためです。ブラウジング履歴に製品が1つしかないことを確認する必要があります。したがって、DAOレイヤーのGethistoryViewメソッドにはこのコードがあります
<span style = "ホワイトスペース:pre"> </span> if(list.contains(item)){counts ++;続く; }次に、ツールクラスがあります
dbhelpher singletonパターン接続オブジェクトを取得します
パッケージutil; Java.sql.Connectionをインポートします。 Java.sql.drivermanagerをインポートします。 java.sql.sqlexceptionをインポートします。パブリッククラスdbhelper {private static final string driver = "com.mysql.jdbc.driver"; private static final string url = "jdbc:mysql:// localhost:3306/shopping?useunicode = true&charcterencoding = utf-8";プライベート静的最終文字列username = "root";プライベート静的最終文字列パスワード= "123";プライベート静的接続con = null; //静的ブロックコードは、ドライバーのstatic {try {class.forname(driver); } catch(classNotFoundException e){// todo auto-enerated catch block e.printstacktrace(); }} public static Connection getConnection(){if(con == null){try {con = drivermanager.getConnection(url、username、password); } catch(sqlexception e){// todo auto-enerated catch block e.printstacktrace(); }} return con; }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。