当前位置:网站首页>简单自定义mvc
简单自定义mvc
2022-06-30 02:58:00 【安离九歌】
前言
,上次我们分享了通用分页(2),今天分享的内容是自定义MVC。此前我也分享过了mvc模式,今天要分享的是定义一个我们自己mvc。
一、最初版增删改查
最初版的增删改查每一个功能对应了一个servlet。
package com.zhw.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/book/add")
public class AddBookServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("处理书籍的增加业务,调用BookBiz");
}
}package com.zhw.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/book/del")
public class DelBookServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("处理书籍的删除业务,调用BookBiz");
}
}package com.zhw.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/book/edit")
public class EditBookServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("处理书籍的编辑业务,调用BookBiz");
}
}
package com.zhw.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/book/list")
public class ListBookServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("处理书籍的查询业务,调用BookBiz");
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>目前的增删改查</h3>
<a href="${pageContext.request.contextPath }/book/add">增加</a>
<a href="${pageContext.request.contextPath }/book/del">删除</a>
<a href="${pageContext.request.contextPath }/book/update">修改</a>
<a href="${pageContext.request.contextPath }/book/select">查看</a>
</body>
</html>

对于每一个功能都需要一个单独的servelt我们可以进行优化。
<%@ 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>
<h3>类数量过多问题的优化</h3>
<a href="${pageContext.request.contextPath }/book.action?methodName=add">增加</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=del">删除</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=edit">修改</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=list">查询</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=load">回显</a>
</body>
</html>
package com.zhw.servlet;
import java.io.IOException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/book.action")
public class BookServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 为了区分当前请求的目的,增删改查的目的,就从前台将要调用的方法名传递到后台
String methodName = req.getParameter("methodName");
if("add".equals(methodName)){
// 如果前台传递到后台的是一个新增的请求,那么后台就调用新增方法
add(req,resp);
}
else if("del".equals(methodName)) {
del(req,resp);
}
else if("edit".equals(methodName)) {
edit(req,resp);
}
else if("list".equals(methodName)) {
list(req,resp);
}
// else if("load".equals(methodName)) {
// load(req,resp);
// }
}
private void list(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 list 方法");
}
// private void load(HttpServletRequest req, HttpServletResponse resp) {
// System.out.println("在用一个servlet中调用 list 方法");
// }
private void edit(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 edit 方法");
}
private void del(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 del 方法");
}
private void add(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 add 方法");
}
} 
二、反射版增删查改
package com.zhw.web;
import java.io.IOException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/book.action")
public class BookServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 为了区分当前请求的目的,增删改查的目的,就从前台将要调用的方法名传递到后台
String methodName = req.getParameter("methodName");
// methodName可能是add/del/edit/list/load/xxx/yyy/aaa...
// 前台传递什么方法,就调用当前类的对应方法
try {
Method m = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
m.setAccessible(true);
// 调用当前类实例的methodName方法
m.invoke(this, req,resp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
private void list(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 list 方法");
}
private void load(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 list 方法");
}
private void edit(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 edit 方法");
}
private void del(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 del 方法");
}
private void add(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 add 方法");
}
}
<%@ 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>
<h3>类数量过多问题的优化</h3>
<a href="${pageContext.request.contextPath }/book.action?methodName=add">增加</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=del">删除</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=edit">修改</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=list">查询</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=load">回显</a>
</body>
</html>
三、实现
首先看一下原理图:

思路如下:
先建立中央控制器也就是ActionServlet
对应处理人就是Action
ActionSupport继承于Action
BooKAction实现ActionSupport
package com.zhw.framework;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zhw.web.BooKAction;
/**
* 中央控制器:
* 主要职能:接受浏览器请求,找到对应的处理人
*
* @author Administrator
*
*/
@WebServlet("*.action")
public class DispatcherServlet extends HttpServlet{
private Map<String, Action> actions=new HashMap<String, Action>();
// 程序启动时,只会加载一次
@Override
public void init() throws ServletException {
actions.put("/book", new BooKAction());
// actions.put("/order", new BooKAction());
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//http://localhost:8080/mvc/book.aciton?methodName=list
String uri = req.getRequestURI();
// 要拿到/book,就是最后一个/到最后一个点的位置
uri=uri.substring(uri.lastIndexOf("/")
, uri.lastIndexOf("."));
Action action = actions.get(uri);
System.out.println(action);
action.execute(req, resp);
}
}package com.zhw.framework;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 子控制器:
* 对应请求的处理人
* @author Administrator
*
*/
public interface Action {
void execute(HttpServletRequest req, HttpServletResponse resp);
}package com.zhw.framework;
import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ActionSupport implements Action{
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) {
String methodName = req.getParameter("methodName");
// methodName可能是add/del/edit/list/load/xxx/yyy/aaa...
// 前台传递什么方法,就调用当前类的对应方法
try {
Method m = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
m.setAccessible(true);
// 调用当前类实例的methodName方法
m.invoke(this, req,resp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void list(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 list 方法");
}
private void load(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 list 方法");
}
private void edit(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 edit 方法");
}
private void del(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 del 方法");
}
private void add(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 add 方法");
}
}package com.zhw.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zhw.framework.Action;
import com.zhw.framework.ActionSupport;
public class BooKAction extends ActionSupport {
private void list(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 list 方法");
}
private void load(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 list 方法");
}
private void edit(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 edit 方法");
}
private void del(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 del 方法");
}
private void add(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 add 方法");
}
}
总结
这次分享的内容是自定义MVC,希望对你能有帮助,下期预告自定义MVC。
我是九歌,一个喜欢编程的程序员。
如有错误还望指正,谢谢啦。
边栏推荐
- What kind of foreign exchange trading platform is regulated and safe?
- Recursion frog jumping steps problem
- Summary of PHP test sites encountered in CTF questions (I)
- [Postgres] Postgres database migration
- (graph theory) connected component (template) + strongly connected component (template)
- New edition of diazotization process in 2022 and analysis of diazotization process
- 2022护网行动在即,关于护网的那些事儿
- Some technology sharing
- 在php中字符串的概念是什么
- Wechat applet +php to realize authorized login operation
猜你喜欢

O & M (20) make and start USB flash disk and install win10

What is the difference between a layer 3 switch and a layer 2 switch

Heavy attack -- ue5's open source digital twin solution

Time complexity analysis

Distributed file system fastdfs

uniapp 地址转换经纬度

浅谈IDEA的优化和使用

约瑟夫环 数学解法
![[Postgres] Postgres database migration](/img/45/7074aa766640160a3b6f00b109cb2f.png)
[Postgres] Postgres database migration

CMake教程系列-02-使用cmake代码生成二进制
随机推荐
Raki's notes on reading paper: named entity recognition as dependency parsing
Interrupt operation: abortcontroller learning notes
Use of Arthas
Heavy attack -- ue5's open source digital twin solution
Raki's notes on reading paper: Leveraging type descriptions for zero shot named entity recognition and classification
Servlet面试题
迅为恩智浦iTOP-IMX6开发平台
CMake教程系列-02-使用cmake代碼生成二進制
What about punctuation in the first column of unity text
O & M (20) make and start USB flash disk and install win10
Uniapp address translation latitude and longitude
快速排序、聚簇索引、寻找数据中第k大的值
uniapp 地址转换经纬度
在php中字符串的概念是什么
Série de tutoriels cmake - 02 - génération de binaires à l'aide du Code cmake
Some technology sharing
Global and Chinese market of ERP software for garment and textile industries 2022-2028: Research Report on technology, participants, trends, market size and share
Jvxetable sub table record loading completion event
Functions in C language
Cmake tutorial series -04- compiling related functions