Struts2完成CRUD(增 刪 改 查)功效實例代碼。本站提示廣大學習愛好者:(Struts2完成CRUD(增 刪 改 查)功效實例代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是Struts2完成CRUD(增 刪 改 查)功效實例代碼正文
CRUD是Create(創立)、Read(讀取)、Update(更新)和Delete(刪除)的縮寫,它是通俗運用法式的縮影。假如您控制了某框架的CRUD編寫,那末意味可使用該框架創立通俗運用法式了,所以年夜家應用新框架開辟OLTP(Online Transaction Processing)運用法式時,起首會研討一下若何編寫CRUD。這相似於年夜家在進修新編程說話時愛好編寫“Hello World”。
本文旨在講述Struts 2上的CRUD開辟,所認為了例子的簡略易懂,我不會花時光在數據庫的操作上。取而代之的是一個模仿數據庫的哈希表(Hash Map)。
詳細完成
起首,讓我們看看的“冒牌”的DAO(Data Access Object,數據拜訪對象),代碼以下:
package tutorial.dao; import java.util.Collection; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import tutorial.model.Book; public class BookDao { private static final BookDao instance; private static final ConcurrentMap<String, Book> data; static { instance = new BookDao(); data = new ConcurrentHashMap<String, Book>(); data.put("978-0735619678", new Book("978-0735619678", "Code Complete, Second Edition", 32.99)); data.put("978-0596007867", new Book("978-0596007867", "The Art of Project Management", 35.96)); data.put("978-0201633610", new Book("978-0201633610", "Design Patterns: Elements of Reusable Object-Oriented Software", 43.19)); data.put("978-0596527341", new Book("978-0596527341", "Information Architecture for the World Wide Web: Designing Large-Scale Web Sites", 25.19)); data.put("978-0735605350", new Book("978-0735605350", "Software Estimation: Demystifying the Black Art", 25.19)); } private BookDao() {} public static BookDao getInstance() { return instance; } public Collection<Book> getBooks() { return data.values(); } public Book getBook(String isbn) { return data.get(isbn); } public void storeBook(Book book) { data.put(book.getIsbn(), book); } public void removeBook(String isbn) { data.remove(isbn); } public void removeBooks(String[] isbns) { for(String isbn : isbns) { data.remove(isbn); } } }
清單1 src/tutorial/dao/BookDao.java
以上代碼信任不消說明年夜家也清晰,我應用ConcurrentMap數據構造存儲Book對象,這重要是為了便利檢索和保留Book對象;別的,我還將data變量設為靜態獨一來模仿運用法式的數據庫。
接上去是的數據模子Book類,代碼以下:
package tutorial.model; public class Book { private String isbn; private String title; private double price; public Book() { } public Book(String isbn, String title, double price) { this.isbn = isbn; this.title = title; this.price = price; } public String getIsbn() { return isbn; } public void setIsbn(String isbn) { this.isbn = isbn; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
清單2 src/tutorial/model/Book.java
Book類有三個屬性isbn,、title和price分離代表書本的編號、稱號和價錢,個中編號用於獨一標識書本(相當數據庫中的主鍵)。
然後,我們再來看看Action類的代碼:
package tutorial.action; import java.util.Collection; import tutorial.dao.BookDao; import tutorial.model.Book; import com.opensymphony.xwork2.ActionSupport; public class BookAction extends ActionSupport { private static final long serialVersionUID = 872316812305356L; private String isbn; private String[] isbns; private Book book; private Collection<Book> books; private BookDao dao = BookDao.getInstance(); public Book getBook() { return book; } public void setBook(Book book) { this.book = book; } public String getIsbn() { return isbn; } public void setIsbn(String isbn) { this.isbn = isbn; } public String[] getIsbns() { return isbns; } public void setIsbns(String[] isbns) { this.isbns = isbns; } public Collection<Book> getBooks() { return books; } public void setBooks(Collection<Book> books) { this.books = books; } public String load() { book = dao.getBook(isbn); return SUCCESS; } public String list() { books = dao.getBooks(); return SUCCESS; } public String store() { dao.storeBook(book); return SUCCESS; } public String remove() { if(null != isbn) { dao.removeBook(isbn); } else { dao.removeBooks(isbns); } return SUCCESS; } }
清單3 src/tutorial/action/BookAction.java
BookAction類中屬性isbn用於表現待編纂或刪除的書本的編號,屬性isbns用於表現多個待刪除的書本的編號數組,屬性book表現以後書本,屬性books則表現以後的書本列表。BookAction有四個Action辦法分離是load、list、store和remove,也等於CRUD都集中在BookAction中完成。
再上去是Action的設置裝備擺設代碼:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="Struts2_CRUD_DEMO" extends="struts-default" namespace="/Book"> <action name="List" class="tutorial.action.BookAction" method="list"> <result>List.jsp</result> </action> <action name="Edit" class="tutorial.action.BookAction" method="load"> <result>Edit.jsp</result> </action> <action name="Store" class="tutorial.action.BookAction" method="store"> <result type="redirect">List.action</result> </action> <action name="Remove" class="tutorial.action.BookAction" method="remove"> <result type="redirect">List.action</result> </action> </package> </struts>
清單4 src/struts.xml
以上的設置裝備擺設中,我應用了四個Action界說。它們都在“/Book”名值空間內。如許我便可以分離經由過程“http://localhost:8080/Struts2_CRUD/Book/List.action”、“http://localhost:8080/Struts2_CRUD/Book/Edit.action”、“http://localhost:8080/Struts2_CRUD/Book/Store.action”和“http://localhost:8080/Struts2_CRUD/Book/Remove.action”來挪用BookAction的四個Action辦法停止CRUD操作。固然,這只是小我愛好,你年夜可以只界說一個Action(假定其稱號為“Book”),以後經由過程“http://localhost:8080/Struts2_CRUD/Book!list.action”的方法來拜訪,具體做法請參考《Struts 2.0的Action講授》。別的,我因為願望在完成編纂或刪除以後回到列表頁,所以應用類型為redirect(重定向)的result。
上面是列表頁面的代碼:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Book List</title> <style type="text/css"> table { border: 1px solid black; border-collapse: collapse; } table thead tr th { border: 1px solid black; padding: 3px; background-color: #cccccc; } table tbody tr td { border: 1px solid black; padding: 3px; } </style> </head> <body> <h2>Book List</h2> <s:form action="Remove" theme="simple"> <table cellspacing="0"> <thead> <tr> <th>Select</th> <th>ISBN</th> <th>Title</th> <th>Price</th> <th>Operation</th> </tr> </thead> <tbody> <s:iterator value="books"> <tr> <td><input type="checkbox" name="isbns" value='<s:property value="isbn" />' /></td> <td><s:property value="isbn" /></td> <td><s:property value="title" /></td> <td>$<s:property value="price" /></td> <td> <a href='<s:url action="Edit"><s:param name="isbn" value="isbn" /></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> </table> <s:submit value="Remove" /><a href="Edit.jsp">Add Book</a> </s:form> </body> </html>
清單5 WebContent/Book/List.jsp
以上代碼,值得留意的是在<s:form>標簽,我設置了theme屬性為“simple”,如許可以撤消其默許的表格結構。之前,有些同伙問我“假如不願望提交按鈕放在左邊應當如何做?”,上述做汗是謎底之一。固然,更佳的做法自界說一個theme,並將其設為默許運用到全部站點,如斯一來便可以獲得同一的站點作風。我會在今後的文章中會對此作具體的描寫。
編纂或添加書本的頁面代碼以下:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Book</title> </head> <body> <h2> <s:if test="null == book"> Add Book </s:if> <s:else> Edit Book </s:else> </h2> <s:form action="Store" > <s:textfield name="book.isbn" label="ISBN" /> <s:textfield name="book.title" label="Title" /> <s:textfield name="book.price" label="Price" /> <s:submit /> </s:form> </body> </html>
清單6 WebContent/Book/Edit.jsp
假如book為null,則注解該頁面用於添加書本,反之則為編纂頁面。
為了便利年夜家運轉示例,我把web.xml的代碼也貼出來,以下:
<?xml version="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://java.sun.com/xml/ns/j2ee 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-mapping> <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,湧現以下圖所示頁面:
清單8 列表頁面
點擊“Add Book”,湧現以下圖所示頁面:
清單9 添加書本頁面
撤退退卻回到列表頁面,點擊“Edit”,湧現以下圖所示頁面:
清單10 編纂書本頁面
總結
本文只是粗略地了引見Struts 2的CRUD完成辦法,所以有許多功效沒有完成,如國際化和數據校驗等。年夜家可以在下面例子的基本將其完美,看成演習也不錯。有不明確的處所迎接給我留言,小編會實時答復年夜家的,在此也異常感激年夜家對網站的支撐!