2.优化增删改查Servlet代码
创建一个Servlet继承ActionSupport并实现ModelDriver<需要操作的实体>
package com.zking.web; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.zking.dao.BookDao; import com.zking.entity.Book; import com.zking.framework.ActionSupport; import com.zking.framework.ModelDriver; import com.zking.util.PageBean; /** * @author Java方文山 * */ public class BookAction extends ActionSupport implements ModelDriver<Book>{ private Book book = new Book(); private BookDao bookDao = new BookDao(); @Override public Book getModel() { return book; } public String add(HttpServletRequest req, HttpServletResponse resp) { try { bookDao.add(book); } catch (Exception e) { e.printStackTrace(); } return "toList"; } public String list(HttpServletRequest req, HttpServletResponse resp) { try { PageBean pageBean = new PageBean(); pageBean.setPagination(true); pageBean.setRequest(req); List<Book> list = bookDao.list(book,pageBean); req.setAttribute("books", list); req.setAttribute("pageBean", pageBean); } catch (Exception e) { e.printStackTrace(); } return "list"; } public String delete(HttpServletRequest req, HttpServletResponse resp) { try { bookDao.delete(book); } catch (Exception e) { e.printStackTrace(); } return "toList"; } public String edit(HttpServletRequest req, HttpServletResponse resp) { try { bookDao.edit(book); } catch (Exception e) { e.printStackTrace(); } return "toList"; } /** * 跳转到新增修改页面 * @param req * @param resp * @return */ public String toEdit(HttpServletRequest req, HttpServletResponse resp) { try { /* * 如果是跳转修改页面,那么需要做bid条件的精准查询 */ if(book.getBid() != 0) { List<Book> list = bookDao.list(book, null); req.setAttribute("b", list.get(0)); } } catch (Exception e) { e.printStackTrace(); } return "toEdit"; } }
因为前面也说到了,我们有三种跳转情况,所以我们return的值也有三种(注意配置好xml文件)
五、案例实操
1.将PageTag自定义标签进行配置
<?xml version="1.0" encoding="UTF-8" ?> <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0"> <description>zking 1.1 core library</description> <display-name>zking core</display-name> <tlib-version>1.1</tlib-version> <short-name>zking</short-name> <uri>http://jsp.veryedu.cn</uri> <tag> <name>page</name> <tag-class>com.zking.tag.PageTag</tag-class> <body-content>JSP</body-content> <attribute> <name>pageBean</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
放入web-inf目录下
2.jsp页面环境搭建
首页bookList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://jsp.veryedu.cn" prefix="z"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!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"> <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.css" rel="stylesheet"> <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.js"></script> <title>书籍列表</title> <style type="text/css"> .page-item input { padding: 0; width: 40px; height: 100%; text-align: center; margin: 0 6px; } .page-item input, .page-item b { line-height: 38px; float: left; font-weight: 400; } .page-item.go-input { margin: 0 10px; } </style> </head> <body> <c:if test="${empty pageBean}"> <jsp:forward page="${pageContext.request.contextPath }/book.action?methodName=list"></jsp:forward> </c:if> <form class="form-inline" action="${pageContext.request.contextPath }/book.action?methodName=list" method="post"> <div class="form-group mb-2"> <input type="text" class="form-control-plaintext" name="bname" placeholder="请输入书籍名称"> <!-- <input name="rows" value="20" type="hidden"> --> <!-- 不想分页 --> <input name="pagination" value="true" type="hidden"> </div> <button type="submit" class="btn btn-primary mb-2">查询</button> <a class="btn btn-primary mb-2" href="${pageContext.request.contextPath }/book.action?methodName=toEdit">新增</a> </form> <table class="table table-striped "> <thead> <tr> <th scope="col">书籍ID</th> <th scope="col">书籍名</th> <th scope="col">价格</th> <th scope="col">操作</th> </tr> </thead> <tbody> <c:forEach var="b" items="${books }"> <tr> <td>${b.bid }</td> <td>${b.bname }</td> <td>${b.price }</td> <td><a href="${pageContext.request.contextPath }/book.action?methodName=toEdit&bid=${b.bid}">修改</a> <a href="${pageContext.request.contextPath }/book.action?methodName=delete&bid=${b.bid}">删除</a> </td> </tr> </c:forEach> </tbody> </table> <z:page pageBean="${pageBean }"></z:page> </body> </html>
修改/新增页面bookEdit.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://jsp.veryedu.cn" prefix="z"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!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"> <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.css" rel="stylesheet"> <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.js"></script> <title>书籍新增/修改</title> </head> <body> <form class="form-inline" action="${pageContext.request.contextPath }/book.action?methodName=${empty b ? 'add' : 'edit'}" method="post"> 书籍ID:<input type="text" name="bid" value="${b.bid }"><br> 书籍名称:<input type="text" name="bname" value="${b.bname }"><br> 书籍价格:<input type="text" name="price" value="${b.price }"><br> <input type="submit"> </form> </body> </html>
3.案例演示
看到这,我相信你对自定义MVC一定有自己的独特理解了!!
至此自定义MVC三部曲完结!!