对MVC的理解以及如何实现一个简单的MVC

IOC 容器与 Controller
  • 在 Spring 框架中,所有的 Controller 都会被 Spring 的 IOC 容器管理。
  • 当应用程序启动时,Spring 会扫描所有带有 @Controller 注解的类,并将它们作为 Bean 注册到 IOC 容器中。
方法扫描与 Dispatcher
  • Spring MVC 在启动时,会扫描所有 Controller 类中的方法。
  • 根据方法上的注解(例如 @RequestMapping, @GetMapping, @PostMapping 等),Spring MVC 将这些方法与特定的 URL 模式映射,并将这些映射添加到一个中央的 DispatcherServlet 中。
参数封装
  • 每个 Controller 方法的参数以及参数上的注解(如 @RequestParam, @PathVariable, @RequestBody 等)都会被封装成 Param 对象。
  • 这些 Param 对象包含了参数的类型、名称、默认值和注解类型等信息,并被存储在 methodParameters 列表中。
请求处理
  • 当一个 HTTP 请求到达时,DispatcherServlet 会根据请求的 URL 匹配到相应的处理器(Controller 方法)。
  • DispatcherServlet 调用匹配的处理器的 process 方法来处理请求。
  • process 方法中,根据请求的具体信息,调用对应的 Controller 方法。
返回结果
  • Controller 方法的返回值会被封装成一个 Result 对象,包含了处理是否成功以及处理的具体结果。
  • Result 对象会传递给视图解析器,将结果渲染成最终的视图(如 HTML、JSON 等),并返回给客户端。

实现一个简单的MVC

