MVC框架自定義實現(xiàn)過程
1、思維導(dǎo)圖
2、什么是MVC?
MVC全名是Model View Controller,是模型(model)-視圖(view)-控制器(controller)的縮寫, 它是一種軟件設(shè)計典范,用一種業(yè)務(wù)邏輯、數(shù)據(jù)、界面顯示分離的方法組織代碼。
3、運行原理
用戶發(fā)送請求 → 中央控制器接受用戶請求 → 分析請求連接/獲取到用戶需要的類+方法 → 調(diào)用相對應(yīng)的Model → 訪問數(shù)據(jù)庫服務(wù)器
4、演繹過程
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ā)送改變,或者新增需求的時候,需要改動下面代碼 * * 解決方案: * 前臺傳遞name到后臺,實際就是想要調(diào)用當(dāng)前(this)類對象的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)過初始化,action容器內(nèi)部就有了子控制器 //init(初始化方法),service(服務(wù)),destroy(銷毀) @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 { //完成子控制器對的過程 //瀏覽器: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)過初始化,action容器內(nèi)部就有了子控制器 //init(初始化方法),service(服務(wù)),destroy(銷毀) @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 { //完成子控制器對的過程 //瀏覽器: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" >查詢</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" >查詢</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、運行結(jié)果
以上就是MVC框架自定義實現(xiàn)過程的詳細(xì)內(nèi)容,更多關(guān)于MVC框架的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Springboot+Netty+Websocket實現(xiàn)消息推送實例
這篇文章主要介紹了Springboot+Netty+Websocket實現(xiàn)消息推送實例,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02java開發(fā)分布式服務(wù)框架Dubbo原理機制詳解
這篇文章主要為大家介紹了java開發(fā)分布式服務(wù)框架Dubbo的原理機制詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-11-11Springboot項目刪除項目同步target文件問題解決方案
這篇文章主要介紹了Springboot項目刪除項目同步target文件問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-12-12SpringBoot Devtools實現(xiàn)項目熱部署的方法示例
這篇文章主要介紹了SpringBoot Devtools實現(xiàn)項目熱部署的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01springboot+vue前后端分離項目中使用jwt實現(xiàn)登錄認(rèn)證
本文介紹了如何在SpringBoot+Vue前后端分離的項目中使用JWT實現(xiàn)登錄認(rèn)證,內(nèi)容包括后端的響應(yīng)工具類、JWT工具類、登錄用戶實體類、登錄接口、測試接口、過濾器、啟動類以及前端的登錄頁面實現(xiàn),感興趣的可以了解一下2024-10-10java.lang.IllegalStateException:方法有太多主體參數(shù)問題
這篇文章主要介紹了java.lang.IllegalStateException:方法有太多主體參數(shù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07springboot中配置好登錄攔截后,swagger訪問不了問題
這篇文章主要介紹了springboot中配置好登錄攔截后,swagger訪問不了問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12