CRUDは、作成、読み取り、更新、削除の略語であり、通常のアプリケーションの縮図です。フレームワークのCRUD執筆を習得する場合、フレームワークを使用して通常のアプリケーションを作成できることを意味します。したがって、新しいフレームワークを使用してOLTP(オンライントランザクション処理)アプリケーションを開発する場合、まずCRUDの書き方を研究します。これは、新しいプログラミング言語を学ぶときに人々が「Hello World」を書くのが好きな方法に似ています。
この記事は、Struts 2でのCrudの開発を説明することを目的としているため、簡単になりやすくなり、データベース操作に時間を費やすことはありません。代わりに、データベースをシミュレートするハッシュテーブル(ハッシュマップ)。
特定の実装
まず、「偽の」DAO(データアクセスオブジェクト、データアクセスオブジェクト)を見てみましょう。コードは次のとおりです。
パッケージTutorial.dao;インポートjava.util.collection;インポートjava.util.concurrent.concurrenthashmap; import java.util.concurrent.concurrentmap; import.model.book; public class bookdao {private static final bookdao static concrentmap < bookdao(); data = new concurrenthashmap <string、book>(); data.put( "978-0735619678"、new book( "、" 978-0735619678 "、" Code Code Complete、第2版、32.99)); Data.put( "978-059600767" book( "978-0596007867"、 "The Art of Project Management"、35.96)))))))))) Book( "978-0596527341"、 "World Wide Webの情報アーキテクチャ:大規模なWebサイトの設計"、25.19)); data.put( "978-0735605350"、新しい本( "978-0735605350") {} public static bookdao getInstance(){return instance;} public collection <book> getbooks(){return data.values();} public book getbook(string data.get(isbn);} public void storebook(book book){book datasbn()、book.getisbn( {data.remove(isbn);} public void removebooks(string [] isbn){for(string isbn:isbn){data.remove(isbn);}}}}}}リスト1 SRC/チュートリアル/DAO/BookDao.java
上記のコードには説明が必要ないと思います。 CONCURRENTMAPデータ構造を使用して、主に本のオブジェクトの検索とストレージを容易にするための帳簿オブジェクトを保存します。さらに、アプリケーションのデータベースをシミュレートするために、データ変数を静的に一意に設定します。
次はデータモデルの本のクラスです。コードは次のとおりです。
パッケージチュートリアル。モデル;パブリッククラスブック{プライベートストリングISBN;プライベートストリングタイトル;プライベートダブルプライス;パブリックブック(){}パブリックブック(String ISBN、String Title、Double Price){this.isbn = isbn; this.title = title; this.price = price;} public string getisbn( isbn;} public double getPrice(){return price;} public void setprice(double fice){this.price = fires;} public string gettitle(){return title;} public void settle(string title){this.title = title;}}}}}リスト2 SRC/チュートリアル/モデル/book.java
本のクラスには3つの属性があります。ISBN、タイトル、価格は、それぞれ本の数、名前、価格を表します。この番号は、本を一意に識別するために使用されます(データベースの主キーに相当)。
次に、アクションクラスのコードを見てみましょう。
パッケージチュートリアル;アクション;インポートjava.util.collection; import tutorial.dao.bookdao; Import tutorial.model.book; Import com.opensymphony.xwork2.actionsupport; public class bookactions ActionSupport {private static final long serialvorsionuid = 872316812303356l; private string書籍;プライベートコレクション<book>本;プライベートブックダオ= bookdao.getInstance(); public book getbook(){return book;} public void setbook(book = book;} public string getisbn(){return isbn;} {return isbn;} public void setisbnns(string [] isbnns){this.isbns = isbnns;} public collection <book> getBooks(){return books;} public void setbooks(collection <book> books){this.books = books;} rutt inuctune(} book = dao.get Book(is) {books = dao.getbooks(); return success;} public string store(){dao.storebook(book); reture success;} public string remove(){if(null!= isbn){dao.removebook(isbn);} else {dao.removebooks(isbns);}リターンサクセス;}リスト3 SRC/チュートリアル/アクション/bookaction.java
Bookactionクラスでは、ISBNの属性を使用して編集または削除される書籍の数を表すために使用され、属性ISBNSは削除される本の配列を表すために使用され、属性本は現在の本を表し、属性の本は現在の本リストを表します。 Bookactionには、負荷、リスト、ストア、削除の4つのアクションメソッドがあります。つまり、CRUDはすべてBookactionで実装されています。
次はアクション構成コードです。
<?xml version = "1.0" encoding = "utf-8"?> <!doctype struts public " - // apacheソフトウェア財団// dtd struts構成2.0 // en" "http://struts.apache.org/dtds/struts-2.0.dtd extends = "struts-default" namespace = "/book"> <action name = "list" method = "list"> <result> jsp </result> </action> <action name = "編集" method = "load"> <result> edit.jsp </result> </action> <action name = "store" method "store" name = "remove" method = "remove"> <result type = "redirect"> list.action </result> </action> </package> </struts>
リスト4 SRC/struts.xml
上記の構成では、4つのアクション定義を使用しました。それらはすべて「/book」名前空間内にあります。このようにして、「http:// localhost:8080/struts2_crud/book/list.action」で、Crud操作の4つのアクションメソッドをCrud操作に呼び出すことができます。 「http:// localhost:8080/struts2_crud/book/remove.action」。もちろん、これは単なる個人的な好みです。アクションを定義するだけ(その名前が「本」であると仮定)、「http:// localhost:8080/struts2_crud/book!list.action」を介してアクセスできます。詳細については、「Struts 2.0アクション説明」を参照してください。また、編集や削除を終えた後にリストページに戻りたいので、タイプリダイレクト(リダイレクト)の結果を使用します。
これがリストページのコードです:
<%@ page Language = "Java" contentType = "text/html; charset = utf-8" pageencoding = "utf-8"%> <%@ taglib prefix = "s" uri = "/struts-tags"%> <!doctype html public " - // "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <hhtml xmlns =" http://www.w3.org/1999/xhtml " 1px solid black; border-collapse:collapse;}テーブルThead tr th {border:1px solid black; padding:3px; background-color:#ccccc;} table tbody td {border:1px solid black; padding:3px;} </style> </head> <body> <h2> book </h2> <""> cellpacing = "0"> <thead> <tr> <th> select </th> <th> isbn </th> <th> price </th> <th> price </th> <th> <th> <th>操作</th> </tr> </thead> <tbody> <s:iterator value = "books"> <td> <入力タイプ= " Value = "ISBN" />> ' /> < /td> <td> <s:プロパティ値= "isbn" /> /> < /td> <td> $ <s:プロパティ値= "価格" /> < /td> <td> <a href =' <s:url action = "edit"> <s /> </s:url> '> edit </a> <a href =' <s:url action = "remove"> <s:param name = "isbn" value = "isbn"/> </s:url> '> delete </a> </td> </tr> </s:iterator> </tbody> </taby> < href = "edit.jsp"> book </a> </s:form> </body> </html>を追加しますリスト5 WebContent/book/list.jsp
上記のコードでは、<s:form>タグでは、テーマプロパティを「シンプル」に設定して、デフォルトのテーブルレイアウトをキャンセルできることに注意してください。以前、何人かの友人は私に「送信ボタンを右に配置したくない場合はどうすればよいですか?」と尋ねました。上記の発汗は答えの1つです。もちろん、テーマをカスタマイズしてデフォルトでサイト全体に適用して、統一されたサイトスタイルを取得できるようにすることをお勧めします。これについては、今後の記事で詳しく説明します。
書籍を編集または追加するためのページコードは次のとおりです。
<%@ page Language = "Java" contentType = "text/html; charset = utf-8" pageencoding = "utf-8"%> <%@ taglib prefix = "s" uri = "/struts-tags"%> <!doctype html public " - // "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <hhtml xmlns =" http://www.w3.org/1999/xhtml " == book "> book < /s:if> <s:else> edit book < /s:els> < /h2> <s:form action =" store "> <s:" book.isbn "label =" isbn " /> <s:textfield name =" book.title "label =" title " />> <S /> </s:form> </body> </html>
リスト6 WebContent/book/edit.jsp
本がnullの場合、ページが本を追加するために使用され、その逆も編集ページです。
すべての人が例を実行するように促進するために、次のようにweb.xmlコードも投稿しました。
<?xmlバージョン= "1.0" encoding = "utf-8"?> <web-app id = "webapp_9" version = "2.4" xmlns = "http://java.sun.com/xml/ns/j2ee" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation = "http:nns.s.sun http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name> struts 2 fileupload </display-name> <filter> <filter-name> struts2 </filter-name> <filter-class> org.apache.struts2.dispatcher.filterdispatcher </filter-class> </filter> <フィルターマッピング> <filter-name> struts2 </filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file> index.html </welcome-file> </welcome-file-list> </web-app>
リスト7 WebContent/web-inf/web.xml
タスクが実行され、以下がアプリケーションを実行するために公開されます。http:// localhost:8080/struts2_crud/book/list.action inaction in the browser、および以下の図に示すページが表示されます。
リスト8のリストページ
[本の追加]をクリックすると、以下の図に示されているページが表示されます。
リスト9ブックページを追加します
リストページに戻り、[編集]をクリックすると、以下の図に示すページが表示されます。
リスト10編集ブックページ
要約します
この記事では、Struts 2のCRUD実装方法を簡単に紹介するため、国際化やデータ検証など、多くの機能が実装されていません。上記の例に基づいて改善することもできます。また、練習することもできます。わからない場合は、メッセージを残してください。編集者は、すべての人に時間内に返信します。 Wulin Network Webサイトへのサポートに感謝します!