当前位置:网站首页>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! .
边栏推荐
- [live broadcast notes 0629] Concurrent Programming II: lock
- 福利抽奖 | 开源企业级监控Zabbix6.0都有哪些亮点
- How does native JS generate Jiugong lattice
- Link garbled escape character
- Federal learning: dividing non IID samples by Dirichlet distribution
- Customize the buttons of jvxetable and the usage of $set under notes
- 如何实现远程协同办公,收好这份攻略!
- Sorting method of administrative route code letter + number
- 浅谈IDEA的优化和使用
- 【直播笔记0629】 并发编程二:锁
猜你喜欢

O & M (21) make winpe startup USB flash disk

Use compose to realize the effect of selecting movie seats by panning tickets

mysql 主从数据库同步失败的原因

IDEA 远程调试 Remote JVM Debug

Auto.js学习笔记16:按项目保存到手机上,不用每次都保存单个js文件,方便调试和打包

Study diary: February 15, 2022

Intel hex, Motorola S-Record format detailed analysis

约瑟夫环 数学解法

How to realize remote collaborative office, keep this strategy!

中断操作:AbortController学习笔记
随机推荐
编译一个无导入表的DLL
Three solutions to forced hibernation of corporate computers
链接乱码转义符
Link garbled escape character
Note the use of export/import and class inheritance in ES6
How to modify and add fields when MySQL table data is large
华为面试题: 高矮个子排队
What kind of foreign exchange trading platform is regulated and safe?
Idea remote debugging remote JVM debug
Comparable和Comparator的区别
2022 new test questions for safety management personnel of metal and nonmetal mines (small open pit quarries) and certificate examination for safety management personnel of metal and nonmetal mines (s
Add a custom button to jvxetable
WPF Initialized事件在.cs中绑定不被触发的原因
Differences between comparable and comparator
Utf8 error in Oracle migration of Jincang Kingbase database
Which is a good foreign exchange trading platform? Is it safe to have regulated funds?
约瑟夫环 数学解法
&nbsp; Difference from spaces
Visual HTA form designer htamaker interface introduction and usage, Download | HTA VBS visual script writing
2022 tool fitter (Advanced) and tool fitter (Advanced) certificate examination