• SpringMVC 源码学习 返回值处理


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

            1、响应数据

            2、响应页面

    一、响应数据

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

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

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-json</artifactId>
    4. <version>2.3.4.RELEASE</version>
    5. <scope>compile</scope>
    6. </dependency>

            第二步:开启@ResponseBody注解

            之后再写学习记录。

    二、响应页面

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

            Demo:

            控制器:

    1. @Controller
    2. public class helloController {
    3. @GetMapping("/index")
    4. public String helloTest(Model model){
    5. String msg = "thymeleaf渲染了";
    6. model.addAttribute("msg",msg);
    7. return "test";
    8. }
    9. }

            HTML页面:

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

            结果:

     1、源码学习

            

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

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

            

            然后到执行这一步

             这里获取到了返回值

     

             所以还是看这个方法:

    1. @Nullable
    2. public Object invokeForRequest(NativeWebRequest request, @Nullable ModelAndViewContainer mavContainer, Object... providedArgs) throws Exception {
    3. Object[] args = this.getMethodArgumentValues(request, mavContainer, providedArgs);
    4. if (this.logger.isTraceEnabled()) {
    5. this.logger.trace("Arguments: " + Arrays.toString(args));
    6. }
    7. return this.doInvoke(args);
    8. }

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

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

    1. public void invokeAndHandle(ServletWebRequest webRequest, ModelAndViewContainer mavContainer, Object... providedArgs) throws Exception {
    2. Object returnValue = this.invokeForRequest(webRequest, mavContainer, providedArgs);
    3. ......
    4. try {
    5. this.returnValueHandlers.handleReturnValue(returnValue, this.getReturnValueType(returnValue), mavContainer, webRequest);
    6. } catch (Exception var6) {
    7. if (this.logger.isTraceEnabled()) {
    8. this.logger.trace(this.formatErrorForReturnValue(returnValue), var6);
    9. }
    10. ......
    11. }
    12. }

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

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

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

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

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

            这个方法内部为:

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

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

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

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

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

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

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

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

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

             方法内部如下:

    1. private void processDispatchResult(HttpServletRequest request, HttpServletResponse response, @Nullable HandlerExecutionChain mappedHandler, @Nullable ModelAndView mv, @Nullable Exception exception) throws Exception {
    2. boolean errorView = false;
    3. if (exception != null) {
    4. if (exception instanceof ModelAndViewDefiningException) {
    5. this.logger.debug("ModelAndViewDefiningException encountered", exception);
    6. mv = ((ModelAndViewDefiningException)exception).getModelAndView();
    7. } else {
    8. Object handler = mappedHandler != null ? mappedHandler.getHandler() : null;
    9. mv = this.processHandlerException(request, response, handler, exception);
    10. errorView = mv != null;
    11. }
    12. }
    13. if (mv != null && !mv.wasCleared()) {
    14. this.render(mv, request, response);
    15. if (errorView) {
    16. WebUtils.clearErrorRequestAttributes(request);
    17. }
    18. } else if (this.logger.isTraceEnabled()) {
    19. this.logger.trace("No view rendering, null ModelAndView returned.");
    20. }
    21. if (!WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) {
    22. if (mappedHandler != null) {
    23. mappedHandler.triggerAfterCompletion(request, response, (Exception)null);
    24. }
    25. }
    26. }

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

    render方法内部:

    1. protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception {
    2. Locale locale = this.localeResolver != null ? this.localeResolver.resolveLocale(request) : request.getLocale();
    3. response.setLocale(locale);
    4. String viewName = mv.getViewName();
    5. View view;
    6. if (viewName != null) {
    7. view = this.resolveViewName(viewName, mv.getModelInternal(), locale, request);
    8. if (view == null) {
    9. throw new ServletException("Could not resolve view with name '" + mv.getViewName() + "' in servlet with name '" + this.getServletName() + "'");
    10. }
    11. } else {
    12. view = mv.getView();
    13. if (view == null) {
    14. throw new ServletException("ModelAndView [" + mv + "] neither contains a view name nor a View object in servlet with name '" + this.getServletName() + "'");
    15. }
    16. }
    17. if (this.logger.isTraceEnabled()) {
    18. this.logger.trace("Rendering view [" + view + "] ");
    19. }
    20. try {
    21. if (mv.getStatus() != null) {
    22. response.setStatus(mv.getStatus().value());
    23. }
    24. view.render(mv.getModelInternal(), request, response);
    25. } catch (Exception var8) {
    26. if (this.logger.isDebugEnabled()) {
    27. this.logger.debug("Error rendering view [" + view + "]", var8);
    28. }
    29. throw var8;
    30. }
    31. }

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

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

    1. @Nullable
    2. protected View resolveViewName(String viewName, @Nullable Map<String, Object> model, Locale locale, HttpServletRequest request) throws Exception {
    3. if (this.viewResolvers != null) {
    4. Iterator var5 = this.viewResolvers.iterator();
    5. while(var5.hasNext()) {
    6. ViewResolver viewResolver = (ViewResolver)var5.next();
    7. View view = viewResolver.resolveViewName(viewName, locale);
    8. if (view != null) {
    9. return view;
    10. }
    11. }
    12. }
    13. return null;
    14. }

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

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

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

            调用了ContentNegotiatingViewResolver.resolveViewName这个方法

    1. @Nullable
    2. public View resolveViewName(String viewName, Locale locale) throws Exception {
    3. .......
    4. if (requestedMediaTypes != null) {
    5. List<View> candidateViews = this.getCandidateViews(viewName, locale, requestedMediaTypes);
    6. View bestView = this.getBestView(candidateViews, requestedMediaTypes, attrs);
    7. if (bestView != null) {
    8. return bestView;
    9. }
    10. }

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

    1. private List<View> getCandidateViews(String viewName, Locale locale, List<MediaType> requestedMediaTypes) throws Exception {
    2. List<View> candidateViews = new ArrayList();
    3. if (this.viewResolvers != null) {
    4. Assert.state(this.contentNegotiationManager != null, "No ContentNegotiationManager set");
    5. Iterator var5 = this.viewResolvers.iterator();
    6. while(var5.hasNext()) {
    7. ViewResolver viewResolver = (ViewResolver)var5.next();
    8. View view = viewResolver.resolveViewName(viewName, locale);
    9. if (view != null) {
    10. candidateViews.add(view);
    11. }
    12. ......
    13. }

           最后有两个满足:

             所以先看看ThymeleafResolver.resolveViewName

    1. @Nullable
    2. public View resolveViewName(String viewName, Locale locale) throws Exception {
    3. if (!this.isCache()) {
    4. return this.createView(viewName, locale);
    5. } else {
    6. Object cacheKey = this.getCacheKey(viewName, locale);
    7. View view = (View)this.viewAccessCache.get(cacheKey);
    8. if (view == null) {
    9. synchronized(this.viewCreationCache) {
    10. view = (View)this.viewCreationCache.get(cacheKey);
    11. if (view == null) {
    12. view = this.createView(viewName, locale);
    13. if (view == null && this.cacheUnresolved) {
    14. view = UNRESOLVED_VIEW;
    15. }
    16. if (view != null && this.cacheFilter.filter(view, viewName, locale)) {
    17. this.viewAccessCache.put(cacheKey, view);
    18. this.viewCreationCache.put(cacheKey, view);
    19. }
    20. }
    21. }
    22. } else if (this.logger.isTraceEnabled()) {
    23. this.logger.trace(formatKey(cacheKey) + "served from cache");
    24. }
    25. return view != UNRESOLVED_VIEW ? view : null;
    26. }
    27. }

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

    1. protected View createView(String viewName, Locale locale) throws Exception {
    2. if (!this.alwaysProcessRedirectAndForward && !this.canHandle(viewName, locale)) {
    3. vrlogger.trace("[THYMELEAF] View \"{}\" cannot be handled by ThymeleafViewResolver. Passing on to the next resolver in the chain.", viewName);
    4. return null;
    5. } else {
    6. String forwardUrl;
    7. if (viewName.startsWith("redirect:")) {
    8. vrlogger.trace("[THYMELEAF] View \"{}\" is a redirect, and will not be handled directly by ThymeleafViewResolver.", viewName);
    9. forwardUrl = viewName.substring("redirect:".length(), viewName.length());
    10. RedirectView view = new RedirectView(forwardUrl, this.isRedirectContextRelative(), this.isRedirectHttp10Compatible());
    11. return (View)this.getApplicationContext().getAutowireCapableBeanFactory().initializeBean(view, viewName);
    12. } else if (viewName.startsWith("forward:")) {
    13. vrlogger.trace("[THYMELEAF] View \"{}\" is a forward, and will not be handled directly by ThymeleafViewResolver.", viewName);
    14. forwardUrl = viewName.substring("forward:".length(), viewName.length());
    15. return new InternalResourceView(forwardUrl);
    16. } else if (this.alwaysProcessRedirectAndForward && !this.canHandle(viewName, locale)) {
    17. vrlogger.trace("[THYMELEAF] View \"{}\" cannot be handled by ThymeleafViewResolver. Passing on to the next resolver in the chain.", viewName);
    18. return null;
    19. } else {
    20. vrlogger.trace("[THYMELEAF] View {} will be handled by ThymeleafViewResolver and a {} instance will be created for it", viewName, this.getViewClass().getSimpleName());
    21. return this.loadView(viewName, locale);
    22. }
    23. }
    24. }

            先判断能否处理,然后判断是不是以重定向开头或者是转发开头的,目前的请求是"/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

  • 相关阅读:
    关于语言大模型的八大论断
    C++模拟OpenGL库——图片处理及纹理系统(三):图片缩放操作:简单插值&二次线性插值
    SpringBoot-黑马程序员-学习笔记(四)
    knn算法详解
    理解QT信号和槽
    java基于springboot在线学习教育网站管理系统附代码段
    SQL知识大全(二):SQL的基础知识你都掌握了吗?
    WIN10安装docker
    540 - Team Queue (UVA)
    爬虫代理请求转换selenium添加带有账密的socks5代理
  • 原文地址:https://blog.csdn.net/weixin_42196338/article/details/127880345