一:集合模拟数据库 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>
八:查找书籍--->findBook.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>Insert title here</title> 8 </head> 9 <body> 10 <form action="${pageContext.request.contextPath}/findBookAction" method="post"> 11 <table align="center" border="1"> 12 <caption>查找书籍信息</caption> 13 <tr> 14 <td>书名</td> 15 <td><input type="text" name="bookName"></td> 16 </tr> 17 <tr> 18 <td><input type="submit" value="查找"></td> 19 </tr> 20 </table> 21 </form> 22 </body> 23 </html>
九:显示一本书籍信息--->showOneBook.jsp
用Book获取即可
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="nuc.sw.vo.Book,java.util.*"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>结果书籍信息</title> </head> <body> <table align="left" border="1"> <tr> <td><a href="addBook.jsp">添加一本书籍信息</a></td> </tr> <tr> <td><a href="${pageContext.request.contextPath}/findBook.jsp">查询某本书籍信息</a></td> </tr> </table> <table align="center" border="1"> <caption>结果书籍信息</caption> <tr> <td>书名:</td> <td>作者:</td> <td>定价:</td> </tr> <% Book book=(Book)session.getAttribute("Book"); if(book != null){ pageContext.setAttribute("book",book); %> <tr> <td>${book.bookName} </td> <td>${book.bookAuthor} </td> <td>${book.bookPrice} </td> </tr> <% } %> </table> </body> </html>
十:修改书籍信息--->updateBook.jsp
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 <% 19 String bookName=request.getParameter("bookName"); 20 bookName=new String(bookName.getBytes("ISO-8859-1"),"utf-8"); 21 String bookAuthor=request.getParameter("bookAuthor"); 22 bookAuthor=new String(bookAuthor.getBytes("ISO-8859-1"),"utf-8"); 23 String bookPrice=request.getParameter("bookPrice"); 24 bookPrice=new String(bookPrice.getBytes("ISO-8859-1"),"utf-8"); 25 %> 26 <form action="${pageContext.request.contextPath}/updateBookAction" method="post"> 27 <table align="center" border="1"> 28 <caption>修改书籍信息</caption> 29 <tr> 30 <td>书名:</td> 31 <td><input type="text" name="bookName" value="<%=bookName%>"> </td> 32 <tr> 33 <tr> 34 <td>作者:</td> 35 <td><input type="text" name="bookAuthor" value="<%=bookAuthor%>"></td> 36 <tr> 37 <tr> 38 <td>价格:</td> 39 <td><input type="text" name="bookPrice" value="<%=bookPrice%>"></td> 40 <tr> 41 <tr> 42 <td><input type="submit" value="修改书籍信息"></td> 43 </tr> 44 45 </table> 46 </form> 47 </body> 48 </html>
十一:修改成功后仍需显示所有书籍信息--->showDeleteBookNewInfo.jsp
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=UTF-8"> 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 <br> 45 46 </body> 47 </html>
十二:删除书籍页面--->deletBook.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=UTF-8"> 7 <title>删除书籍信息</title> 8 <script type="text/javascript"> 9 function load(){ 10 document.all.form1.submit(); 11 } 12 </script> 13 </head> 14 <body onload="load()"> 15 16 <% 17 String bookName=request.getParameter("bookName"); 18 bookName=new String(bookName.getBytes("ISO-8859-1"),"utf-8"); 19 %> 20 <form name="form1" 21 action="${pageContext.request.contextPath}/deleteBookAction" 22 method="post"> 23 <input type="text" name="bookName" value="<%=bookName%>"/> 24 <input type="submit" value="删除"/> 25 </form> 26 </body> 27 </html>
用户手动删除:
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=UTF-8"> 7 <title>删除书籍信息</title> 8 </head> 9 <body> 10 <% 11 String bookName=request.getParameter("bookName"); 12 bookName=new String(bookName.getBytes("ISO-8859-1"),"utf-8"); 13 %> 14 <form action="deleteBookAction" method="post"> 15 <input type="text" name="bookName" value="<%=bookName%>"/> 16 <input type="submit" value="删除"/> 17 </form> 18 </body> 19 </html>
十二:项目结构
十三:执行结果
增加书籍:
显示所有书籍信息:
修改数据结构书的价格:
修改成功:
删除struts书:
查找数据结构书:
十四:eclipse一直404错误,一定是strut.xml配置错误的问题;
删除第二本书时空指针出现:<head><body>嵌套错误