mvc增删改查
1、通用分页的jar、自定义mvc框架、自定义标签
导入jar、导入之前写好的pageTag、自定义mvc.xml
2、dao层 通用的增删改方法
在以前的BaseDao里面写dao层 通用的增删改方法
/**
* 通用的增删改方法
* @param sql 决定增删改的一种
* @param attrs 决定位置 new String []{"bid","bname","price"}
* @param t 要操作的实体类
* @return
* @throws SQLException
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public int executeUpdate(String sql,String[] attrs,T t) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
Connection con = DBAccess.getConnection();
PreparedStatement pst = con.prepareStatement(sql);
// pst.setString(1,book.getBname());
// pst.setFloat(2, book.getPrice());
// pst.setInt(3,book.getBid());
for (int i = 1; i <=attrs.length; i++) {
// 属性值获取
Field f = t.getClass().getDeclaredField(attrs[i-1]);
f.setAccessible(true);
pst.setObject(i, f.get(t));
}
int num=pst.executeUpdate();
DBAccess.close(con, pst, null);
return num;
}
3、web层
在web层里面建一个BookAction并且实现ModelDriwern模型驱动接口
作用是将sjp所有传递过来的参数以及参数值都自动封装到浏览器所要操作的实体类中
package com.chenkang.web;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.chenkang.dao.BookDao;
import com.chenkang.entity.Book;
import com.chenkang.framework.ActionSupport;
import com.chenkang.framework.ModelDriwern;
import com.chenkang.util.PageBean;
public class BookAction extends ActionSupport implements ModelDriwern<Book>{
private Book book=new Book();
private BookDao bookDao= new BookDao();
/**
* 分页查询
* @param req
* @param resp
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public String list(HttpServletRequest req,HttpServletResponse resp) throws InstantiationException, IllegalAccessException, SQLException {
PageBean pageBean=new PageBean();
pageBean.setRequest(req);
List<Book> list = this.bookDao.list(book, pageBean);
req.setAttribute("bookList", list);
req.setAttribute("pageBean", pageBean);
return "list";
}
/**
* 跳转到增加或者修改界面
* @param req
* @param resp
* @return
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public String preSave(HttpServletRequest req,HttpServletResponse resp) throws InstantiationException, IllegalAccessException, SQLException {
// bid的类型是int类型,而int类型的默认值是0,如果未传递bid的值那么bid=0就是跳新增页面
if(book.getBid()==0) {
System.out.println("增加逻辑......");
}else {
// 修改数据回显逻辑
// !=0就查询id==?的一条数据并且便利出来就是查询单个
Book b = this.bookDao.list(book, null).get(0);
req.setAttribute("book", b);
}
return "edit";
}
/**
* 新增
* @param req
* @param resp
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
*/
public String add(HttpServletRequest req,HttpServletResponse resp) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
this.bookDao.add(book);
// 新增完了之后需要刷新页面
return "toList";
}
/**
* 修改
* @param req
* @param resp
* @return
*/
public String edit(HttpServletRequest req,HttpServletResponse resp) {
try {
this.bookDao.edit(book);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
| SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
/***
* 删除
* @param req
* @param resp
* @return
*/
public String del(HttpServletRequest req,HttpServletResponse resp) {
try {
this.bookDao.del(book);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
| SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
@Override
public Book getModel() {
// TODO Auto-generated method stub
return book;
}
}
mvc.xml进行配置
<action path="/book" type="com.chenkang.web.BookAction">
<forward name="list" path="/bookList.jsp" redirect="false" />
<forward name="edit" path="/bookEdit.jsp" redirect="false" />
<forward name="toList" path="/book.action?methodName=list" />
</action>
web.xml进行配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>mvc</display-name>
<filter>
<filter-name>encodingFiter</filter-name>
<filter-class>com.chenkang.util.EncodingFiter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingFiter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>dispatcherSerlvet</servlet-name>
<servlet-class>com.chenkang.framework.DispatcherSerlvet</servlet-class>
<init-param>
<param-name>xmlPath</param-name>
<param-value>/mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherSerlvet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
4、jsp页面
bookList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="/zking" prefix="z" %>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>小说目录</h2>
<br>
<form action="${pageContext.request.contextPath}/book.action?methodName=list" method="post">
<input type="hidden" name="pagination" value="true">
书名:<input type="text" name="bname"> <input type="submit"
value="确定">
</form>
<a href="${pageContext.request.contextPath}/book.action?methodName=preSave">增加</a>
<table border="1" width="100%">
<tr>
<td>编号</td>
<td>名称</td>
<td>价格</td>
<td>操作</td>
</tr>
<c:forEach items="${bookList }" var="b">
<tr>
<td>${b.bid }</td>
<td>${b.bname }</td>
<td>${b.price }</td>
<td>
<a href="${pageContext.request.contextPath}/book.action?methodName=preSave&&bid=${b.bid}">修改</a>
<a href="${pageContext.request.contextPath}/book.action?methodName=del&&bid=${b.bid}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<!-- 直接调用page标签就好了 -->
<z:page pageBean="${ pageBean}"></z:page>
</body>
</html>
bookEdit.jsp
修改和增加是同一个页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/book.action" method="post">
<input type="hidden" name="methodName" value="${book.bname==null ? 'add' : 'edit'}">
书籍id:<input type="text" name="bid" value="${book.bid }"><br>
书籍名称:<input type="text" name="bname" value="${book.bname }"><br>
书籍价格:<input type="text" name="price" value="${book.price }"><br>
<input type="submit">
</form>
</body>
</html>