struts2-10-属性驱动实现书籍的增删改查
Posted ~花开不败~
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了struts2-10-属性驱动实现书籍的增删改查相关的知识,希望对你有一定的参考价值。
一:集合模拟数据库 nuc.sw.db--->BookDB.java
1 package nuc.sw.db; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import nuc.sw.vo.Book; 7 8 public class BookDB { 9 //定义静态集合,类可以打点调用,并且类的多个对象也可以使用 10 public static List<Book> bookList=new ArrayList<Book>(); 11 }
二:DAO层对集合做增删改查相应 的操作 nuc.sw.dao--->BookDAO.java
1 package nuc.sw.dao; 2 3 import java.util.List; 4 import nuc.sw.db.BookDB; 5 import nuc.sw.vo.Book; 6 7 public class BookDAO { 8 9 10 //通过模拟数据库获取集合 11 List<Book> list = BookDB.bookList; 12 13 //添加书籍方法 系统默认(add) 14 public void addBook(Book book){ 15 list.add(book); 16 } 17 18 //获取书籍方法 若没有get方法,则返回空 19 public List<Book> getBook(){ 20 return list; 21 } 22 23 //查找书籍信息 24 public Book findBook(Book book){ 25 //泛化遍历集合 满足条件的直接定义 26 for(Book bookOne:list){ 27 if(bookOne.getBookName().equals(book.getBookName())) 28 return bookOne; 29 } 30 return null; 31 } 32 33 //修改书籍信息 34 public void updateBook(Book book1,Book book2){ 35 book1.setBookAuthor(book2.getBookAuthor()); 36 book1.setBookPrice(book2.getBookPrice()); 37 } 38 39 //删除书籍的方法 系统默认(remove) 40 public void deleteBook(Book book){ 41 list.remove(book); 42 } 43 }
三:VO层对Book进行属性说明
1 package nuc.sw.vo; 2 3 public class Book { 4 private String bookName; 5 private String bookAuthor; 6 private float bookPrice; 7 public String getBookName() { 8 return bookName; 9 } 10 public void setBookName(String bookName) { 11 this.bookName = bookName; 12 } 13 public String getBookAuthor() { 14 return bookAuthor; 15 } 16 public void setBookAuthor(String bookAuthor) { 17 this.bookAuthor = bookAuthor; 18 } 19 public float getBookPrice() { 20 return bookPrice; 21 } 22 public void setBookPrice(float bookPrice) { 23 this.bookPrice = bookPrice; 24 } 25 26 27 }
四:在action中调用DAO层的增删改查方法进而转入struts.xml中 nuc.sw.action--->BookAction
1 package nuc.sw.action; 2 3 import java.util.List; 4 5 import com.opensymphony.xwork2.ActionContext; 6 import com.opensymphony.xwork2.ActionSupport; 7 import com.opensymphony.xwork2.ModelDriven; 8 9 import nuc.sw.dao.BookDAO; 10 import nuc.sw.vo.Book; 11 12 public class BookAction extends ActionSupport implements ModelDriven<Book>{ 13 14 //模型驱动 15 private Book b=new Book(); 16 //创建BookDAO的对象来调用增删改查书的方法。 17 BookDAO bookDAO=new BookDAO(); 18 //将bList设置为全局变量 19 List<Book> bList = bookDAO.getBook(); 20 21 //模型驱动 22 @Override 23 public Book getModel() { 24 return b; 25 } 26 27 //添加书籍方法 28 public String addBookMethod() throws Exception { 29 //通过BookDAO的对象来调用增书的方法 30 bookDAO.addBook(b); 31 return "addOK"; 32 } 33 34 //得到书的集合后存放到session中 35 public String getBookMethod() throws Exception { 36 ActionContext.getContext().getSession().put("bList", bList); 37 return "getOK"; 38 } 39 40 //查找一本书籍的放方法 41 public String findBookMethod() throws Exception { 42 Book findBook=bookDAO.findBook(b); 43 ActionContext.getContext().getSession().put("Book",findBook); 44 return "findOK"; 45 } 46 47 //修改书籍方法 48 public String updateBookMethod() throws Exception { 49 //先找到书 50 Book findBook=bookDAO.findBook(b); 51 //调用修改方法 52 bookDAO.updateBook(findBook,b); 53 //修改后仍显示集合的书籍而非修改过的一本书籍信息 54 ActionContext.getContext().getSession().put("bList",bList); 55 return "updateOK"; 56 } 57 58 //删除书籍方法 59 public String deleteBookMethod() throws Exception { 60 //找到书籍 61 Book findBook=bookDAO.findBook(b); 62 //调用删除方法 63 bookDAO.deleteBook(findBook); 64 ActionContext.getContext().getSession().put("bList", bList); 65 return "deleteOK"; 66 } 67 }
五:添加书籍的首页面--->addBook.jsp
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>添加书籍信息</title> 8 </head> 9 <body> 10 <table align="left" border="1"> 11 <tr> 12 <td><a href="addBook.jsp">添加一本书籍信息</a></td> 13 </tr> 14 <tr> 15 <td><a href="${pageContext.request.contextPath}/findBook.jsp">查询某本书籍信息</a></td> 16 </tr> 17 </table> 18 <form action="${pageContext.request.contextPath}/addBookAction" method="post"> 19 <table align="center" border="1"> 20 <tr> 21 <td>书名:</td> 22 <td><input type="text" name="bookName"> </td> 23 <tr> 24 <tr> 25 <td>作者:</td> 26 <td><input type="text" name="bookAuthor"></td> 27 <tr> 28 <tr> 29 <td>价格:</td> 30 <td><input type="text" name="bookPrice"></td> 31 <tr> 32 <tr> 33 <td><input type="submit" value="添加"></td> 34 </tr> 35 </table> 36 </form> 37 </body> 38 </html>
六:配置struts.xml,去相应的Actio中寻找对应的action class method
因为本实验是针对书籍做操作,所以建立Book.xml--->复制struuts.xml,修改名字即可
struts.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 8 <include file="Book.xml" /> 9 10 </struts>
Book.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 8 <constant name="struts.enable.DynamicMethodInvocation" value="false" /> 9 <constant name="struts.devMode" value="true" /> 10 11 <package name="default" namespace="/" extends="struts-default"> 12 13 <action name="addBookAction" class="nuc.sw.action.BookAction" method="addBookMethod"> 14 <result name="addOK" type="chain">getBookAction</result> 15 </action> 16 <action name="getBookAction" class="nuc.sw.action.BookAction" method="getBookMethod"> 17 <result name="getOK">/showBookInfo.jsp</result> 18 </action> 19 <action name="findBookAction" class="nuc.sw.action.BookAction" method="findBookMethod"> 20 <result name="findOK">/showOneBook.jsp</result> 21 </action> 22 <action name="updateBookAction" class="nuc.sw.action.BookAction" method="updateBookMethod"> 23 <result name="updateOK">/showDeleteBookNewInfo.jsp</result> 24 </action> 25 <action name="deleteBookAction" class="nuc.sw.action.BookAction" method="deleteBookMethod"> 26 <result name="deleteOK">/showDeleteBookNewInfo.jsp</result> 27 </action> 28 </package> 29 30 31 </struts>
七:显示所有书籍信息--->showBookInfo.jsp
使用普通的迭代器和EL表达式获取信息,通过占位符进行修改删除操作。
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8" import="nuc.sw.vo.Book,java.util.*"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>全部书籍信息</title> 8 </head> 9 <body> 10 <table align="left" border="1"> 11 <tr> 12 <td><a href="addBook.jsp">添加一本书籍信息</a></td> 13 </tr> 14 <tr> 15 <td><a href="${pageContext.request.contextPath}/findBook.jsp">查询某本书籍信息</a></td> 16 </tr> 17 </table> 18 <table align="center" border="1"> 19 <caption>全部书籍信息</caption> 20 <tr> 21 <td>书名:</td> 22 <td>作者:</td> 23 <td>定价:</td> 24 <td>操作1</td> 25 <td>操作2</td> 26 </tr> 27 <% 28 Iterator<Book> iter=((ArrayList<Book>)session.getAttribute("bList")).iterator(); 29 while(iter.hasNext()){ 30 pageContext.setAttribute("book",iter.next()); 31 %> 32 33 <tr> 34 <td>${book.bookName} </td> 35 <td>${book.bookAuthor} </td> 36 <td>${book.bookPrice} </td> 37 <td><a href="updateBook.jsp?bookName=${book.bookName}&bookAuthor=${book.bookAuthor}&bookPrice=${book.bookPrice}">修改</a></td> 38 <td><a href="deleteBook.jsp?bookName=${book.bookName}">删除</a></td> 39 </tr> 40 <% 41 } 42 %> 43 </table> 44 </body> 45 </html>
 
以上是关于struts2-10-属性驱动实现书籍的增删改查的主要内容,如果未能解决你的问题,请参考以下文章
JAVA面向对象 要求做一个小型的图书管理系统 要有book Order OrderItem三个类 实现购买图书的增删改查