MVC之通用增删改

思维导图:MVC之通用增删改

 

一,导入jar包

步骤:1,鼠标单击选择包,右键选择MVC之通用增删改

 2,选择MVC之通用增删改

 3,点击之后选择jar包位置MVC之通用增删改

4,导入自定义MVC框架所使用的相关jar包 MVC之通用增删改

二,web.xml做配置

1.新建一个source folder文件夹

将framework下的config.xml复制到conf文件夹下(根文件夹)

MVC之通用增删改

2.在*控制器中的、将@WebServlet("*.action")注释掉

3. 在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-crud</display-name>
  <servlet>
     <servlet-name>mvc</servlet-name>
     <servlet-class>com.zking.framework.DispatchServlet</servlet-class>
     <init-param>
        <param-name>configurationLocation</param-name>
        <param-value>/mvc.xml</param-value>
     </init-param>
  
  </servlet>
  <servlet-mapping>
     <servlet-name>mvc</servlet-name>
     <url-pattern>*.action</url-pattern>
  </servlet-mapping>
</web-app>

4,根目录下mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<config> 
		<forward name="list" path="/bookList.jsp" redirect="false" />
		<forward name="toList" path="/Book.action?methodName=list" redirect="true" /> Book.action?methodName=list
		<forward name="toEdit" path="/bookEdit.jsp" redirect="false" />
	</action> 
</config>

三,写代码

 实体类:MVC之通用增删改

 bookdao:

 public class BookDao extends BaseDao{
 
	public void add(Book book) throws Exception {
		String sql="insert into tb_book values(?,?,?)";
		super.executeUpdate(sql, book,new String[]{"id","bname","price"});
	}
	
	public void del(Book book) throws Exception {
		String sql="delete from tb_book where id= ?";
		super.executeUpdate(sql, book,new String[]{"id"});
	}
	
	public void edit(Book book) throws Exception {
		String sql="update tb_book set bname=?,price=? where id=?";
		super.executeUpdate(sql, book,new String[]{"bname","price","id"});
	}
	
	public List<Book> list(Book book,PageBean pageBean) throws Exception {
		String sql="select * from tb_book where 1=1";
		String bname=book.getBname();
		if(StringUtils.isNotBlank(bname)) {
			sql+=" and bname like '%"+bname+"%'";
		}
		return super.executeQuery(sql, Book.class, pageBean);
	} 
}

bookaction:

package com.web;
/**
 *子控制层
 */
 /**
 * 目标:1,利用自己做的自定义mvc框架完成增删改查
 * 2,通用的分页查询&增删改
 * 
 * 步骤:1,导入框架的jar包
 * 2,做好框架的使用配置
 * 3,一切照旧(JSP,servlet/BookAction子控制器,dao,entity)
 * @author zjjt
 *页面跳子控制器去处理,子控制器调dao层完成增删改查
 */
public class BookAction extends ActionSupport implements ModelDriver<Book>{
private Book book=new Book();
private BookDao bookDao=new BookDao();
@Override
	public Book getModel() {
		// TODO Auto-generated method stub
		return book;
	}
	
		/**
		 * 增删改都要调回查询界面
		 * 分析增删改一共有多少结果集的配置
		 * 查询:BookList.jsp  返回值:list
		 * 增删改:book.action?mathodName=list    返回值:toList
		 * 增加修改跳转对应界面:bookEdit.jsp  返回值:toEdit
		 * 
		 * 
		 */
		public String add(HttpServletRequest req, HttpServletResponse resp) {
			try {
				bookDao.add(book);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return "toList";
		}
	
		public String del(HttpServletRequest req, HttpServletResponse resp) {
			try {
				bookDao.del(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.getId()!=0) {
					List<Book> list = bookDao.list(book, null);
					req.setAttribute("b", list.get(0));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return "toEdit";
		}
	
		public String list(HttpServletRequest req, HttpServletResponse resp) {
			try {
				PageBean pageBean = new PageBean();
				pageBean.setRequest(req);
				List<Book> list = bookDao.list(book,pageBean);
				req.setAttribute("book", list);
				req.setAttribute("pageBean", pageBean);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return "list";
		}
	
}

JSP:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ 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>
	<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="false" type="hidden">
		</div>
		<button type="submit" class="btn btn-primary mb-2">查询</button>
		<a class="btn btn-primary mb-2" href="${pageContext.request.contextPath }/bookEdit.jsp">新增</a>
	</form>

	<table class="table table-striped bg-success">
		<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="${book }">
			<tr>
				<td>${b.id }</td>
				<td>${b.bname }</td>
				<td>${b.price }</td>
				<td>
					<a href="${pageContext.request.contextPath }/Book.action?methodName=toEdit&id=${b.id}">修改</a>
					<a href="${pageContext.request.contextPath }/Book.action?methodName=del&id=${b.id}">删除</a>
				</td>
			</tr>
			</c:forEach>
		</tbody>
	</table>
	<!-- 这一行代码就相当于前面分页需求前端的几十行了 -->
	<z:page pageBean="${pageBean }"></z:page>

</body>
</html>

展示:MVC之通用增删改

 MVC之通用增删改

MVC之通用增删改

上一篇:nltk 安装教程


下一篇:网上书城项目介绍