一:使用集合模拟数据库--->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 public static List<Book> bookList=new ArrayList<Book>(); 10 }
二:封装对象(书)--->nuc.sw.vo--->Book.java
package nuc.sw.vo; public class Book { private String bookName; private String bookAuthor; private float bookPrice; public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public String getBookAuthor() { return bookAuthor; } public void setBookAuthor(String bookAuthor) { this.bookAuthor = bookAuthor; } public float getBookPrice() { return bookPrice; } public void setBookPrice(float bookPrice) { this.bookPrice = bookPrice; } }
三:借助DAO层往数据库(集合)添加书籍并且获取集合
package nuc.sw.dao; import java.util.List; import nuc.sw.db.BookDB; import nuc.sw.vo.Book; public class BookDAO { //添加书籍方法 public void addBook(Book book){ BookDB.bookList.add(book); } //获取书籍方法 public List<Book> getBook(){ return BookDB.bookList; } }
四:相应的action中调用DAO中的方法--->nuc.sw.action--->BookAction.java
package nuc.sw.action; import java.util.List; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import nuc.sw.dao.BookDAO; import nuc.sw.vo.Book; public class BookAction extends ActionSupport implements ModelDriven<Book>{ Book b=new Book(); //后期用到添加书和获取书的方法。所以要创建对象 BookDAO bookDAO=new BookDAO(); //模型驱动 @Override public Book getModel() { // TODO Auto-generated method stub return b; } public String addBookMethod() throws Exception { // TODO Auto-generated method stub bookDAO.addBook(b); return "addOK"; } public String getBookMethod() throws Exception { // TODO Auto-generated method stub List<Book> bList=bookDAO.getBook(); ActionContext.getContext().getSession().put("bList", bList); return "getOK"; } }
五:View中写添加书籍和获取书籍的jsp页面--->addBook.jsp--->showBook.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="addBookAction" method="post">
//name和Book.java中一样
11 书名:<input type="text" name="bookName"> <br> 12 作者:<input type="text" name="bookAuthor"><br> 13 价格:<input type="text" name="bookPrice"><br> 14 <input type="submit" value="添加"> 15 </form> 16 </body> 17 </html>
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>Insert title here</title> 8 </head> 9 <body> 10 <table border="1"> 11 <caption>全部书籍信息</caption> 12 <tr> 13 <td>书名:</td> 14 <td>作者:</td> 15 <td>定价:</td> 16 </tr> 17 <% 18 Iterator<Book> iter=((ArrayList<Book>)session.getAttribute("bList")).iterator(); 19 while(iter.hasNext()){ 20 pageContext.setAttribute("book",iter.next()); 21 %> 22 23 <tr> 24 <td>${book.bookName} </td> 25 <td>${book.bookAuthor} </td> 26 <td>${book.bookPrice} </td> 27 </tr> 28 <% 29 } 30 %> 31 32 </table> 33 </body> 34 </html>
六:配置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 <constant name="struts.devMode" value="true" /> 8 <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> 9 <package name="default" namespace="/" extends="struts-default"> 10 <action name="LoginReg" method="LoginMethod" class="nuc.sw.action.LoginRegAction"> 11 <result name="success">/welcome.jsp</result> 12 <result name="error">/login.jsp</result> 13 <result name="input">/login.jsp</result> 14 </action> 15 <action name="RegisterReg" method="RegisterMethod" class="nuc.sw.action.LoginRegAction"> 16 <result name="success">/register.jsp</result> 17 <result name="error">/login.jsp</result> 18 <result name="input">/login.jsp</result> 19 </action> 20 <action name="addBookAction" class="nuc.sw.action.BookAction" method="addBookMethod">
//结果类型使用chain 可以实现请求转发 从action--->action
21 <result name="addOK" type="chain">getBookAction</result> 22 </action> 23 <action name="getBookAction" class="nuc.sw.action.BookAction" method="getBookMethod"> 24 <result name="getOK">/showBook.jsp</result> 25 </action> 26 </package> 27 <!-- Add packages here --> 28 </struts>
七:项目结构
八:运行结果
添加书籍:
显示书籍: