一.MVC(增刪改查)
導入:
1.1、通用分頁的jar、自定義mvc框架、自定義標簽 導入之前寫好的pageTag、自定義mvc.xml
導入Jar
通用的增刪改方法
BaseDao
1 /** 2 * 3 * @param sql 決定增刪改的一種 4 * @param attrs 決定?位置 new String[]{"bid,"bname"} 5 * @param t 要操作的實體 6 * @return 7 * @throws Exception 8 * @throws NoSuchFieldException 9 */ 10 public int executeUpdate (String sql,String[] attrs,T t) throws NoSuchFieldException, Exception { 11 Connection con=DBAccess.getConnection(); 12 PreparedStatement ps=con.prepareStatement(sql); 13 for(int i=1;i<=attrs.length;i++) { 14 Field f=t.getClass().getDeclaredField(attrs[i-1]); 15 f.setAccessible(true); 16 ps.setObject(i, f.get(t)); 17 } 18 int num=ps.executeUpdate(); 19 DBAccess.close(con, ps, null); 20 return num; 21 } 22
dao方法
BookDao
1 public class BookDao extends BaseDao<Book>{ 2 /** 3 * 查詢所有 4 * @param book 5 * @param pageBean 6 * @return 7 * @throws Exception 8 */ 9 public List<Book> list(Book book,PageBean pageBean) throws Exception{ 10 String sql="select * from t_mvc_book where true"; 11 String bname=book.getBname(); 12 int bid=book.getBid(); 13 if(StringUtils.isNotBlank(bname)) { 14 sql+=" and bname like '%"+bname+"%'"; 15 } 16 if(bid!=0) { 17 sql+=" and bid="+bid; 18 } 19 return super.executeQuery(sql, Book.class, pageBean); 20 21 } 22 /** 23 * 修改 24 * @param book 25 * @return 26 * @throws Exception 27 * @throws NoSuchFieldException 28 */ 29 public int upde(Book book) throws NoSuchFieldException, Exception { 30 String sql="update t_mvc_book set bname=?,price=? where bid=?"; 31 return super.executeUpdate(sql, new String[] {"bname","price","bid"}, book); 32 } 33 34 /** 35 * 新增 36 * @param book 37 * @return 38 * @throws Exception 39 * @throws NoSuchFieldException 40 */ 41 public int add(Book book) throws NoSuchFieldException, Exception { 42 String sql="insert into t_mvc_book values(?,?,?)"; 43 System.out.println(sql); 44 return super.executeUpdate(sql, new String[] {"bid","bname","price"}, book); 45 } 46 47 /** 48 * 刪除 49 * @param book 50 * @return 51 * @throws Exception 52 * @throws NoSuchFieldException 53 */ 54 public int del(Book book) throws NoSuchFieldException, Exception { 55 String sql="delete from t_mvc_book where bid=?"; 56 57 return super.executeUpdate(sql, new String[] {"bid"}, book); 58 } 59 }
Book實體類
1 package mvc.entity; 2 3 public class Book { 4 private int bid; 5 private String bname; 6 private float price; 7 8 @Override 9 public String toString() { 10 return "Book [bid=" + bid + ", bname=" + bname + ", price=" + price + "]"; 11 } 12 13 public int getBid() { 14 return bid; 15 } 16 17 public void setBid(int bid) { 18 this.bid = bid; 19 } 20 21 public String getBname() { 22 return bname; 23 } 24 25 public void setBname(String bname) { 26 this.bname = bname; 27 } 28 29 public float getPrice() { 30 return price; 31 } 32 33 public void setPrice(float price) { 34 this.price = price; 35 } 36 37 public Book(int bid, String bname, float price) { 38 super(); 39 this.bid = bid; 40 this.bname = bname; 41 this.price = price; 42 } 43 44 public Book() { 45 super(); 46 } 47 48 49 }
BookAction調用通用分頁,自定義mvc框架,自定義標簽
1 package com.mvc.web; 2 3 import java.util.List; 4 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 import com.mvc.dao.BookDao; 10 import com.mvc.entity.Book; 11 import com.mvc.framework.ActionSupport; 12 import com.mvc.framework.ModelDrivern; 13 import com.mvc.util.PageBean; 14 15 public class BookAction extends ActionSupport implements ModelDrivern<Book>{ 16 private Book book=new Book(); 17 private BookDao bookdao=new BookDao(); 18 /** 19 * 分頁查詢 20 * @param request 21 * @param response 22 * @return 23 * @throws Exception 24 */ 25 26 public String list(HttpServletRequest request,HttpServletResponse response) throws Exception { 27 PageBean pageBean=new PageBean(); 28 pageBean.setRequest(request); 29 List<Book> list = this.bookdao.list(book, pageBean); 30 request.setAttribute("booklist", list); 31 request.setAttribute("pagebean", pageBean); 32 return "list"; 33 } 34 35 /** 36 * 37 * 跳轉到增加或修改頁面 38 * @param request 39 * @param response 40 * @return 41 * @throws Exception 42 */ 43 public String preSave(HttpServletRequest request,HttpServletResponse response) { 44 //bid的類型是int類型,而int類型的默認值是0,如果jsp未傳遞bid的參數值那么bid=0; 45 if(book.getBid()==0) { 46 System.out.println("增加邏輯"); 47 48 } 49 else { 50 //修改數據回顯 51 Book b; 52 try { 53 b = this.bookdao.list(book, null).get(0); 54 request.setAttribute("book", b); 55 } catch (Exception e) { 56 // TODO Auto-generated catch block 57 e.printStackTrace(); 58 } 59 60 } 61 return "edit"; 62 63 64 } 65 66 /** 67 * 新增 68 * @param request 69 * @param response 70 * @return 71 * @throws Exception 72 */ 73 public String add(HttpServletRequest request,HttpServletResponse response) throws Exception { 74 //新增完了之后刷新頁面 75 this.bookdao.add(book); 76 return "tolist"; 77 78 } 79 80 /** 81 * 修改 82 * @param request 83 * @param response 84 * @return 85 * @throws Exception 86 */ 87 public String upde(HttpServletRequest request,HttpServletResponse response) throws Exception { 88 //修改完了之后刷新頁面 89 this.bookdao.upde(book); 90 return "tolist"; 91 92 } 93 /** 94 * 刪除 95 * @param request 96 * @param response 97 * @return 98 * @throws Exception 99 */ 100 public String del(HttpServletRequest request,HttpServletResponse response) throws Exception { 101 //刪除之后刷新頁面 102 this.bookdao.del(book); 103 return "tolist"; 104 } 105 106 @Override 107 public Book getModel() { 108 // TODO Auto-generated method stub 109 return book; 110 } 111 112 }
配置mvc.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- 3 config標簽:可以包含0~N個action標簽 4 --> 5 <config> 6 <!-- <action path="/addCal" type="com.mvc.web.AddCalAction">--> 7 <!--<forward name="res" path="/res.jsp" redirect="false" />--> 8 <!--</action>--> 9 10 <!--<action path="/delCal" type="com.mvc.web.DelCalAction">--> 11 <!-- <forward name="res" path="/res.jsp" redirect="false" />--> 12 <!--</action>--> 13 14 <!--<action path="/mulCal" type="com.mvc.web.MulCalAction">--> 15 <!-- <forward name="res" path="/res.jsp" redirect="false" />--> 16 <!--</action>--> 17 18 <!--<action path="/divCal" type="com.mvc.web.DivCalAction">--> 19 <!-- <forward name="res" path="/res.jsp" redirect="false" />--> 20 <!--</action>--> 21 22 <action path="/cal" type="com.mvc.web.CalAction"> 23 <forward name="res" path="/res.jsp" redirect="false" /> 24 </action> 25 26 <action path="/book" type="com.mvc.web.BookAction"> 27 <forward name="list" path="/bookList.jsp" redirect="false" /> 28 <forward name="edit" path="/bookEdit.jsp" redirect="false" /> 29 <forward name="toList" path="/book.action?methodName=list"></forward> 30 </action> 31 32 33 </config>
頁面代碼
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 4 <%@ taglib uri="/XuFanQi" prefix="x" %> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <title>Insert title here</title> 10 </head> 11 <body> 12 <h2>小說目錄</h2> 13 <br> 14 15 <form action="${pageContext.request.contextPath}/book.action?methodName=list" 16 method="post"> 17 書名:<input type="text" name="bname"> <input type="submit" 18 value="確定"> 19 <input type="hidden" name="bname"> 20 </form> 21 <a href="${pageContext.request.contextPath}/book.action?methodName=preSave">增加</a> 22 <table border="1" width="100%"> 23 <tr> 24 <td>編號</td> 25 <td>名稱</td> 26 <td>價格</td> 27 <del>操作</del> 28 </tr> 29 <c:forEach items="${bookList }" var="b"> 30 <tr> 31 <td>${b.bid }</td> 32 <td>${b.bname }</td> 33 <td>${b.price }</td> 34 <td> 35 <a href="${pageContext.request.contextPath}/book.action?methodName=preSave&&bid=${b.bid}">修改</a> 36 <a href="${pageContext.request.contextPath}/book.action?methodName=dele&&bid=${b.bid}">刪除</a> 37 38 39 </td> 40 </tr> 41 </c:forEach> 42 </table> 43 44 <x:Page pageBean="${pageBean }"></x:Page> 45 </body> 46 </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>Insert title here</title> 8 </head> 9 <body> 10 <form action="${pageContext.request.contextPath}/book.action" method="post"> 11 <input type="hidden" name="methodName" value="${book.bname == null?'add':'edit' }"> 12 書籍ID:<input type="text" name="bid" value="${book.bid }"><br> 13 書籍名稱:<input type="text" name="bname" value="${book.bname }"><br> 14 書籍價格:<input type="text" name="price" value="${book.price }"><br> 15 <input type="submit"> 16 </form> 17 </body> 18 </html>
運行效果
查詢
新增
修改
刪除