SpringMVC 源码学习 返回值处理

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的解析器。

        

        然后到执行这一步

         这里获取到了返回值

 

         所以还是看这个方法:

    @Nullablepublic 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准备好的。

    @Nullableprotected 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这个方法

    @Nullablepublic 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

    @Nullablepublic 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包形式打包。

巩固:

        这些文件的位置配置 一般都有对应的AutoConfiguration,在其中有 properties可以查看这些配置,比如上边提到的thymeleaf的模板默认路径:

  

springboot的war包形式打包:

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

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

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

相关文章

洛谷10月月赛Round.1| P3399 丝绸之路 [DP]

题目背景 张骞于公元前138年曾历尽艰险出使过西域。加强了汉朝与西域各国的友好往来。从那以后&#xff0c;一队队骆驼商队在这漫长的商贸大道上行进&#xff0c;他们越过崇山峻岭&#xff0c;将中国的先进技术带向中亚、西亚和欧洲&#xff0c;将那里的香料、良马传进了我国。…

body div js 放大图片_jquery图片放大插件鼠标悬停图片放大效果

都知道jquery都插件是非常强大的&#xff0c;最近分享点jquery插件效果&#xff0c;方便效果开发使用。一、HTML代码<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> …

MySQL数据库的优化

MySQL数据库的优化 http://www.vpser.net/opt/vps-mysql-opt.html转载于:https://www.cnblogs.com/carbon3/p/5930868.html

我的博客html

我——龙天宇 <table><tr><td width150 align"right"><DIV style"FILTER: Glow(color#4A7AC9,strength50); WIDTH: 149px; HEIGHT: 119px" aligncenter><br><img height89 width119 src"http://img.blog.163.com/p…

预览docx_Windows-快速预览文件-QuickLook

开源、免费的文件快速预览工具&#xff0c; 支持图片、文档、音视频、代码文本、压缩包等多种格式。获得 Mac OS 空格键快速预览文件相同的体验效果图文件夹音视频 浏览压缩包&#xff0c;文本支持的格式&#xff1a;图片&#xff1a;.png, .jpg, .bmp, .gif, .psd, .apng&…

html简单样式

1.外部样式表 link rel"stylesheet" type"text/css" href"bbb.css"> 2.内部样式表 <style type"text/css"> p{ color: bisque; }</style> 3.内联样式表 <a style"color: blueviolet">hhh…

json 微信小程序 筛选_微信小程序学习记录

全局配置app.json 文件用来对微信小程序进行全局配置。pages 类型为 String Array 是 页​面路径列表&#xff0c;创建目录和更改时会自动更改文件。用于指定小程序由哪些页面组成&#xff0c;每一项都对应一个页面的 路径文件名 信息。window 用于设置小程序的状态栏、导航条、…

前端学习(2146):vue中TypeError: this.getResolve is not a function

可能是加载顺序的问题 const path require(path) module.exports {entry: ./src/main.js,output: {path: path.resolve(__dirname, dist),filename: bund.js},module: {rules: [{test: /\.css$/,use: [style-loader, style-loader]}]} }

驱动思想之机制和策略

驱动程序的角色 作为一个程序员, 你能够对你的驱动作出你自己的选择, 并且在所需的编程时间和结果的灵活性之间, 选择一个可接受的平衡. 尽管说一个驱动是"灵活"的, 听起来有些奇怪, 但是我们喜欢这个字眼, 因为它强调了一个驱动程序的角色是提供机制, 而不是策略. 机…

vba 执行网页javascript_JavaScript秘密笔记 第一集

1. 什么是JavaScript2. 如何使用JavaScript3. *变量4. *数据类型谁记得笔记越多&#xff0c;谁学的越烂&#xff01;1. 什么是JavaScript:前端三大语言:HTML: 专门编写网页内容的语言CSS: 专门编写网页样式的语言问题: 使用HTML和CSS做出的网页&#xff0c;只能看不能用——静态…

文字

标题&#xff1a; 标题的大小一共有六种&#xff0c;两个标签一组&#xff0c;也就是从<h1>到<h6>&#xff0c;<h1>最大&#xff0c;<h6>最小。使用标题标签时&#xff0c;该标签会将字体变成粗体字&#xff0c;并且会自成一行。 一般&#xff1a; <…

一定质量的封闭气体被压缩后_多晶硅氯氢化装置补充氢隔膜压缩机十字头铜套磨损原因分析与改善探讨...

潘祝新&#xff0c;王永(江苏中能硅业科技发展有限公司&#xff0c;江苏徐州221004)[摘要]&#xff1a;补充氢气压缩机是多晶硅行业氯氢化装置中的关键设备&#xff0c;它为生产系统的稳定运行不断的补充高纯氢气&#xff0c;其稳定而高效的运行对于生产系统的稳定性及提高氯硅…

vue 生成发布包_年轻人如何从0到1封装发布一个vue组件__Vue.js

封装发布组件是前端开发中非常重要的能力&#xff0c;通过对常用组件的封装可以提升团队开发的效率&#xff0c;避免重复劳作且不方便维护。好的组件的抽象和封装能让组件得到更广泛和多环境兼容的应用。本文讲述了如何一步步从0到1封装发布一个常用的toast组件的过程。本文是搭…

指针选择排序法,10个整数从小到大排序

//指针方法&#xff0c;选择排序法对10个int按从小到大排列 #include<stdio.h> main() {int n10,i,b,a[10],*p;int sort(int *q,int n);// scanf("%d",&10);for(pa;p<a10;p)//键盘输入数组元素scanf("%d",p);pa;//超重要!!!!!!!!!!不能忘sort…