定义Dispatcher

    static class Dispatcher {final static Result NOT_PROCESSED = new Result(false, null);Logger logger = LoggerFactory.getLogger(getClass());boolean isRest;boolean isResponseBody;boolean isVoid;Pattern urlPattern;Object controller;Method handlerMethod;Param[] methodParameters;public Dispatcher(String httpMethod, boolean isRest, Object controller, Method method, String urlPattern) throws ServletException {this.isRest = isRest;this.isResponseBody = method.getAnnotation(ResponseBody.class) != null;this.isVoid = method.getReturnType() == void.class;this.urlPattern = Pattern.compile(urlPattern);this.controller = controller;this.handlerMethod = method;Parameter[] param = method.getParameters();Annotation[][] paramAnnos = method.getParameterAnnotations();this.methodParameters = new Param[param.length];for (int i = 0; i < param.length; i++) {methodParameters[i] = new Param(httpMethod, method, param[i], paramAnnos[i]);}}

定义Param

    static class Param {String name;ParamType paramType;Class<?> classType;String defaultValue;/*** 对参数进行解析,并设置参数类型、参数名、参数默认值** @param httpMethod* @param method* @param parameter* @param annotations* @throws ServletException*/public Param(String httpMethod, Method method, Parameter parameter, Annotation[] annotations) throws ServletException {PathVariable pv = ClassUtils.getAnnotaion(annotations, PathVariable.class);RequestParam rq = ClassUtils.getAnnotaion(annotations, RequestParam.class);RequestBody rb = ClassUtils.getAnnotaion(annotations, RequestBody.class);int total = (pv == null ? 0 : 1) + (rq == null ? 0 : 1) + (rb == null ? 0 : 1);if (total > 1) {throw new ServletException("Only one annotation can be used in a parameter." + method);}this.classType = parameter.getType();if (pv != null) {this.name = pv.value();this.paramType = ParamType.PATH_VARIABLE;} else if (rq != null) {this.name = rq.value();this.defaultValue = rq.defaultValue();this.paramType = ParamType.REQUEST_PARAM;} else if (rb != null) {this.paramType = ParamType.REQUEST_BODY;} else {this.paramType = ParamType.SERVLET_VARIABLE;if (this.classType != HttpServletRequest.class && this.classType != HttpServletResponse.class &&this.classType != HttpSession.class && this.classType != ServletContext.class) {throw new ServerErrorException("请给参数标记注解,不支持的参数类型 " + classType + " at method " + method);}}}

定义处理结果

    static record Result(boolean processed, Object returnObject) {}

初始化

   @Overridepublic void init(ServletConfig config) throws ServletException {logger.info("init{}.", getClass().getName());ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) this.applicationContext;List<BeanDefinition> beanDefinitions = configurableApplicationContext.findBeanDefinitions(Object.class);for (BeanDefinition def : beanDefinitions) {Class<?> beanClass = def.getBeanClass();Object bean = def.getRequiredInstance();Controller controller = beanClass.getAnnotation(Controller.class);RestController restController = beanClass.getAnnotation(RestController.class);if (controller != null && restController != null) {throw new ServletException("Controller and RestController can not be used at the same time." + beanClass.getName());}if (controller != null) {addController(false, def.getName(), bean);}else{addController(true, def.getName(), bean);}}}

doService

    void doService(String url, HttpServletRequest req, HttpServletResponse resp, List<Dispatcher> dispatchers) throws Exception {for (Dispatcher dispatcher : dispatchers){Result result = dispatcher.process(url, req, resp);if(result.processed()){Object r = result.returnObject();if(dispatcher.isRest){if(!resp.isCommitted()){resp.setContentType("application/json;charset=UTF-8");}if(dispatcher.isResponseBody){if(r instanceof String s){PrintWriter pw = resp.getWriter();pw.write(s);pw.flush();}else if(r instanceof byte[] data){ServletOutputStream output = resp.getOutputStream();output.write(data);output.flush();}else{throw new ServerErrorException("Unsupported return type: " + r.getClass());}}}else{if( !resp.isCommitted()){resp.setContentType("text/html");}if( r instanceof String s){if (dispatcher.isResponseBody) {// send as response body:PrintWriter pw = resp.getWriter();pw.write(s);pw.flush();} else if (s.startsWith("redirect:")) {// send redirect:resp.sendRedirect(s.substring(9));} else {// error:throw new ServletException("Unable to process String result when handle url: " + url);}} else if (r instanceof byte[] data) {if (dispatcher.isResponseBody) {// send as response body:ServletOutputStream output = resp.getOutputStream();output.write(data);output.flush();} else {// error:throw new ServletException("Unable to process byte[] result when handle url: " + url);}}else if(r instanceof ModelAndView mv){String view = mv.getViewName();if (view.startsWith("redirect:")) {// send redirect:resp.sendRedirect(view.substring(9));} else {this.viewResolver.render(view, mv.getModel(), req, resp);}} else if (!dispatcher.isVoid && r != null) {// error:throw new ServletException("Unable to process " + r.getClass().getName() + " result when handle url: " + url);}}}return;}}

详细代码地址
https://github.com/laicoffee/Spring-Summer

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/367185.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Qt自定义信号

1.Teacher类下定义信号signals: Student类下定义槽函数&#xff1a; Teacher.h #pragma once#include <QObject>class Teacher : public QObject {Q_OBJECTpublic:Teacher(QObject *parent);~Teacher(); signals:void Ask(); //老师向学生提问void Ask(QString str);…

WebStorm配置路径别名(jsconfig.json)

项目是 ViteVueTs 新建一个 jsconfig.json文件 {"compilerOptions": {"baseUrl": ".","paths": {"/*": ["./src/*"]}},"exclude": ["node_modules", "dist"] }然后在 vite.confi…

C语言的数据结构:图的基本概念

前言 之前学过了其它的数据结构&#xff0c;如&#xff1a; 集合 \color{#5ecffd}集合 集合 —— 数据元素属于一个集合。 线型结构 \color{#5ecffd}线型结构 线型结构 —— 一个对一个&#xff0c;如线性表、栈、队列&#xff0c;每一个节点和其它节点之间的关系 一个对一个…

燃料电池混合电源的能量管理系统

这个例子显示了燃料电池混合电源的能量管理系统。 这个例子展示了燃料电池混合电源的能量管理系统。 电路描述 本文给出了基于燃料电池的多电动飞机应急动力系统的仿真模型。随着MEA中起落架和飞控系统的电气化程度的提高&#xff0c;常规应急电源系统(冲压式空气涡轮或空气驱…

01:Linux的基本命令

Linux的基本命令 1、常识1.1、Linux的隐藏文件1.2、绝对路径与相对路径 2、基本命令2.1、ls2.2、cd2.3、pwd / mkdir / mv / touch / cp / rm / cat / rmdir2.4、ln2.5、man2.6、apt-get 本教程是使用的是Ubuntu14.04版本。 1、常识 1.1、Linux的隐藏文件 在Linux中&#xf…

【ROS中Cjson文件的作用】

在ROS (Robot Operating System) 中&#xff0c;.json 文件通常用于存储配置信息、数据序列化或者在某些情况下用于网络通信和数据交换。JSON&#xff08;JavaScript Object Notation&#xff09;是一种轻量级的数据交换格式&#xff0c;易于人阅读和编写&#xff0c;同时也易于…

【WebGIS干货分享】Webgis 面试题-浙江中海达

1、Cesium 中有几种拾取坐标的方式&#xff0c;分别介绍 Cesium 是一个用于创建 3D 地球和地理空间应用的 JavaScript 库。在 Cesium 中&#xff0c;你可以使用不同的方式来拾取坐标&#xff0c;以便与地球或地图上的对象进行交 互。以下是 Cesium 中几种常见的拾取坐标的方式…

深入理解C++中的锁

目录 1.基本互斥锁&#xff08;std::mutex&#xff09; 2.递归互斥锁&#xff08;std::recursive_mutex&#xff09; 3.带超时机制的互斥锁&#xff08;std::timed_mutex&#xff09; 4.带超时机制的递归互斥锁&#xff08;std::recursive_timed_mutex&#xff09; 5.共享…

[论文阅读笔记33] Matching Anything by Segmenting Anything (CVPR2024 highlight)

这篇文章借助SAM模型强大的泛化性&#xff0c;在任意域上进行任意的多目标跟踪&#xff0c;而无需任何额外的标注。 其核心思想就是在训练的过程中&#xff0c;利用strong augmentation对一张图片进行变换&#xff0c;然后用SAM分割出其中的对象&#xff0c;因此可以找到一组图…

网络爬虫基础知识

文章目录 网络爬虫基础知识爬虫的定义爬虫的工作流程常用技术和工具爬虫的应用1. 抓取天气信息2. 抓取新闻标题3. 抓取股票价格4. 抓取商品价格5. 抓取博客文章标题 网络爬虫基础知识 爬虫的定义 网络爬虫&#xff08;Web Crawler 或 Spider&#xff09;是一种自动化程序&…

《企业实战分享 · 常用运维中间件》

&#x1f4e2; 大家好&#xff0c;我是 【战神刘玉栋】&#xff0c;有10多年的研发经验&#xff0c;致力于前后端技术栈的知识沉淀和传播。 &#x1f497; &#x1f33b; 近期刚转战 CSDN&#xff0c;会严格把控文章质量&#xff0c;绝不滥竽充数&#xff0c;如需交流&#xff…

HUAWEI MPLS 静态配置和动态LDP配置

MPLS(Multi-Protocol Label Switching&#xff0c;多协议标签交换技术)技术的出现&#xff0c;极大地推动了互联网的发展和应用。例如&#xff1a;利用MPLS技术&#xff0c;可以有效而灵活地部署VPN(Virtual Private Network&#xff0c;虚拟专用网)&#xff0c;TE(Traffic Eng…

a-range-picker 国际化不生效

1、问题&#xff1a;按照官方 添加后是这样的 周和月没有翻译 1-1官方配置如下图 1-2效果&#xff1a; 2、import locale from "ant-design-vue/es/date-picker/locale/zh_CN"; 打印出locale是这样的 这个文件翻译文件中没有相关翻译 3、解决&#xff1a; 简单粗…

【实战场景】记一次UAT jvm故障排查经历

【实战场景】记一次UAT jvm故障排查经历 开篇词&#xff1a;干货篇&#xff1a;1.查看系统资源使用情况2.将十进制进程号转成十六进制3.使用jstack工具监视进程的垃圾回收情况4.输出指定线程的堆内存信息5.观察日志6.本地环境复现 总结篇&#xff1a;我是杰叔叔&#xff0c;一名…

Objective-C使用块枚举的细节

对元素类型的要求 在 Objective-C 中&#xff0c;NSArray 只能存储对象类型&#xff0c;而不能直接存储基本类型&#xff08;例如 int&#xff09;。但是&#xff0c;可以将基本类型封装在 NSNumber 等对象中&#xff0c;然后将这些对象存储在 NSArray 中。这样&#xff0c;en…

H6922 便携移动储能升压恒压方案 2.8-40V耐压 7.5A大电流应用芯片IC

H6922芯片是一款便携移动储能升压恒压控制驱动芯片&#xff0c;满足2.8-40V宽输入电压范围的升压恒压电源应用而设计。下面我将基于您提供的信息&#xff0c;对H6922的特性和典型应用进行更详细的解释。 产品特性详解 宽输入电压&#xff1a;H6922支持2.8-40V的宽输入电压范围…

【Windows】Visual Studio Installer下载缓慢解决办法

【Windows】Visual Studio Installer下载缓慢解决办法 1.背景2.分析3.结果 1.背景 使用visual studio在线安装包进行IDE安装&#xff0c;发现下载几乎停滞&#xff0c;网速几乎为零。 经过排查并不是因为实际网络带宽导致。 这里涉及DNS知识&#xff1b; DNS&#xff08;Dom…

SCI丨5分期刊,JCR一区

SCI&#xff0c;5分&#xff0c;JCR Q1&#xff0c;中科大类3小类2区 1 基于复杂网络与xxx能源汽车节能数值分析 2 基于热能损失优化的xxx与性能管理 3 基于xxxLCA技术的绿色制造工艺优化研究 4 基于xxx入侵检测技术的物联网智能制造监控系统设计 6 基于物联网技术xxx电力系…

BMP280 环境传感器

型号简介 BMP280是博世&#xff08;bosch-sensortec&#xff09;的一款气压传感器&#xff0c;特别适用于移动应用。其小尺寸和低功耗使其能够应用于电池供电的设备&#xff0c;如手机、GPS 模块或手表。基于博世久经考验的压阻式压力传感器技术&#xff0c;具有高精度和线性度…

Elasticsearch备份数据到本地,并导入到新的服务 es 服务中

文章目录 使用elasticsearch-dump工具备份安装node.js(二进制安装)解压设置环境变量安装elasticsearch-dump docker安装使用ES备份文件到本地 使用elasticsearch-dump工具备份 这个工具备份时间比较长 安装node.js(二进制安装) wget https://nodejs.org/dist/v16.18.0/node-…