91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Springmvc ViewResolver實現的過程

發布時間:2020-11-02 15:58:10 來源:億速云 閱讀:211 作者:Leah 欄目:開發技術

Springmvc ViewResolver實現的過程?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

總結:

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

既然看到有ModelAndView直接跳轉jsp的, 有請求轉發的,有重定向的,這里整體是怎么設計的: (@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()里的:

Springmvc ViewResolver實現的過程

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()會根據不同的路徑生成不同的view, return mav 會返回JstlView, return "forward:/patch" 會返回InternalResourceView, return "direct:/patch" 會返回IndirectView

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

Springmvc ViewResolver實現的過程

再來看是如何生成不同的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實現了resolveViewName方法

Springmvc ViewResolver實現的過程

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 &#63; view : null);
    }
  }

InterResourceViewResover中沒有createView方法,所以是調用它父類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"在這里構造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" 在這里構造view
    } 

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

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

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

Springmvc ViewResolver實現的過程

斷點放在這里,

Springmvc ViewResolver實現的過程

然后下一步:

Springmvc ViewResolver實現的過程

看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

桃江县| 商洛市| 婺源县| 南开区| 汶上县| 湛江市| 仪征市| 潞城市| 乌兰浩特市| 平安县| 阳山县| 宁波市| 新晃| 崇阳县| 忻州市| 体育| 湘潭县| 高陵县| 中阳县| 嘉黎县| 遵化市| 高要市| 西充县| 金昌市| 太康县| 平阴县| 铜陵市| 青田县| 来安县| 滁州市| 永吉县| 容城县| 南投市| 云龙县| 东方市| 巨野县| 辽宁省| 盐源县| 湖州市| 长宁区| 天气|