CRUD是Create(創(chuàng)建)、Read(讀取)、Update(更新)和Delete(刪除)的縮寫,它是普通應(yīng)用程序的縮影。如果您掌握了某框架的CRUD編寫,那么意味可以使用該框架創(chuàng)建普通應(yīng)用程序了,所以大家使用新框架開發(fā)OLTP(Online Transaction Processing)應(yīng)用程序時,首先會研究一下如何編寫CRUD。這類似于大家在學(xué)習(xí)新編程語言時喜歡編寫“Hello World”。
本文旨在講述Struts 2上的CRUD開發(fā),所以為了例子的簡單易懂,我不會花時間在數(shù)據(jù)庫的操作上。取而代之的是一個模擬數(shù)據(jù)庫的哈希表(Hash Map)。
具體實現(xiàn)
首先,讓我們看看的“冒牌”的DAO(Data Access Object,數(shù)據(jù)訪問對象),代碼如下:






































































以上代碼相信不用解釋大家也清楚,我使用ConcurrentMap數(shù)據(jù)結(jié)構(gòu)存儲Book對象,這主要是為了方便檢索和保存Book對象;另外,我還將data變量設(shè)為靜態(tài)唯一來模擬應(yīng)用程序的數(shù)據(jù)庫。
接下來是的數(shù)據(jù)模型Book類,代碼如下:


























































Book類有三個屬性isbn,、title和price分別代表書籍的編號、名稱和價格,其中編號用于唯一標識書籍(相當數(shù)據(jù)庫中的主鍵)。
然后,我們再來看看Action類的代碼:








































































































BookAction類中屬性isbn用于表示待編輯或刪除的書籍的編號,屬性isbns用于表示多個待刪除的書籍的編號數(shù)組,屬性book表示當前書籍,屬性books則表示當前的書籍列表。BookAction有四個Action方法分別是load、list、store和remove,也即是CRUD都集中在BookAction中實現(xiàn)。
再下來是Action的配置代碼:
<!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>
以上的配置中,我使用了四個Action定義。它們都在“/Book”名值空間內(nèi)。這樣我就可以分別通過“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”來調(diào)用BookAction的四個Action方法進行CRUD操作。當然,這只是個人喜好,你大可以只定義一個Action(假設(shè)其名稱為“Book”),之后通過“http://localhost:8080/Struts2_CRUD/Book!list.action”的方式來訪問,詳細做法請參考《Struts 2.0的Action講解》。另外,我由于希望在完成編輯或刪除之后回到列表頁,所以使用類型為redirect(重定向)的result。
下面是列表頁面的代碼:
<%@ 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>
以上代碼,值得注意的是在<s:form>標簽,我設(shè)置了theme屬性為“simple”,這樣可以取消其默認的表格布局。之前,有些朋友問我“如果不希望提交按鈕放在右邊應(yīng)該怎樣做?”,上述做汗是答案之一。當然,更佳的做法自定義一個theme,并將其設(shè)為默認應(yīng)用到整個站點,如此一來就可以得到統(tǒng)一的站點風格。我會在以后的文章中會對此作詳細的描述。
編輯或添加書籍的頁面代碼如下:
<%@ 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>
如果book為null,則表明該頁面用于添加書籍,反之則為編輯頁面。
為了方便大家運行示例,我把web.xml的代碼也貼出來,如下:
<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>
大功告成,下面發(fā)布運行應(yīng)用程序,在瀏覽器中鍵入:http://localhost:8080/Struts2_CRUD/Book/List.action,出現(xiàn)如下圖所示頁面:
清單8 列表頁面
點擊“Add Book”,出現(xiàn)如下圖所示頁面:
清單9 添加書籍頁面
后退回到列表頁面,點擊“Edit”,出現(xiàn)如下圖所示頁面:
清單10 編輯書籍頁面
總結(jié)
本文只是粗略地了介紹Struts 2的CRUD實現(xiàn)方法,所以有很多功能沒有實現(xiàn),如國際化和數(shù)據(jù)校驗等。大家可以在上面例子的基礎(chǔ)將其完善,當作練習(xí)也不錯。如果過程中有不明白之處,可以參考我早前的文章或給我發(fā)E-Mail:max.m.yuan@gmail.com。