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);
}
}
}
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;
}
}
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;
}
}
<?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>
<%@ 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>
<%@ 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>
<?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>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有