全部博文(2065)
分类: Java
2010-06-14 22:29:20
实现CRUD操作
步骤一:编写DAO层即数据访问对象层
package tutorial.dao;
import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import tutorial.model.Book;
//DAO层。接收BEAN属性实现数据的访问与变更功能
public class BookDao {
private static final BookDao instance;
private static final ConcurrentMap
static {
instance = new BookDao();
data = new ConcurrentHashMap
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
return data.values();
}
public Book getBook(String isbn) {
return data.get(isbn);
}//模拟select *
from book where isbn = 'isbn';
public void storeBook(Book book) {
data.put(book.getIsbn(), book);
}//模拟insert into
book
public void removeBook(String isbn){
data.remove(isbn);
}//模拟delete from book where isbn=''
public void removeBooks(String[] isbns){
for (String string : isbns) {
data.remove(string);
}
}//删除掉一个范围里面的全部的编号书目即相当于delete from table where isbn in
public static void main(String[] args) {
BookDao dao = BookDao.getInstance();
BookDao dao2 = BookDao.getInstance();
System.out.println(dao == dao2);//返回 true表示的是 同一个对象了。单实例!!!!
dao.storeBook(new Book("978-0735605334", "Software
Estimation: 4543245 the Black Art", 444.19));
dao2.storeBook(new Book("978-073560533323", "Software
Estimation: 33333 the Black Art", 444.19));
System.out.println(BookDao.data.get("978-0735605334").getTitle());
}
}
用了单实例实现的。
第二步:编写数据BEAN
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 String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
这个类就是JAVABEAN存储数据用的。
第三步:编写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
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
return books;
}
public void setBooks(Collection
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;
}
//在这里面我们定义了CRUD操作CRUD都集中在BookAction
}
定义完了Action之后就开始配置文件
xml version="1.0" encoding="UTF-8"?>
DOCTYPE struts PUBLIC
"-//Apache
Software Foundation//DTD Struts Configuration 2.0//EN"
"">
<struts>
<package name="Struts2_CRUD_DEMO"
extends="struts-default" namespace="/Book">
<action name="List"
class="tutorial.action.BookAction" method="list">
<result>List.jspresult>
action>
<action name="Edit"
class="tutorial.action.BookAction" method="load">
<result>Edit.jspresult>
action>
<action name="Store"
class="tutorial.action.BookAction" method="store">
<result type="redirect">List.actionresult>
action>
<action name="Remove"
class="tutorial.action.BookAction" method="remove">
<result type="redirect">List.actionresult>
action>
package>
struts>
以上的配置中,我使用了四个Action定义。它们都在“/Book”名值空间内。这样我就可以分别通过。
PS:看看这里面我们都可以定义其命名空间的哦!
开始编写相关的JSP页面
List.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
DOCTYPE html PUBLIC "-//W
<html xmlns="">
<head>
<title>Book Listtitle>
<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 Listh2>
<s:form action="Remove"
theme="simple">
<table cellspacing="0">
<thead>
<tr>
<th>Selectth>
<th>ISBNth>
<th>Titleth>
<th>Priceth>
<th>Operationth>
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 Booka>
s:form>
body>
html>
编写Edit.jsp页面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
DOCTYPE html PUBLIC "-//W
<html xmlns="">
<head>
<title>Booktitle>
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>
实现的步骤简单介绍一下:
配置好存储与业务层。然后编写Action这个是一个重点与难点了。写完了Action之后就可以配置好其配置文件了。