前言
通用增删改查、通用分页、XML解析反射建模,包括自定义MVC工作原理与低阶使用等等前面一系列的的文章,其实都是为了这篇博客所作的准备。在本篇博客,会将前面十几篇博客内容串联起来,做出一个简单却不平凡的微项目。
一、环境配置
1.1 将框架打包成jar包
将前面我们所写的通用Servlet——框架打包成Jar包
1.2 将Jar包导入新项目
将Jar包导入新项目,并导入相关依赖
1.3 将分页标签相关文件、及相关助手类导入
tag和util
1.4 配置文件
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" version="3.1">
<display-name>J2EE_Project</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>com.xqx.framework.DispatherServlet</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>
tld文件:自定义标签
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<description>JSTL 1.1 core library</description>
<display-name>JSTL core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>x</short-name>
<uri>http://jsp.xqx.cn</uri>
<tag>
<name>page</name>
<tag-class>com.xqx.tag.PageTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>pageBean</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
二、前后台编写
2.1 实体类
package com.xqx.entity;
/**
*
* @author W许潜行
*
*/
public class Book {
private int bid;
private String bname;
private float price;
@Override
public String toString() {
return "Book [bid=" + bid + ", bname=" + bname + ", price=" + price + "]";
}
public int getBid() {
return bid;
}
public void setBid(int bid) {
this.bid = bid;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public Book(int bid, String bname, float price) {
super();
this.bid = bid;
this.bname = bname;
this.price = price;
}
public Book() {
// TODO Auto-generated constructor stub
}
}
2.2 dao
package com.xqx.dao;
import java.util.List;
import com.xqx.entity.Book;
import com.xqx.util.BaseDao;
import com.xqx.util.PageBean;
import com.xqx.util.StringUtils;
/**Book dao方法 继承BaseDao
* @author W许潜行
* 2023年7月4日 上午10:00:32
*/
public class BookDao extends BaseDao<Book> {
/**新增
* @param book
* @return
* @throws Exception
*/
public int add(Book book) throws Exception {
String sql = "insert into t_mvc_book values(?,?,?)";
return super.executeUpdate(sql, book, new String[] { "bid", "bname", "price" });
}
/**删除
* @param book
* @return
* @throws Exception
*/
public int delete(Book book) throws Exception {
String sql = "delete from t_mvc_book where bid =?";
return super.executeUpdate(sql, book, new String[] { "bid" });
}
/**修改
* @param book
* @return
* @throws Exception
*/
public int update(Book book) throws Exception {
String sql = "update t_mvc_book set bname=?,price=? where bid=?";
return super.executeUpdate(sql, book, new String[] { "bname", "price", "bid" });
}
/**查询
* @param book
* @param pageBean
* @return
* @throws Exception
*/
public List<Book> list(Book book, PageBean pageBean) throws Exception {
String sql = "select * from t_mvc_book where 1=1 ";
int bid = book.getBid();
String bname = book.getBname();
if (StringUtils.isNotBlank(bname)) {
sql += " where bname like '%" + bname + "%'";
}
if (bid != 0) {
sql += " and where bid=" + bid;
}
return super.executeQuery(sql, Book.class, pageBean);
}
}
2.3 Servlet
package com.xqx.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.xqx.dao.BookDao;
import com.xqx.entity.Book;
import com.xqx.framework.Action;
import com.xqx.framework.ModelDriver;
import com.xqx.util.PageBean;
/**子控制类
* @author W许潜行
* 2023年7月4日 上午10:04:25
*/
public class BookAction extends Action implements ModelDriver<Book> {
private Book book = new Book();
BookDao bd = new BookDao();
/**
* 查询数据
*
* @param req
* @param resp
* @return
* @throws Exception
*/
public String list(HttpServletRequest req, HttpServletResponse resp) throws Exception {
PageBean pageBean = new PageBean();
// 初始化
pageBean.setRequest(req);
List<Book> list = bd.list(book, pageBean);
req.setAttribute("list", list);
req.setAttribute("pageBean", pageBean);
System.out.println("调用了");
return "list";
}
/**
* 传入编辑页面的值
*
* @param req
* @param resp
* @return
* @throws Exception
*/
public String toEdit(HttpServletRequest req, HttpServletResponse resp) throws Exception {
List<Book> list = bd.list(book, null);
req.setAttribute("edit", list.get(0));
return "toEdit";
}
/**新增
* @param req
* @param resp
* @return
* @throws Exception
*/
public String add(HttpServletRequest req, HttpServletResponse resp) throws Exception {
bd.add(book);
return "toList";
}
/**删除
* @param req
* @param resp
* @return
* @throws Exception
*/
public String delete(HttpServletRequest req, HttpServletResponse resp) throws Exception {
bd.delete(book);
return "toList";
}
/**修改
* @param req
* @param resp
* @return
* @throws Exception
*/
public String update(HttpServletRequest req, HttpServletResponse resp) throws Exception {
bd.update(book);
return "toList";
}
@Override
public Book getModel() {
return book;
}
}
2.4 配置mvc.xml
conf/mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<action path="/book" type="com.xqx.web.BookAction">
<forward name="list" path="/bookList.jsp" redirect="false" />
<forward name="toList" path="/book.action?methodName=list" redirect="true" />
<forward name="toEdit" path="/bookEdit.jsp" redirect="false" />
</action>
</config>
所需要操作的类和方法在该文件配置
2.5 JSP
booList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="x" uri="http://jsp.xqx.cn"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
<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;
}
.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="请输入书籍名称">
</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 items="${list }" var="b">
<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>
<!-- 分页导航栏 -->
<x:page pageBean="${pageBean }"></x:page>
</body>
</html>
bookEdit.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.xqx.cn" prefix="x"%>
<%@ 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 edit ? 'add' : 'edit'}" method="post">
书籍ID:<input type="text" name="bid" value="${edit.bid }"><br>
书籍名称:<input type="text" name="bname" value="${edit.bname }"><br>
书籍价格:<input type="text" name="price" value="${edit.price }"><br>
<input type="submit">
</form>
</body>
</html>
2.6 运行结果
文章来源:https://www.toymoban.com/news/detail-531441.html
文章来源地址https://www.toymoban.com/news/detail-531441.html
修改
好啦,本篇博客就到此为止!
希望你看完本篇文章有所收获,祝你变得更强!!!
到了这里,关于自定义MVC的进阶使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!