MVC框架自定義實(shí)現(xiàn)過(guò)程
1、思維導(dǎo)圖

2、什么是MVC?
MVC全名是Model View Controller,是模型(model)-視圖(view)-控制器(controller)的縮寫(xiě), 它是一種軟件設(shè)計(jì)典范,用一種業(yè)務(wù)邏輯、數(shù)據(jù)、界面顯示分離的方法組織代碼。
3、運(yùn)行原理
用戶(hù)發(fā)送請(qǐng)求 → 中央控制器接受用戶(hù)請(qǐng)求 → 分析請(qǐng)求連接/獲取到用戶(hù)需要的類(lèi)+方法 → 調(diào)用相對(duì)應(yīng)的Model → 訪(fǎng)問(wèn)數(shù)據(jù)庫(kù)服務(wù)器
4、演繹過(guò)程
4.1.控制層
BookServlet:
package com.tyf.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;
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 {
/**
* 增刪改查缺陷:
* 當(dāng)需求發(fā)送改變,或者新增需求的時(shí)候,需要改動(dòng)下面代碼
*
* 解決方案:
* 前臺(tái)傳遞name到后臺(tái),實(shí)際就是想要調(diào)用當(dāng)前(this)類(lèi)對(duì)象的name方法
*/
String name = req.getParameter("name");
try {
Method m = this.getClass().getDeclaredMethod(name, HttpServletRequest.class,HttpServletResponse.class);
m.setAccessible(true);
m.invoke(this, req,resp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*if ("add".equals(name)) {
add(req,resp);
}else if ("delete".equals(name)) {
delete(req,resp);
}else if ("edit".equals(name)) {
edit(req,resp);
}else if ("list".equals(name)) {
list(req,resp);
}else if ("load".equals(name)) {
load(req,resp);
}*/
}
}
DispatchServlet:
package com.tyf.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.tyf.web.BookAction;
/**
*
* 目標(biāo):
* 根據(jù)自定義mvc框架的原理圖 完成框架的研發(fā)
* @author Tang 中央控制器
* 尋找子控制器
*
* 2021年8月30日 下午6:49:35
*/
@WebServlet("*.action")
public class DispatchServlet extends HttpServlet {
//存放子控制器的容器
private Map<String , ActionSupport> actions = new HashMap<String , ActionSupport>();
//初始化子控制器容器(集合),經(jīng)過(guò)初始化,action容器內(nèi)部就有了子控制器
//init(初始化方法),service(服務(wù)),destroy(銷(xiāo)毀)
@Override
public void init() throws ServletException {
actions.put("/book", 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 {
//完成子控制器對(duì)的過(guò)程
//瀏覽器:http://localhost:8080/J2ee12/book.action?name=add
//目標(biāo):BookAction.add()...
/**
* 思路:
* 1.從瀏覽器URL中獲取到"/book"字符串
* 2.在子控制器中拿到BookAction
* 3.BookAction.add()
*/
String url = req.getRequestURI();
url = url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));
//action=BookAction
ActionSupport action = actions.get(url);
action.execute(req, resp);
}
}
4.2.模型層
ActionSupport:
package com.tyf.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 name = req.getParameter("name");
try {
Method m = this.getClass().getDeclaredMethod(name, HttpServletRequest.class,HttpServletResponse.class);
m.setAccessible(true);
m.invoke(this, req,resp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
DispatchServlet:
package com.tyf.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.tyf.web.BookAction;
/**
*
* 目標(biāo):
* 根據(jù)自定義mvc框架的原理圖 完成框架的研發(fā)
*
*
* @author Tang 中央控制器
* 尋找子控制器
*
* 2021年8月30日 下午6:49:35
*/
@WebServlet("*.action")
public class DispatchServlet extends HttpServlet {
//存放子控制器的容器
private Map<String , ActionSupport> actions = new HashMap<String , ActionSupport>();
//初始化子控制器容器(集合),經(jīng)過(guò)初始化,action容器內(nèi)部就有了子控制器
//init(初始化方法),service(服務(wù)),destroy(銷(xiāo)毀)
@Override
public void init() throws ServletException {
actions.put("/book", 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 {
//完成子控制器對(duì)的過(guò)程
//瀏覽器:http://localhost:8080/J2ee12/book.action?name=add
//目標(biāo):BookAction.add()...
/**
* 思路:
* 1.從瀏覽器URL中獲取到"/book"字符串
* 2.在子控制器中拿到BookAction
* 3.BookAction.add()
*/
String url = req.getRequestURI();
url = url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));
//action=BookAction
ActionSupport action = actions.get(url);
action.execute(req, resp);
}
}
4.3視圖層
<%@ 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>
目前多數(shù)人增刪改查的代碼:
<a href="${pageContext.request.contextPath}/book/add" rel="external nofollow" >增加</a>
<a href="${pageContext.request.contextPath}/book/delete" rel="external nofollow" >刪除</a>
<a href="${pageContext.request.contextPath}/book/edit" rel="external nofollow" >修改</a>
<a href="${pageContext.request.contextPath}/book/list" rel="external nofollow" >查詢(xún)</a>
<hr>
增刪改查的代碼2.0
<a href="${pageContext.request.contextPath}/book.action?name=add" rel="external nofollow" >增加</a>
<a href="${pageContext.request.contextPath}/book.action?name=delete" rel="external nofollow" >刪除</a>
<a href="${pageContext.request.contextPath}/book.action?name=edit" rel="external nofollow" >修改</a>
<a href="${pageContext.request.contextPath}/book.action?name=list" rel="external nofollow" >查詢(xún)</a>
<hr>
增刪改查的代碼3.0
<a href="${pageContext.request.contextPath}/book.action?name=load" rel="external nofollow" >回顯</a>
<a href="${pageContext.request.contextPath}/book.action?name=ref" rel="external nofollow" >關(guān)聯(lián)</a>
</body>
</html>
5、運(yùn)行結(jié)果

以上就是MVC框架自定義實(shí)現(xiàn)過(guò)程的詳細(xì)內(nèi)容,更多關(guān)于MVC框架的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Springboot+Netty+Websocket實(shí)現(xiàn)消息推送實(shí)例
這篇文章主要介紹了Springboot+Netty+Websocket實(shí)現(xiàn)消息推送實(shí)例,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
java開(kāi)發(fā)分布式服務(wù)框架Dubbo原理機(jī)制詳解
這篇文章主要為大家介紹了java開(kāi)發(fā)分布式服務(wù)框架Dubbo的原理機(jī)制詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11
Springboot項(xiàng)目刪除項(xiàng)目同步target文件問(wèn)題解決方案
這篇文章主要介紹了Springboot項(xiàng)目刪除項(xiàng)目同步target文件問(wèn)題解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
SpringBoot Devtools實(shí)現(xiàn)項(xiàng)目熱部署的方法示例
這篇文章主要介紹了SpringBoot Devtools實(shí)現(xiàn)項(xiàng)目熱部署的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
springboot+vue前后端分離項(xiàng)目中使用jwt實(shí)現(xiàn)登錄認(rèn)證
本文介紹了如何在SpringBoot+Vue前后端分離的項(xiàng)目中使用JWT實(shí)現(xiàn)登錄認(rèn)證,內(nèi)容包括后端的響應(yīng)工具類(lèi)、JWT工具類(lèi)、登錄用戶(hù)實(shí)體類(lèi)、登錄接口、測(cè)試接口、過(guò)濾器、啟動(dòng)類(lèi)以及前端的登錄頁(yè)面實(shí)現(xiàn),感興趣的可以了解一下2024-10-10
java.lang.IllegalStateException:方法有太多主體參數(shù)問(wèn)題
這篇文章主要介紹了java.lang.IllegalStateException:方法有太多主體參數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
springboot中配置好登錄攔截后,swagger訪(fǎng)問(wèn)不了問(wèn)題
這篇文章主要介紹了springboot中配置好登錄攔截后,swagger訪(fǎng)問(wèn)不了問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12

