一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語(yǔ)言 - JAVA教程 - Struts2實(shí)現(xiàn)CRUD(增 刪 改 查)功能實(shí)例代碼

Struts2實(shí)現(xiàn)CRUD(增 刪 改 查)功能實(shí)例代碼

2020-05-09 11:56sky_zhangfan JAVA教程

CRUD是Create(創(chuàng)建)、Read(讀取)、Update(更新)和Delete(刪除)的縮寫(xiě),它是普通應(yīng)用程序的縮影。接下來(lái)通過(guò)本文給大家介紹Struts2實(shí)現(xiàn)CRUD(增 刪 改 查)功能實(shí)例代碼,感興趣的朋友一起看看吧

CRUD是Create(創(chuàng)建)、Read(讀?。?、Update(更新)和Delete(刪除)的縮寫(xiě),它是普通應(yīng)用程序的縮影。如果您掌握了某框架的CRUD編寫(xiě),那么意味可以使用該框架創(chuàng)建普通應(yīng)用程序了,所以大家使用新框架開(kāi)發(fā)OLTP(Online Transaction Processing)應(yīng)用程序時(shí),首先會(huì)研究一下如何編寫(xiě)CRUD。這類似于大家在學(xué)習(xí)新編程語(yǔ)言時(shí)喜歡編寫(xiě)“Hello World”。

本文旨在講述Struts 2上的CRUD開(kāi)發(fā),所以為了例子的簡(jiǎn)單易懂,我不會(huì)花時(shí)間在數(shù)據(jù)庫(kù)的操作上。取而代之的是一個(gè)模擬數(shù)據(jù)庫(kù)的哈希表(Hash Map)。

具體實(shí)現(xiàn)

首先,讓我們看看的“冒牌”的DAO(Data Access Object,數(shù)據(jù)訪問(wèn)對(duì)象),代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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數(shù)據(jù)結(jié)構(gòu)存儲(chǔ)Book對(duì)象,這主要是為了方便檢索和保存Book對(duì)象;另外,我還將data變量設(shè)為靜態(tài)唯一來(lái)模擬應(yīng)用程序的數(shù)據(jù)庫(kù)。

接下來(lái)是的數(shù)據(jù)模型Book類,代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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類有三個(gè)屬性isbn,、title和price分別代表書(shū)籍的編號(hào)、名稱和價(jià)格,其中編號(hào)用于唯一標(biāo)識(shí)書(shū)籍(相當(dāng)數(shù)據(jù)庫(kù)中的主鍵)。

然后,我們?cè)賮?lái)看看Action類的代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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用于表示待編輯或刪除的書(shū)籍的編號(hào),屬性isbns用于表示多個(gè)待刪除的書(shū)籍的編號(hào)數(shù)組,屬性book表示當(dāng)前書(shū)籍,屬性books則表示當(dāng)前的書(shū)籍列表。BookAction有四個(gè)Action方法分別是load、list、store和remove,也即是CRUD都集中在BookAction中實(shí)現(xiàn)。

再下來(lái)是Action的配置代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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

以上的配置中,我使用了四個(gè)Action定義。它們都在“/Book”名值空間內(nèi)。這樣我就可以分別通過(guò)“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”來(lái)調(diào)用BookAction的四個(gè)Action方法進(jìn)行CRUD操作。當(dāng)然,這只是個(gè)人喜好,你大可以只定義一個(gè)Action(假設(shè)其名稱為“Book”),之后通過(guò)“http://localhost:8080/Struts2_CRUD/Book!list.action”的方式來(lái)訪問(wèn),詳細(xì)做法請(qǐng)參考《Struts 2.0的Action講解》。另外,我由于希望在完成編輯或刪除之后回到列表頁(yè),所以使用類型為redirect(重定向)的result。

下面是列表頁(yè)面的代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<%@ 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>標(biāo)簽,我設(shè)置了theme屬性為“simple”,這樣可以取消其默認(rèn)的表格布局。之前,有些朋友問(wèn)我“如果不希望提交按鈕放在右邊應(yīng)該怎樣做?”,上述做汗是答案之一。當(dāng)然,更佳的做法自定義一個(gè)theme,并將其設(shè)為默認(rèn)應(yīng)用到整個(gè)站點(diǎn),如此一來(lái)就可以得到統(tǒng)一的站點(diǎn)風(fēng)格。我會(huì)在以后的文章中會(huì)對(duì)此作詳細(xì)的描述。

編輯或添加書(shū)籍的頁(yè)面代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<%@ 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,則表明該頁(yè)面用于添加書(shū)籍,反之則為編輯頁(yè)面。

為了方便大家運(yùn)行示例,我把web.xml的代碼也貼出來(lái),如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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

大功告成,下面發(fā)布運(yùn)行應(yīng)用程序,在瀏覽器中鍵入:http://localhost:8080/Struts2_CRUD/Book/List.action,出現(xiàn)如下圖所示頁(yè)面:

Struts2實(shí)現(xiàn)CRUD(增 刪 改 查)功能實(shí)例代碼

清單8 列表頁(yè)面

點(diǎn)擊“Add Book”,出現(xiàn)如下圖所示頁(yè)面:

Struts2實(shí)現(xiàn)CRUD(增 刪 改 查)功能實(shí)例代碼

清單9 添加書(shū)籍頁(yè)面

后退回到列表頁(yè)面,點(diǎn)擊“Edit”,出現(xiàn)如下圖所示頁(yè)面:

Struts2實(shí)現(xiàn)CRUD(增 刪 改 查)功能實(shí)例代碼

清單10 編輯書(shū)籍頁(yè)面

總結(jié)

本文只是粗略地了介紹Struts 2的CRUD實(shí)現(xiàn)方法,所以有很多功能沒(méi)有實(shí)現(xiàn),如國(guó)際化和數(shù)據(jù)校驗(yàn)等。大家可以在上面例子的基礎(chǔ)將其完善,當(dāng)作練習(xí)也不錯(cuò)。有不明白的地方歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的,在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 色老板视频在线 | 毛片免费全部免费观看 | 日韩v| 国产一精品一av一免费爽爽 | 亚洲图片一区二区三区 | 高清不卡一区二区 | 国产成人h视频在线播放网站 | 99久久精品免费精品国产 | 欧美日韩一区二区三区免费不卡 | 国产精品www视频免费看 | 午夜久 | 午夜小视频网站 | 精品无人区一区二区三区 | 亚洲成av人在线视 | xxxxx性中国hd | 国产91精选在线观看麻豆 | 天天做天天爽天天谢 | 国产成人综合精品 | 天堂色 | 911亚洲精品国内自产 | 精品视频一区二区观看 | 欧美视频在线播放观看免费福利资源 | 亚洲欧美日韩在线观看看另类 | 2019天天干天天操 | 国产欧美精品一区二区三区–老狼 | 日韩大片在线播放 | 国产第一福利 | 韩国甜性涩爱在线播放 | www.87福利| 亚洲春黄在线观看 | 亚洲狠狠婷婷综合久久久久网站 | 无遮挡h肉动漫高清在线 | 99视频在线观看视频一区 | 日韩性事 | chinesemature老女人 | 久久亚洲精品AV无码四区 | 91精品国产免费久久国语蜜臀 | 成人国产在线播放 | 四虎影音 | 国产日韩精品一区二区在线观看 | 日本全黄三级在线观看 |