struts2-11-OGNL实现书籍的增删改查

Posted ~花开不败~

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了struts2-11-OGNL实现书籍的增删改查相关的知识,希望对你有一定的参考价值。

一:集合模拟数据库 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 import com.opensymphony.xwork2.util.ValueStack;
 9 
10 import nuc.sw.dao.BookDAO;
11 import nuc.sw.vo.Book;
12 
13 public class BookAction extends ActionSupport implements ModelDriven<Book>{
14     
15     //模型驱动
16     private Book b=new Book();
17     //创建BookDAO的对象来调用增删改查书的方法。
18     BookDAO bookDAO=new BookDAO();
19     //将bList设置为全局变量
20     List<Book> bList = bookDAO.getBook();
21     
22     //值栈来存放;
23     ValueStack vs=ActionContext.getContext().getValueStack();
24     //模型驱动
25     @Override
26     public Book getModel() {
27         return b;
28     }
29     
30     //添加书籍方法
31     public String addBookMethod() throws Exception {
32         //通过BookDAO的对象来调用增书的方法
33         bookDAO.addBook(b);
34         return "addOK";
35   }
36     
37     //得到书的集合后存放到vs中
38     public String getBookMethod() throws Exception {
39         vs.set("books", bList);
40         return "getOK";
41     }
42     
43     //查找一本书籍的方法
44     public String findBookMethod() throws Exception {
45         Book findBook=bookDAO.findBook(b);
46         ActionContext.getContext().getSession().put("Book",findBook);
47         return "findOK";
48     }
49     
50     //修改书籍方法
51     public String updateBookMethod() throws Exception {
52         //先找到书
53         Book findBook=bookDAO.findBook(b);
54         //调用修改方法
55         bookDAO.updateBook(findBook,b);
56         //修改后仍显示集合的书籍而非修改过的一本书籍信息
57         ActionContext.getContext().getSession().put("Book",bList);
58         return "updateOK";
59     }
60     
61     //删除书籍方法
62     public String deleteBookMethod() throws Exception {
63         //找到书籍
64         Book findBook=bookDAO.findBook(b);
65         bookDAO.deleteBook(findBook);
66         //vs.set("Book","bList");  这样操作会显示删除所有,但实际只删除了一个
67         ActionContext.getContext().getSession().put("Book", bList);
68         return "deleteOK";
69     }
70 }

 

五:添加书籍的首页面--->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

      使用OGNL表达式获取信息,通过占位符进行修改删除操作。

 

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8" import="nuc.sw.vo.Book,java.util.*"%>
 3 <%@ taglib prefix="s" uri="/struts-tags"%>  
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 8 <title>全部书籍信息</title>
 9 </head>
10 <body>
11 <table align="left" border="1"> 
12   <tr>
13     <td><a href="addBook.jsp">添加一本书籍信息</a></td>
14   </tr>
15   <tr>
16     <td><a href="${pageContext.request.contextPath}/findBook.jsp">查询某本书籍信息</a></td>
17   </tr>
18 </table>
19   <table align="center" border="1">
20          <caption>全部书籍信息</caption>
21       <tr>
22           <td>书名:</td>
23           <td>作者:</td>
24          <td>定价:</td>
25          <td>操作1</td>
26          <td>操作2</td>
27        </tr>
28      <s:iterator value="books" var="b">
29       <tr>
30         <td><s:property value="#b.bookName" /></td>
31         <td><s:property value="#b.bookAuthor" /></td>
32         <td><s:property value="#b.bookPrice" /></td>
33         <td><a href="${pageContext.request.contextPath}/updateBook.jsp?
34                 bookName=<s:property value="#b.bookName" />
35                 &bookAuthor=<s:property value="#b.bookAuthor" />
36                 &bookPrice=<s:property value="#b.bookPrice" />">修改</a></td>
37         <td><a href="${pageContext.request.contextPath}/deleteBook.jsp?bookName=<s:property value="#b.bookName"/>">删除</a></td>
38       </tr>
39     </s:iterator>
40   </table>
41   <br>
42  
43   <s:debug/>
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存入session中,通过OGNL获取即可。

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8" import="nuc.sw.vo.Book,java.util.*"%>
 3 <%@ taglib prefix="s" uri="/struts-tags"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 8 <title>结果书籍信息</title>
 9 </head>
10 <body>
11 <table align="left" border="1"> 
12   <tr>
13     <td><a href="addBook.jsp">添加一本书籍信息</a></td>
14   </tr>
15   <tr>
16     <td><a href="${pageContext.request.contextPath}/findBook.jsp">查询某本书籍信息</a></td>
17   </tr>
18 </table>
19  <table align="center" border="1">
20          <caption>结果书籍信息</caption>
21  <tr>
22           <td>书名:</td>
23           <td>作者:</td>
24          <td>定价:</td>
25   </tr>
26     <tr>
27       <td><s:property value="#session.Book.bookName"/></td>
28       <td><s:property value="#session.Book.bookAuthor"/></td>
29       <td><s:property value="#session.Book.bookPrice"/></td>
30     </tr>    
31 </table>    
32 </body>
33 </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

      OGNL获取集合中的数据

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib prefix="s" uri="/struts-tags"%>  
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>书籍信息</title>
 9 </head>
10 <body>
11 <table align="left" border="1"> 
12   <tr>
13     <td><a href="addBook.jsp">添加一本书籍信息</a></td>
14   </tr>
15   <tr>
16     <td><a href="${pageContext.request.contextPath}/findBook.jsp">查询某本书籍信息</a></td>
17   </tr>
18 </table>
19   <table align="center" border="1">
20          <caption>书籍信息</caption>
21       <tr>
22           <td>书名:</td>
23           <td>作者:</td>
24          <td>定价:</td>
25          <td>操作1ModelForm(书籍的增删改)

61 书籍和出版社 的增删改查 几秒后跳转一个页面

SSM框架整合---完成对图书数据表的增删改查

JAVA面向对象 要求做一个小型的图书管理系统 要有book Order OrderItem三个类 实现购买图书的增删改查

基于Form组件实现的增删改和基于ModelForm实现的增删改

vue-实现对数组的增删改查