当前位置:网站首页>Simple custom MVC
Simple custom MVC
2022-06-30 03:05:00 【An Li Jiu Ge】
Preface
, Last time we shared the common paging (2), The content shared today is custom MVC. I have shared it before mvc Pattern , What we want to share today is to define ourselves mvc.
One 、 Addition, deletion, modification and query of the original version
In the original version, each function corresponds to one 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(" Deal with the increasing business of books , call 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(" Deal with deleting books , call 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(" Deal with the editing business of books , call 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(" Deal with the query business of books , call 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> The current addition, deletion, modification and query </h3>
<a href="${pageContext.request.contextPath }/book/add"> increase </a>
<a href="${pageContext.request.contextPath }/book/del"> Delete </a>
<a href="${pageContext.request.contextPath }/book/update"> modify </a>
<a href="${pageContext.request.contextPath }/book/select"> see </a>
</body>
</html>

For each function, a separate servelt We can optimize .
<%@ 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> Optimization of problems with too many classes </h3>
<a href="${pageContext.request.contextPath }/book.action?methodName=add"> increase </a>
<a href="${pageContext.request.contextPath }/book.action?methodName=del"> Delete </a>
<a href="${pageContext.request.contextPath }/book.action?methodName=edit"> modify </a>
<a href="${pageContext.request.contextPath }/book.action?methodName=list"> Inquire about </a>
<a href="${pageContext.request.contextPath }/book.action?methodName=load"> The echo </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 {
// To distinguish the purpose of the current request , The purpose of adding, deleting, and modifying the query , Pass the method name to be called from the foreground to the background
String methodName = req.getParameter("methodName");
if("add".equals(methodName)){
// If a new request is passed from the foreground to the background , Then the background will call the new method
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(" Using a servlet Call in list Method ");
}
// private void load(HttpServletRequest req, HttpServletResponse resp) {
// System.out.println(" Using a servlet Call in list Method ");
// }
private void edit(HttpServletRequest req, HttpServletResponse resp) {
System.out.println(" Using a servlet Call in edit Method ");
}
private void del(HttpServletRequest req, HttpServletResponse resp) {
System.out.println(" Using a servlet Call in del Method ");
}
private void add(HttpServletRequest req, HttpServletResponse resp) {
System.out.println(" Using a servlet Call in add Method ");
}
} 
Two 、 Add, delete, check and modify the reflection version
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 {
// To distinguish the purpose of the current request , The purpose of adding, deleting, and modifying the query , Pass the method name to be called from the foreground to the background
String methodName = req.getParameter("methodName");
// methodName May be add/del/edit/list/load/xxx/yyy/aaa...
// What method does the front desk transfer , Call the corresponding method of the current class
try {
Method m = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
m.setAccessible(true);
// Call the... Of the current class instance methodName Method
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(" Using a servlet Call in list Method ");
}
private void load(HttpServletRequest req, HttpServletResponse resp) {
System.out.println(" Using a servlet Call in list Method ");
}
private void edit(HttpServletRequest req, HttpServletResponse resp) {
System.out.println(" Using a servlet Call in edit Method ");
}
private void del(HttpServletRequest req, HttpServletResponse resp) {
System.out.println(" Using a servlet Call in del Method ");
}
private void add(HttpServletRequest req, HttpServletResponse resp) {
System.out.println(" Using a servlet Call in add Method ");
}
}
<%@ 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> Optimization of problems with too many classes </h3>
<a href="${pageContext.request.contextPath }/book.action?methodName=add"> increase </a>
<a href="${pageContext.request.contextPath }/book.action?methodName=del"> Delete </a>
<a href="${pageContext.request.contextPath }/book.action?methodName=edit"> modify </a>
<a href="${pageContext.request.contextPath }/book.action?methodName=list"> Inquire about </a>
<a href="${pageContext.request.contextPath }/book.action?methodName=load"> The echo </a>
</body>
</html>
3、 ... and 、 Realization
First look at the schematic diagram :

Ideas as follows :
First set up the central controller, that is ActionServlet
The corresponding handler is Action
ActionSupport Inherited from Action
BooKAction Realization 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;
/**
* The central controller :
* Main functions : Accept browser requests , Find the corresponding handler
*
* @author Administrator
*
*/
@WebServlet("*.action")
public class DispatcherServlet extends HttpServlet{
private Map<String, Action> actions=new HashMap<String, Action>();
// Program startup , Will only load once
@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();
// To get /book, It's the last one / To the last point
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;
/**
* Sub controller :
* The handler of the corresponding request
* @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 May be add/del/edit/list/load/xxx/yyy/aaa...
// What method does the front desk transfer , Call the corresponding method of the current class
try {
Method m = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
m.setAccessible(true);
// Call the... Of the current class instance methodName Method
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(" Using a servlet Call in list Method ");
}
private void load(HttpServletRequest req, HttpServletResponse resp) {
System.out.println(" Using a servlet Call in list Method ");
}
private void edit(HttpServletRequest req, HttpServletResponse resp) {
System.out.println(" Using a servlet Call in edit Method ");
}
private void del(HttpServletRequest req, HttpServletResponse resp) {
System.out.println(" Using a servlet Call in del Method ");
}
private void add(HttpServletRequest req, HttpServletResponse resp) {
System.out.println(" Using a servlet Call in add Method ");
}
}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(" Using a servlet Call in list Method ");
}
private void load(HttpServletRequest req, HttpServletResponse resp) {
System.out.println(" Using a servlet Call in list Method ");
}
private void edit(HttpServletRequest req, HttpServletResponse resp) {
System.out.println(" Using a servlet Call in edit Method ");
}
private void del(HttpServletRequest req, HttpServletResponse resp) {
System.out.println(" Using a servlet Call in del Method ");
}
private void add(HttpServletRequest req, HttpServletResponse resp) {
System.out.println(" Using a servlet Call in add Method ");
}
}
summary
The content shared this time is customized MVC, I hope it can help you , The preview of the next issue is customized MVC.
I'm nine songs , A programmer who likes programming .
If there is any mistake, please correct it , Thank you very much! .
边栏推荐
- How to realize remote collaborative office, keep this strategy!
- Gulang bilibilibili Live Screen Jackie
- What kind of foreign exchange trading platform is regulated and safe?
- (图论) 连通分量(模板) + 强连通分量(模板)
- GTK interface programming (I): Environment Construction
- Raki's notes on reading paper: neighborhood matching network for entity alignment
- 自定义JvxeTable的按钮及备注下$set的用法
- NLP text summary: data set introduction and preprocessing [New York Times annotated corpus]
- The rigorous judgment of ID number is accurate to the last place in the team
- Lua Basics
猜你喜欢

Prompt learning a blood case caused by a space

怎么使用Vant实现数据分页和下拉加载
![[live broadcast notes 0629] Concurrent Programming II: lock](/img/5c/42f5c9a9969b4d2bb950a7caac5555.png)
[live broadcast notes 0629] Concurrent Programming II: lock

HOOK Native API

How to switch ipykernel to a different CONDA virtual environment in jupyterlab?

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

uniapp 地址转换经纬度

MySQL extracts strings from table fields

HTA入门基础教程 | VBS脚本的GUI界面 HTA简明教程 ,附带完整历程及界面美化

C # basic learning (XIII) | breakpoint debugging
随机推荐
DC/DC变换器轻载时三种工作模式的原理及优缺点
(graph theory) connected component (template) + strongly connected component (template)
【实战技能】如何撰写敏捷开发文档
Auto. JS learning notes 16: save to the mobile phone by project, instead of saving a single JS file every time, which is convenient for debugging and packaging
Use compose to realize the effect of selecting movie seats by panning tickets
2. successfully solved bug:exception when publishing [Failed to connect and initialize SSH connection...
福利抽奖 | 开源企业级监控Zabbix6.0都有哪些亮点
Cmake tutorial series-01-minimum configuration example
Gulang bilibilibili Live Screen Jackie
正则全匹配:密码由8位以上数字,大小写字母,特殊字符组成
threejs 镜子案例Reflector 创建镜子+房子搭建+小球移动
Code for generating test and training sets
MySQL extracts strings from table fields
The Oracle main program is deleted, but the data is on another hard disk. Can I import the data again?
PHP two-dimensional array randomly fetches a random or fixed number of one-dimensional arrays
通用分页(2)
如何实现远程协同办公,收好这份攻略!
What is the metauniverse: where are we, where are we going
HTA introductory basic tutorial | GUI interface of vbs script HTA concise tutorial, with complete course and interface beautification
Welfare lottery | what are the highlights of open source enterprise monitoring zabbix6.0