MVC框架自定義實現過程

1、思維導圖

2、什麼是MVC?

MVC全名是Model View Controller,是模型(model)-視圖(view)-控制器(controller)的縮寫, 它是一種軟件設計典范,用一種業務邏輯、數據、界面顯示分離的方法組織代碼。

3、運行原理

用戶發送請求 → 中央控制器接受用戶請求 → 分析請求連接/獲取到用戶需要的類+方法 → 調用相對應的Model → 訪問數據庫服務器

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 {
		/**
		 * 增刪改查缺陷:
		 * 		當需求發送改變,或者新增需求的時候,需要改動下面代碼
		 * 
		 * 解決方案:
		 * 		前臺傳遞name到後臺,實際就是想要調用當前(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;
/**
 * 
 * 目標:
 * 	根據自定義mvc框架的原理圖  完成框架的研發
 * @author Tang  中央控制器
 * 	尋找子控制器
 *
 * 2021年8月30日  下午6:49:35
 */
 
@WebServlet("*.action")
public class DispatchServlet extends HttpServlet {	
	//存放子控制器的容器
	private Map<String , ActionSupport> actions = new HashMap<String , ActionSupport>();
	//初始化子控制器容器(集合),經過初始化,action容器內部就有瞭子控制器
	//init(初始化方法),service(服務),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
		//目標: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; 
/**
 * 
 * 目標:
 * 	根據自定義mvc框架的原理圖  完成框架的研發
 * 
 * 
 * @author Tang  中央控制器
 * 	尋找子控制器
 *
 * 2021年8月30日  下午6:49:35
 */ 
@WebServlet("*.action")
public class DispatchServlet extends HttpServlet {	
	//存放子控制器的容器
	private Map<String , ActionSupport> actions = new HashMap<String , ActionSupport>();
	//初始化子控制器容器(集合),經過初始化,action容器內部就有瞭子控制器
	//init(初始化方法),service(服務),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
		//目標: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>
	目前多數人增刪改查的代碼:
		<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" >關聯</a>
	
</body>
</html>

5、運行結果

以上就是MVC框架自定義實現過程的詳細內容,更多關於MVC框架的資料請關註WalkonNet其它相關文章!

推薦閱讀: