SpringMVC 源码学习 返回值处理

news2025/2/26 4:40:37

SpringMVC中对返回值的数据基本分为两类:

        1、响应数据

        2、响应页面

一、响应数据

        响应数据大多数都是将返回值的格式转换为JSON格式然后展示在页面或者保存i起来。

        第一步:在SpringBoot中需要引入json场景

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-json</artifactId>
      <version>2.3.4.RELEASE</version>
      <scope>compile</scope>
    </dependency>

        第二步:开启@ResponseBody注解

        之后再写学习记录。

二、响应页面

        第一个疑问:再创建Springboot项目时,spring Initializr说连接不到 URL,所以用Maven创建WebApp。但是之后写Demo的时候发现再SpringBoot项目下,webapp/WEB-INF/templates是查询不到的,报500错误,只能再resource下写templates才能访问页面,通过源码学习看看是为什么。

        Demo:

        控制器:

@Controller
public class helloController {

    @GetMapping("/index")
    public String helloTest(Model model){
        String msg = "thymeleaf渲染了";
        model.addAttribute("msg",msg);
        return "test";
    }
}

        HTML页面:

<body>
    <p th:text="${msg}">thymeleaf没渲染!!!!</p>
</body>

        结果:

 1、源码学习

        

         这一段中有上面是学习过的参数解析器,下面是返回值解析器

        这个返回值解析器中共有15种,之前响应数据并转换为JSON格式返回给页面的就是序号为11的解析器。

        

        然后到执行这一步

         这里获取到了返回值

 

         所以还是看这个方法:

    @Nullable
    public Object invokeForRequest(NativeWebRequest request, @Nullable ModelAndViewContainer mavContainer, Object... providedArgs) throws Exception {
        Object[] args = this.getMethodArgumentValues(request, mavContainer, providedArgs);
        if (this.logger.isTraceEnabled()) {
            this.logger.trace("Arguments: " + Arrays.toString(args));
        }

        return this.doInvoke(args);
    }

        这个方法内部为:第一行获取控制器方法中的参数

        最后通过反射调用控制器的方法。(再方法体内打了个断点)

public void invokeAndHandle(ServletWebRequest webRequest, ModelAndViewContainer mavContainer, Object... providedArgs) throws Exception {
        Object returnValue = this.invokeForRequest(webRequest, mavContainer, providedArgs);
......

        try {
            this.returnValueHandlers.handleReturnValue(returnValue, this.getReturnValueType(returnValue), mavContainer, webRequest);
        } catch (Exception var6) {
            if (this.logger.isTraceEnabled()) {
                this.logger.trace(this.formatErrorForReturnValue(returnValue), var6);
            }

......

        }
    }

           接着就是再try中去处理返回值,看看handleReturnValue方法

public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
        HandlerMethodReturnValueHandler handler = this.selectHandler(returnValue, returnType);
        if (handler == null) {
            throw new IllegalArgumentException("Unknown return value type: " + returnType.getParameterType().getName());
        } else {
            handler.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
        }
    }

        执行第一步之后我们得到了返回值解析器为ViewNameMethod...Handler,说明使用Thymeleaf响应页面的时候都是使用该解析器。

         这个选择过程和之前选择对应的解析器的方法是一样的,都是一个接口,一个判断,一个执行。

        接着找到了返回值解析器之后,会执行这个方法

        这个方法内部为:

    public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
        if (returnValue instanceof CharSequence) {
            String viewName = returnValue.toString();
            mavContainer.setViewName(viewName);
            if (this.isRedirectViewName(viewName)) {
                mavContainer.setRedirectModelScenario(true);
            }
        } else if (returnValue != null) {
            throw new UnsupportedOperationException("Unexpected return type: " + returnType.getParameterType().getName() + " in method: " + returnType.getMethod());
        }

    }

        可以看到这里是判断你是不是重定向的场景的,如果是重定向就再这里会处理。目前的返回值只是一个test,通过thymeleaf渲染为 xx/test.html,所以不经过这一步。

        执行完了之后mavContainer中包含了model和view

     mavContainer中将 给model中设置的值以及最后响应的view都保存再这里了。

         并且,如果方法中的参数也是一个自定义类型的对象,也会再这了放到mavContainer中。

        最后赋给变量var15也是这些数据

        之后再DispatcherServlet中获取到了model 和view中的值

         applyDefaultViewName是如果你返回值是空的,就给你设置一个默认的视图地址,这个默认的地址就是一开始访问的地址。比如说访问的url为 "/login" 但返回值为空,默认的视图地址就还是 “/login”。

        之后就是再这里处理派发结果,也就是视图解析的原理:

         方法内部如下:

    private void processDispatchResult(HttpServletRequest request, HttpServletResponse response, @Nullable HandlerExecutionChain mappedHandler, @Nullable ModelAndView mv, @Nullable Exception exception) throws Exception {
        boolean errorView = false;
        if (exception != null) {
            if (exception instanceof ModelAndViewDefiningException) {
                this.logger.debug("ModelAndViewDefiningException encountered", exception);
                mv = ((ModelAndViewDefiningException)exception).getModelAndView();
            } else {
                Object handler = mappedHandler != null ? mappedHandler.getHandler() : null;
                mv = this.processHandlerException(request, response, handler, exception);
                errorView = mv != null;
            }
        }

        if (mv != null && !mv.wasCleared()) {
            this.render(mv, request, response);
            if (errorView) {
                WebUtils.clearErrorRequestAttributes(request);
            }
        } else if (this.logger.isTraceEnabled()) {
            this.logger.trace("No view rendering, null ModelAndView returned.");
        }

        if (!WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) {
            if (mappedHandler != null) {
                mappedHandler.triggerAfterCompletion(request, response, (Exception)null);
            }

        }
    }

        其中先判断是否有异常或者有啥问题,没有的话就会执行这个render

render方法内部:

    protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Locale locale = this.localeResolver != null ? this.localeResolver.resolveLocale(request) : request.getLocale();
        response.setLocale(locale);
        String viewName = mv.getViewName();
        View view;
        if (viewName != null) {
            view = this.resolveViewName(viewName, mv.getModelInternal(), locale, request);
            if (view == null) {
                throw new ServletException("Could not resolve view with name '" + mv.getViewName() + "' in servlet with name '" + this.getServletName() + "'");
            }
        } else {
            view = mv.getView();
            if (view == null) {
                throw new ServletException("ModelAndView [" + mv + "] neither contains a view name nor a View object in servlet with name '" + this.getServletName() + "'");
            }
        }

        if (this.logger.isTraceEnabled()) {
            this.logger.trace("Rendering view [" + view + "] ");
        }

        try {
            if (mv.getStatus() != null) {
                response.setStatus(mv.getStatus().value());
            }

            view.render(mv.getModelInternal(), request, response);
        } catch (Exception var8) {
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Error rendering view [" + view + "]", var8);
            }

            throw var8;
        }
    }

        可以看到如果 mv.getViewName()获取到的值不为空就会执行这个解析视图名称的过程。

         接着再resolveViewName中,有一个视图解析器viewResolvers,也是springMVC准备好的。

    @Nullable
    protected View resolveViewName(String viewName, @Nullable Map<String, Object> model, Locale locale, HttpServletRequest request) throws Exception {
        if (this.viewResolvers != null) {
            Iterator var5 = this.viewResolvers.iterator();

            while(var5.hasNext()) {
                ViewResolver viewResolver = (ViewResolver)var5.next();
                View view = viewResolver.resolveViewName(viewName, locale);
                if (view != null) {
                    return view;
                }
            }
        }

        return null;
    }

通过这个视图解析器看哪个匹配获取到view对象。 

