最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Springmvc ViewResolver設計實現(xiàn)過程解析

 更新時間:2020年10月30日 11:55:14   作者:圣金巫靈  
這篇文章主要介紹了Springmvc ViewResolver設計實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

總結(jié):

ViewResolver 如果要改需要自己注入到容器中并進行修改, springmvc使用的是InterResourceViewResover
view不需要自己改,是springmvc根據(jù)return返回值選的

既然看到有ModelAndView直接跳轉(zhuǎn)jsp的, 有請求轉(zhuǎn)發(fā)的,有重定向的,這里整體是怎么設計的: (@ResponseBody的在此不作展開)

HiController:

@Controller
public class HiController {
  @RequestMapping("/hi")
  public ModelAndView getHi() {
    ModelAndView mav = new ModelAndView("me");
    return mav;
  }

  @RequestMapping("/yes")
  public String forwardYes() {
    return "forward:patch";
  }

  @RequestMapping("/no")
  public String RedirectNo() {
    return "redirect:patch";
  }

  @ResponseBody
  @RequestMapping("/patch")
  public String redirectNo() {
    return "from forward or redirect request";   // 這種情況沒有view,在這里不討論
  }
}

主要代碼:

DispatcherServlet.doDispatch()里的:

DispatcherServlet.render方法:

protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception {
    // Determine locale for request and apply it to the response.
    Locale locale =
        (this.localeResolver != null ? this.localeResolver.resolveLocale(request) : request.getLocale());
    response.setLocale(locale);

    View view;
    String viewName = mv.getViewName();
    if (viewName != null) {
      // We need to resolve the view name.
      view = resolveViewName(viewName, mv.getModelInternal(), locale, request);   // 1
      if (view == null) {
        throw new ServletException("Could not resolve view with name '" + mv.getViewName() +
            "' in servlet with name '" + getServletName() + "'");
      }
    }
    else {
      // No need to lookup: the ModelAndView object contains the actual View object.
      view = mv.getView();
      if (view == null) {
        throw new ServletException("ModelAndView [" + mv + "] neither contains a view name nor a " +
            "View object in servlet with name '" + getServletName() + "'");
      }
    }

    // Delegate to the View object for rendering.
    if (logger.isTraceEnabled()) {
      logger.trace("Rendering view [" + view + "] ");
    }
    try {
      if (mv.getStatus() != null) {
        response.setStatus(mv.getStatus().value());
      }
      view.render(mv.getModelInternal(), request, response);  // 2
    }
    catch (Exception ex) {
      if (logger.isDebugEnabled()) {
        logger.debug("Error rendering view [" + view + "]", ex);
      }
      throw ex;
    }
  }

1. view = resolveViewName()會根據(jù)不同的路徑生成不同的view, return mav 會返回JstlView, return "forward:/patch" 會返回InternalResourceView, return "direct:/patch" 會返回IndirectView

2. 不同的view去走不同的view.render(), 根據(jù)不同的view重寫abstract void renderMergedOutputModel方法

再來看是如何生成不同的view:[/code][code]view = resolveViewName() 進去,走到
DiapatcherServlet先有ViewResolver這個,用來生成不同的view

protected View resolveViewName(String viewName, @Nullable Map<String, Object> model,
      Locale locale, HttpServletRequest request) throws Exception {

    if (this.viewResolvers != null) {
      for (ViewResolver viewResolver : this.viewResolvers) {
        View view = viewResolver.resolveViewName(viewName, locale);
        if (view != null) {
          return view;
        }
      }
    }
    return null;
  }

ViewResolver 接口只有一個方法

public interface ViewResolver {
  @Nullable
  View resolveViewName(String viewName, Locale locale) throws Exception;

}

要配置具體的視圖解析器,springMVC中使用的是InterResourceViewResover,InterResourceViewResover 和他的父類UrlBasedViewResolver中都沒有重寫resolveViewName方法,再上一層的父類AbstractCahingViewResolver實現(xiàn)了resolveViewName方法

AbstractCahingViewResolver:

