思维导图
?
补充知识:1.解决配置文件可随意更改问题
1.配置的文件位置:web.xml
@Override
public void init() throws ServletException {
try {
configModel = ConfigModelFactory.build("/mvc.xml");
} catch (Exception e) {
e.printStackTrace();
}
}
2.配置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>T266_MVC</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>
3.再做个判断,等于Null或者等于" " 给它一个默认的值
@Override
public void init() throws ServletException {
try {
String configurationLacation=this.getInitParameter("configurationLacation");
if(configurationLacation==null||"".equals(configurationLacation)){
configurationLacation="/zking.xml";
}
configModel = ConfigModelFactory.build("/mvc.xml");
} catch (Exception e) {
e.printStackTrace();
}
}
2.利用框架开发项目
1.导入框架的jar? :点击framework包右键选中Export在弹出的框里收缩jar file
2.配置框架的相关配置文件
①mvc.xml放到源文件夹下方
②在web.xml中配置mvc.xml,让中央控制器加载mvc.xml建模
3.业务开发
①实体类entiy
②Dao层:1.写sql 2.建立连接 3.预定义对象PreparedStatement 4.设置参数 5.执行sql
③Web层子控制器
④View层JSP
3.完整版增删查改
页面展示:
?修改后:
?
增加后:
删除后:
?
?代码展示:
BookDao类:
public void add(Book book) throws Exception {
String sql = "insert into t_book values(?,?,?)";
super.executeUpdate(sql, book, new String[] {"bid","bname","price"});
}
public void delete(Book book) throws Exception {
String sql = "delete from t_book where bid = ?";
super.executeUpdate(sql, book, new String[] {"bid"});
}
public void edit(Book book) throws Exception {
String sql = "update t_book set bname = ?,price = ? where bid = ?";
super.executeUpdate(sql, book, new String[] {"bname","price","bid"});
}
public List<Book> list(Book book,PageBean pageBean) throws Exception {
String sql = "select * from t_book where 1=1 ";
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);
}
web层
子控制器必须继承ActionSupport实现模型驱动接口
public class BookAction extends ActionSupport implements ModelDriver<Book>{
}
1.增删改返回toList? ? ? ? 2.查询返回toList? ? ? ? 3.回显返回toEdit
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 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";
}
public String list(HttpServletRequest req, HttpServletResponse resp) {
PageBean pageBean = new PageBean();
pageBean.setRequest(req);
try {
List<Book> list = bookDao.list(book, pageBean);
req.setAttribute("books", list);
req.setAttribute("pageBean", pageBean);
} catch (Exception e) {
e.printStackTrace();
}
return "list";
}
public String toEdit(HttpServletRequest req, HttpServletResponse resp) {
if(book.getBid() != 0) {
try {
List<Book> list = bookDao.list(book, null);
req.setAttribute("b", list.get(0));
} catch (Exception e) {
e.printStackTrace();
}
}
return "toEdit";
}
View层
<%@ 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>
<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 }/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>
<%@ 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?methodName=${empty b ? 'add' : 'edit'}" method="post">
bid:<input type="text" name="bid" value="${b.bid }"><br>
bname:<input type="text" name="bname" value="${b.bname }"><br>
price:<input type="text" name="price" value="${b.price }"><br>
<input type="submit">
</form>
</body>
</html>
注意:!!!? ? 问题
运行完页面会出现报错? :http://localhost:8080/T266_mvc_crud/bookList.jsp
?解决方法:
应当在界面输入:http://localhost:8080/T266_mvc_crud/book.action?methodName=list
|