最后选择的解析器是ContentNegotiatingViewResolver,在这个解析器的基础下选择了Thymeleaf视图对象。

 现在具体看看是怎么获取到这个视图对象的。

        调用了ContentNegotiatingViewResolver.resolveViewName这个方法

    @Nullable
    public View resolveViewName(String viewName, Locale locale) throws Exception {
       .......
        if (requestedMediaTypes != null) {
            List<View> candidateViews = this.getCandidateViews(viewName, locale, requestedMediaTypes);
            View bestView = this.getBestView(candidateViews, requestedMediaTypes, attrs);
            if (bestView != null) {
                return bestView;
            }
        }

        这里有 获取候选解析器和选择一个最合适的解析器。

    private List<View> getCandidateViews(String viewName, Locale locale, List<MediaType> requestedMediaTypes) throws Exception {
        List<View> candidateViews = new ArrayList();
        if (this.viewResolvers != null) {
            Assert.state(this.contentNegotiationManager != null, "No ContentNegotiationManager set");
            Iterator var5 = this.viewResolvers.iterator();

            while(var5.hasNext()) {
                ViewResolver viewResolver = (ViewResolver)var5.next();
                View view = viewResolver.resolveViewName(viewName, locale);
                if (view != null) {
                    candidateViews.add(view);
                }

    ......
    }

       最后有两个满足:

         所以先看看ThymeleafResolver.resolveViewName

    @Nullable
    public View resolveViewName(String viewName, Locale locale) throws Exception {
        if (!this.isCache()) {
            return this.createView(viewName, locale);
        } else {
            Object cacheKey = this.getCacheKey(viewName, locale);
            View view = (View)this.viewAccessCache.get(cacheKey);
            if (view == null) {
                synchronized(this.viewCreationCache) {
                    view = (View)this.viewCreationCache.get(cacheKey);
                    if (view == null) {
                        view = this.createView(viewName, locale);
                        if (view == null && this.cacheUnresolved) {
                            view = UNRESOLVED_VIEW;
                        }

                        if (view != null && this.cacheFilter.filter(view, viewName, locale)) {
                            this.viewAccessCache.put(cacheKey, view);
                            this.viewCreationCache.put(cacheKey, view);
                        }
                    }
                }
            } else if (this.logger.isTraceEnabled()) {
                this.logger.trace(formatKey(cacheKey) + "served from cache");
            }

            return view != UNRESOLVED_VIEW ? view : null;
        }
    }

这里创建了一个View ,具体代码为:

   protected View createView(String viewName, Locale locale) throws Exception {
        if (!this.alwaysProcessRedirectAndForward && !this.canHandle(viewName, locale)) {
            vrlogger.trace("[THYMELEAF] View \"{}\" cannot be handled by ThymeleafViewResolver. Passing on to the next resolver in the chain.", viewName);
            return null;
        } else {
            String forwardUrl;
            if (viewName.startsWith("redirect:")) {
                vrlogger.trace("[THYMELEAF] View \"{}\" is a redirect, and will not be handled directly by ThymeleafViewResolver.", viewName);
                forwardUrl = viewName.substring("redirect:".length(), viewName.length());
                RedirectView view = new RedirectView(forwardUrl, this.isRedirectContextRelative(), this.isRedirectHttp10Compatible());
                return (View)this.getApplicationContext().getAutowireCapableBeanFactory().initializeBean(view, viewName);
            } else if (viewName.startsWith("forward:")) {
                vrlogger.trace("[THYMELEAF] View \"{}\" is a forward, and will not be handled directly by ThymeleafViewResolver.", viewName);
                forwardUrl = viewName.substring("forward:".length(), viewName.length());
                return new InternalResourceView(forwardUrl);
            } else if (this.alwaysProcessRedirectAndForward && !this.canHandle(viewName, locale)) {
                vrlogger.trace("[THYMELEAF] View \"{}\" cannot be handled by ThymeleafViewResolver. Passing on to the next resolver in the chain.", viewName);
                return null;
            } else {
                vrlogger.trace("[THYMELEAF] View {} will be handled by ThymeleafViewResolver and a {} instance will be created for it", viewName, this.getViewClass().getSimpleName());
                return this.loadView(viewName, locale);
            }
        }
    }

        先判断能否处理,然后判断是不是以重定向开头或者是转发开头的,目前的请求是"/test",所以都不是,所以直接进入else这里加载视图,最后返回了了 一个ThymeleafView对象。

        返回的第二个对象叫内部资源视图,这个暂时不清楚怎么用,先空下来。

        然后再这两个view对象选最合适的最终获取到了ThymeleafView对象,最后响应页面也就是前面提到了使用 view对象的render方法。

        也就是在

         render方法中获取到了一个ThymeleafView 对象

        最后在这里调用这个视图的render方法

 

         进入这个方法是:

         等于说在执行renderFragment这个方法。

        最后在这个方法里找到了熟悉的那句话:

         

目前为止还没有解决在SpringBoot类型项目下为什么只能在resources下放文件才能被找到。

但在网上查找资料以及文档的时候看到这两个标签:

 

        想起来SpringBoot项目默认的打包方式为jar包。

所以在生成的target文件中并没有WEB-INF这个东西,也就是说webapp下的东西没有被打包,所以当然无法访问。

        当想改变thymeleaf的解析的路径时可以改它的前缀值就行,但是若想和springMVC中一样把页面写道webapp下打包 只能以war包形式打包。

 

springboot的war包形式打包:

SpringBoot项目打包成war包并部署到服务器上_lc11535的博客-CSDN博客_springboot项目打包war

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

如何设计存储架构

步骤 步骤1&#xff1a;估算性能需求 任务 基于具体的业务场景来估算性能需求&#xff0c;包括存储量、读写性能等 挑战 不知道如何估算担心估算不准 步骤2&#xff1a;选择存储系统 任务 根据技术储备、方案优缺点选择合适的存储系统 挑战 不知道有哪些存储系统知道…

UG NX二次开发(C#)-UI Styler-批量选择点

1、前言 在设计UG NX的对话框界面时,有时需要选择点,在UI Styler编辑器中已经设置好了可以指定点选择按钮,这个点选择对话框是单选,不是多选的,如果想选择多个点,那么可以采用对象选择按钮,本文介绍下选择点的操作。 2、指定点按钮 2.1 设计UI Styler UG NX的UI Sty…

vscode中Emmet语法的使用

每篇博文的浪漫主义 【镰仓旅拍|落日绝景 极致画质|沉浸式旅行 FX3】 https://www.bilibili.com/video/BV1jg411Y7vC/?share_sourcecopy_web&vd_source385ba0043075be7c24c4aeb4aaa73352 镰仓旅拍|落日绝景 极致画质|沉浸式旅行 FX31.1快速生成HTML结构语法 生成标签直接…

想要彻底卸载Mac应用程序,还得要用这些方法才行

Mac电脑如果有太多无用的应用程序&#xff0c;很有可能会拖垮Mac系统的运行速度。因此&#xff0c;卸载电脑中无用的软件是优化Mac系统运行速度的最佳方式之一。Mac删除应用程序特别简单&#xff0c;长点击应用点击x&#xff0c;或是直接将应用拖进废纸篓。但是有一些应用长按没…

Qt——(详细)“项目在Debug构建环境下能运行而在Release构建环境下不能运行”解决方案之一,以及 禁用(黄色)警告

系列文章目录 提示&#xff1a; 文章目录系列文章目录前言环境一、问题准备工作——为了在Release环境下可以进行断点调试分析二、解决1、根据需求&#xff0c;对函数类型进行更改2、根据需求&#xff0c;在函数内添加“return [int]”延伸——“禁用警告”消除 变量 的“黄色感…

现代修谱,如何看待支系单飞的现象?

族谱与支谱、房谱的区别 现代修谱&#xff0c;修的是什么谱&#xff0c;你知道吗&#xff1f;其实现代修谱的种类有很多种&#xff0c;有支谱、房谱、族谱、宗谱、统谱、通谱等等&#xff0c;而这些在生活中都被我们简称为家谱。 不过在现代修谱里&#xff0c;宗谱、统谱、通谱…

极智编程 | 谈谈 C++ 中容器 map 和 unordered_map 的区别

欢迎关注我的公众号 [极智视界]&#xff0c;获取我的更多笔记分享 大家好&#xff0c;我是极智视界&#xff0c;本文来 谈谈 C 中 map 和 unordered_map 的区别。 map 和 unordered_map 都可以看做是一种 key-value 的映射关系&#xff0c;unordered_map 可以理解为 无序版的ma…

C语言 数组

C语言 数组一、一维数组1. 数组的创建方式程序清单1程序清单22. 计算数组的元素的个数3. 数组在内存中的存储方式二、二维数组1. 二维数组的创建方式2. 计算二维数组的行和列3. 二维数组在内存中的存储方式三、数组名的含义总结数组名的应用场景数组名作为函数参数一、一维数组…

22.11.16打卡 mysql学习笔记

马上要考试了, 越到考试越想玩, 烦躁烦躁烦躁, 没学多少, 争取明天把mysql基础篇学完 DCL介绍 2022年11月16日 14:54 DCL主要用来处理数据库有哪些用户可以访问, 每个用户具有什么样的权限 用户管理 2022年11月16日 15:20 所有用户的数据都存放在系统数据库mysql中的user表…

Python是什么?要如何学习?

Python 是荷兰人 Guido van Rossum &#xff08;吉多范罗苏姆&#xff0c;中国程序员称其为“龟叔”&#xff09;在 1990 年初开发的一种解释型编程语言。 Python 的诞生是极具戏曲性的&#xff0c;据 Guido 自述记载&#xff0c;Python 语言是在圣诞节期间为了打发无聊的时间而…

基于全景相机的视觉里程计算法研究

一、视觉里程计 视觉里程计技术首先建立相机的成像模型&#xff0c;接着通过标定算法计算相机参数&#xff0c;最后建立相邻图像的关联并估计相机运动轨迹。 1.1相机在空间中运动的描述 描述相机在三维空间中的运动状态&#xff0c;即求解相机在空间中不同时刻下的位姿关系。相…

假冒网站引发多重安全风险 | 官方严正声明:切勿在非官方渠道购买或下载 Navicat 软件

Navicat 严正声明 近期&#xff0c;有关于 Navicat 假冒网站的事件&#xff0c;不法分子通过仿制官方网站&#xff0c;诱导用户下载盗版软件。Navicat 官方已正式向国内监管部门举报&#xff0c;提请将该不法网站下架。目前&#xff0c;监管部门已介入调查中。 我司呼吁广大用…

这次,听人大教授讲讲分布式数据库的多级一致性|TDSQL 关键技术突破

近年来&#xff0c;凭借高可扩展、高可用等技术特性&#xff0c;分布式数据库正在成为金融行业数字化转型的重要支撑。分布式数据库如何在不同的金融级应用场景下&#xff0c;在确保数据一致性的前提下&#xff0c;同时保障系统的高性能和高可扩展性&#xff0c;是分布式数据库…

C语言之详解内存操作函数

个人主页&#xff1a;平行线也会相交 欢迎 点赞&#x1f44d; 收藏✨ 留言✉ 加关注&#x1f493;本文由 平行线也会相交 原创 收录于专栏【C/C】 目录前言memcpy模拟实现memmove模拟实现memcmpmemset前言 memcpy叫做内存拷贝&#xff0c;memmove叫做内存移动&#xff0c;memc…

【数据结构】模拟实现双向链表

你必须非常努力&#xff0c;才能显得毫不费劲 目录 1.模拟实现双向链表 1.1 DLinkedList的内部类 1.2 DLinkedList的成员属性 1.3 DLinkedList的成员方法 1.3.1 在链表开头插入一个新结点 1.3.2 在链表结尾插入一个新的结点 1.3.3 计算结点个数 1.3.4 在链表任意位置…

4.构造器,this,修饰符详解

构造器&#xff1a; 构造器也叫构造方法&#xff0c;无返回值。非构造方法必须要有返回类型 主要作用&#xff1a;完成对象的初始化&#xff0c;创造对象时&#xff0c;自动调用构造器初始化对象 即使没有显示地使用static关键字&#xff0c;构造器实际上也是静态方法 JAVA…

HTML---基础入门知识详解

1&#xff1a;标签的概念 在别人写的网页中我们会看到许多文字&#xff0c;图片排版整齐&#xff0c;让人看的赏心悦目&#xff0c;这就是用到了标签&#xff0c;或者说标签就是帮我们实现某种作用的工具&#xff0c;比如制作段落&#xff0c;换行&#xff0c;导入图片&#x…

Android App 导出APK安装包以及制作App图标讲解及实战(图文解释 简单易懂)

操作有问题请点赞关注收藏后评论区留言~~~ 一、导出APK安装包 之前在运行App的时候&#xff0c;都是先由数据线连接手机和电脑&#xff0c;再通过Android Studio的Run菜单把App安装到手机上&#xff0c;这种方式只能在自己手机上调试应用&#xff0c;如果想在别人手机上安装应…

Python画爱心——谁能拒绝用代码敲出会跳动的爱心呢~

还不快把这份浪漫拿走&#xff01;&#xff01;节日就快到来了&#xff0c;给Ta一个惊喜吧~ 今天给大家分享一个浪漫小技巧&#xff0c;利用Python中的 HTML 制作一个立体会动的心动小爱心 成千上百个爱心汇成一个大爱心&#xff0c;从里到外形成一个立体状&#xff0c;给人视…

FITC标记SPG,FITC-SPG,荧光素标记链球菌G蛋白

产品名称&#xff1a;FITC标记SPG&#xff0c;荧光素标记链球菌G蛋白 英文名称&#xff1a;FITC-SPG 纯度&#xff1a;98% 规格&#xff1a;1mg 5mg 10mg 产地&#xff1a;西安 说明&#xff1a;提供使用说明&#xff0c;核磁图谱&#xff0c;包装&#xff0c;价格&#xff0…