gpt4 book ai didi

MVC框架自定义实现过程

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 26 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章MVC框架自定义实现过程由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

1、思维导图

MVC框架自定义实现过程

  。

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框架自定义实现过程的详细内容,更多关于MVC框架的资料请关注我其它相关文章! 。

原文链接:https://blog.csdn.net/m0_54525420/article/details/120003916 。

最后此篇关于MVC框架自定义实现过程的文章就讲到这里了,如果你想了解更多关于MVC框架自定义实现过程的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com