mvc(三)
目标:
完成mvc对t_mvc_book表的增删改查
1、通用分页、自定义mvc框架、自定义标签
导入jar、导入之前写好的pageTag、自定义mvc.xml
2、dao层 通用的增删改方法
3、web层
mvc.xml进行配置
4、jsp
1、导入之前的通用分页、帮助类、自定义mvc.xml。
2、通用分页我们已经写过通用的查询方法,现在我们只要再写一个通用的增删改方法
1、通用的增删改方法定义
BookDao类
package com.DZY.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import com.DZY.entity.Book;
import com.DZY.util.BaseDao;
import com.DZY.util.DBAccess;
import com.DZY.util.PageBean;
import com.DZY.util.StringUtils;
public class BookDao extends BaseDao<Book> {
public List<Book> list(Book book,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql="select * from t_mvc_book where true";
String bname=book.getBname();
int bid=book.getBid();
if(StringUtils.isNotBlank(bname)) {
sql+="and bname like '%"+bname+"%'";
}
if(bid !=0) {
sql+="and bid="+bid;
}
return super.executeQuery(sql, Book.class, pageBean);
}
/**
* 修改
* @param book
* @return
* @throws SQLException
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public int edit (Book book) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
String sql="update t_mvc_book set bname=? price=? where bid=?";
return super.executeUpdate(sql, new String[] {"bname","price","bid"}, book);
}
/**
* 新增
* @param book
* @return
* @throws SQLException
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public int add (Book book) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
String sql="insert into t_mvc_book values(?,?,?)";
return super.executeUpdate(sql, new String[] {"bid","bname","price"}, book);
}
/**
* 删除
* @param book
* @return
* @throws SQLException
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public int del (Book book) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
String sql="delete from t_mvc_book where bid=?";
return super.executeUpdate(sql, new String[] {"bid"}, book);
}
}
增加和修改方法除了sql语句不同,就只有pst在设置属性值的位置不一样,参照上面所展示,增加方法ps设置值的顺序是 bid–>bname–>price 而修改是 bname–>price–>bid 而我们只要让方法的设置值位置可变,就变成了一个通用的增删改方法,所以我们给一个String数组,而它的设置顺序我们可以在basedao里面去传入,然后在通用的方法里面遍历这个String数组,利用反射读写属性的原理来实现,因为数组的下标从0开始.
然后再进行编写web层来实现对应的方法:
package com.DZY.web;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.DZY.dao.BookDao;
import com.DZY.entity.Book;
import com.DZY.framework.ActionSupport;
import com.DZY.framework.ModelDrevern;
import com.DZY.util.PageBean;
public class BookAction extends ActionSupport implements ModelDrevern<Book>{
private Book book = new Book();
private BookDao bookDao = new BookDao();
/**
* 分页查询
* @param req
* @param resp
* @return
*/
public String list(HttpServletRequest req,HttpServletResponse resp) {
PageBean pageBean = new PageBean();
pageBean.setRequest(req);
try {
List<Book> list = this.bookDao.list(book, pageBean);
req.setAttribute("bookList", list);
req.setAttribute("pageBean", pageBean);
} catch (InstantiationException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "list";
}
/**
* 跳转到增加或者修改页面
* 新增页面与修改页面是同一个jsp页面
* @param req
* @param resp
* @return
*/
public String preSave(HttpServletRequest req,HttpServletResponse resp) {
if(book.getBid() == 0) {
System.out.println("增加逻辑");
}else {
try {
// 修改数据回显逻辑
Book b = this.bookDao.list(book, null).get(0);
req.setAttribute("book", b);
} catch (InstantiationException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "edit";
}
/**
* 新增
* @param req
* @param resp
* @return
*/
public String add(HttpServletRequest req,HttpServletResponse resp) {
try {
this.bookDao.add(book);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
| SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 新增完了要刷新页面
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;
}
}
由于我们在进行增加和修改的时候需要跳转到同一个页面,所以我们进行判断:
如果前段有传入过来一个id的话,那么进行修改的操作,如果没有,就进行增加的操作;
再把mvc.xml配置好:
<?xml version="1.0" encoding="UTF-8"?>
<!--
config标签:可以包含0~N个action标签
-->
<config>
<action path="/cal" type="com.DZY.web.CalAction">
<forward name="res" path="/res.jsp" redirect="false" />
</action>
<action path="/book" type="com.DZY.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>
</config>
最后编写前段jsp页面:
bookList.jsp:
<h2>小说目录</h2>
<br>
<form action="${pageContext.request.contextPath}/book.action?methodName=list"
method="post">
书名:<input type="text" name="bname"> <input type="submit"
value="确定">
<!--
<input type="hidden" name="rows" value="10">
<input type="hidden" name="page" value="2">
<input type="hidden" name="pagination" value="false">-->
</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:if test="${empty bookList}">
<jsp:forward page="${pageContext.request.contextPath}/book.action?methodName=list"></jsp:forward>
</c:if>
<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>
<z:page pageBean="${pageBean}"></z:page>
boolEdit:修改页面
<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" value="提交">
</form>