@Override
  @Nullable
  public View resolveViewName(String viewName, Locale locale) throws Exception {
    if (!isCache()) {
      return createView(viewName, locale);
    }
    else {
      Object cacheKey = getCacheKey(viewName, locale);
      View view = this.viewAccessCache.get(cacheKey);
      if (view == null) {
        synchronized (this.viewCreationCache) {
          view = this.viewCreationCache.get(cacheKey);
          if (view == null) {
            // Ask the subclass to create the View object.
            view = 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 (logger.isTraceEnabled()) {
          logger.trace(formatKey(cacheKey) + "served from cache");
        }
      }
      return (view != UNRESOLVED_VIEW ? view : null);
    }
  }

InterResourceViewResover中沒有createView方法,所以是調(diào)用它父類UrlBasedViewResolver的createView方法:

@Override
  protected View createView(String viewName, Locale locale) throws Exception {
    // If this resolver is not supposed to handle the given view,
    // return null to pass on to the next resolver in the chain.
    if (!canHandle(viewName, locale)) {
      return null;
    }

    // Check for special "redirect:" prefix.
    if (viewName.startsWith(REDIRECT_URL_PREFIX)) {
      String redirectUrl = viewName.substring(REDIRECT_URL_PREFIX.length());
      RedirectView view = new RedirectView(redirectUrl,
          isRedirectContextRelative(), isRedirectHttp10Compatible());
      String[] hosts = getRedirectHosts();
      if (hosts != null) {
        view.setHosts(hosts);
      }
      return applyLifecycleMethods(REDIRECT_URL_PREFIX, view);  // return "direct:/patch"在這里構(gòu)造view
    }

    // Check for special "forward:" prefix.
    if (viewName.startsWith(FORWARD_URL_PREFIX)) {
      String forwardUrl = viewName.substring(FORWARD_URL_PREFIX.length());
      InternalResourceView view = new InternalResourceView(forwardUrl);
      return applyLifecycleMethods(FORWARD_URL_PREFIX, view);   // return "forward:/patch" 在這里構(gòu)造view
    } 

    // Else fall back to superclass implementation: calling loadView.
    return super.createView(viewName, locale);          // return mav 在這里構(gòu)造view
  }

關于ViewResolver的代碼執(zhí)行順序, 前面分析那么多,這里再打斷點快速驗證一下:

進DispatcherServlet的doDispatch看到就是這個解析器:

斷點放在這里,

然后下一步:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 通過MyBatis讀取數(shù)據(jù)庫數(shù)據(jù)并提供rest接口訪問

    通過MyBatis讀取數(shù)據(jù)庫數(shù)據(jù)并提供rest接口訪問

    這篇文章主要介紹了通過MyBatis讀取數(shù)據(jù)庫數(shù)據(jù)并提供rest接口訪問 的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-08-08
  • Guava事件總線應用場景最佳實踐

    Guava事件總線應用場景最佳實踐

    這篇文章主要為大家介紹了Guava事件總線應用場景最佳實踐,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-12-12
  • SpringBoot整合EasyExcel進行大數(shù)據(jù)處理的方法詳解

    SpringBoot整合EasyExcel進行大數(shù)據(jù)處理的方法詳解

    EasyExcel是一個基于Java的簡單、省內(nèi)存的讀寫Excel的開源項目。在盡可能節(jié)約內(nèi)存的情況下支持讀寫百M的Excel。本文將在SpringBoot中整合EasyExcel進行大數(shù)據(jù)處理,感興趣的可以了解一下
    2022-05-05
  • Mybatis是這樣防止sql注入的

    Mybatis是這樣防止sql注入的

    本文詳細講解了Mybatis是如何防止sql注入的,對大家的學習或工作具有一定的參考借鑒價值。需要的朋友可以收藏下,方便下次瀏覽觀看
    2021-12-12
  • SpringBoot+kaptcha實現(xiàn)驗證碼花式玩法詳解

    SpringBoot+kaptcha實現(xiàn)驗證碼花式玩法詳解

    這篇文章主要想和大家聊聊kaptcha的用法,畢竟這個已經(jīng)有16年歷史的玩意還在有人用,說明它的功能還是相當強大的,感興趣的小伙伴可以了解一下
    2022-05-05
  • 關于Java中Bean的生命周期詳解

    關于Java中Bean的生命周期詳解

    這篇文章主要介紹了關于Java中Bean的生命周期詳解,所謂的?命周期指的是?個對象從誕?到銷毀的整個?命過程,我們把這個過程就叫做?個對象的?命周期,需要的朋友可以參考下
    2023-08-08
  • Android開發(fā)在輪播圖片上加入點擊事件的方法

    Android開發(fā)在輪播圖片上加入點擊事件的方法

    這篇文章主要介紹了Android開發(fā)在輪播圖片上加入點擊事件的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-11-11
  • HashMap 和 HashSet的區(qū)別

    HashMap 和 HashSet的區(qū)別

    本文主要介紹HashMap 和 HashSet的區(qū)別,這里整理了詳細的資料來說名兩者的區(qū)別,并說明如何使用該方法,有需要的小伙伴可以參考下
    2016-09-09
  • Spring Boot 與 Kotlin 使用Redis數(shù)據(jù)庫的配置方法

    Spring Boot 與 Kotlin 使用Redis數(shù)據(jù)庫的配置方法

    Redis是目前業(yè)界使用最廣泛的內(nèi)存數(shù)據(jù)存儲。下面通過本文給大家介紹Spring Boot 與 Kotlin 使用Redis數(shù)據(jù)庫的配置方法,感興趣的朋友一起看看吧
    2018-01-01
  • Java鍵值對Pair的使用方式和操作實現(xiàn)

    Java鍵值對Pair的使用方式和操作實現(xiàn)

    鍵值對是一種常見的數(shù)據(jù)結(jié)構(gòu),它由一個唯一的鍵和與之關聯(lián)的值組成,本文就來介紹一下Java鍵值對Pair的使用方式和操作實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2023-12-12

最新評論

井研县| 阳泉市| 左云县| 水城县| 诏安县| 西华县| 襄樊市| 黄石市| 黄陵县| 青河县| 庆阳市| 左云县| 东阿县| 伊金霍洛旗| 龙南县| 洛浦县| 林甸县| 化德县| 娄烦县| 武邑县| 沁源县| 武清区| 皋兰县| 保山市| 汉川市| 达州市| 平远县| 荣成市| 得荣县| 昭平县| 清涧县| 永顺县| 那曲县| 木兰县| 遵化市| 梓潼县| 昔阳县| 安塞县| 乌审旗| 凭祥市| 雅安市|