您好,登錄后才能下訂單哦!
本篇內容主要講解“MVC框架自定義的實現過程”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“MVC框架自定義的實現過程”吧!
MVC全名是Model View Controller,是模型(model)-視圖(view)-控制器(controller)的縮寫, 它是一種軟件設計典范,用一種業務邏輯、數據、界面顯示分離的方法組織代碼。
用戶發送請求 → 中央控制器接受用戶請求 → 分析請求連接/獲取到用戶需要的類+方法 → 調用相對應的Model → 訪問數據庫服務器
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); }*/ } }
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); } }
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(); } } }
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); } }
<%@ 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>
到此,相信大家對“MVC框架自定義的實現過程”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。