标签:com gate string hosts cat let https ethos next
前一篇只是在debug, 有点乱。
既然看到有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()里的:
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; } }
view = resolveViewName()会根据不同的路径生成不同的view, return mav 会返回JstlView, return "forward:/patch" 会返回InternalResourceView, return "direct:/patch" 会返回IndirectView
不同的view去走不同的view.render()
再来看是如何生成不同的view:
view = resolveViewName() 进去,走到
UrlBasedViewResolver
@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 }
完
标签:com gate string hosts cat let https ethos next
原文地址:https://www.cnblogs.com/dayanjing/p/13834744.html