Spring ViewResolver

发布时间:2020-08-01 20:52:13 作者:xiaosawuhen
来源:网络 阅读:985

Spring ViewResolver

AbstractCachingViewResolver、XmlViewResolver、ResourceBundleViewResolver、UrlBasedViewResolver、InternalResourceViewResolver、VelocityViewResolver、FreeMarkerViewResolver
、ContentNegotiatingViewResolver


加载

org.springframework.web.servlet.view.ContentNegotiatingViewResolver

问题点:

    是不是所有的ViewResolver都是通过ContentNegotiatingViewResolver这个加载的 --> 不是

    ContentNegotiatingViewResolver起到的作用是什么

    只有一个jspView的时候会不会调用ContentNegotiatingViewResolver

	private void initViewResolvers(ApplicationContext context) {
		this.viewResolvers = null;

		if (this.detectAllViewResolvers) {
			// Find all ViewResolvers in the ApplicationContext, including ancestor contexts.
			Map<String, ViewResolver> matchingBeans =
					BeanFactoryUtils.beansOfTypeIncludingAncestors(context, ViewResolver.class, true, false);
			if (!matchingBeans.isEmpty()) {
				this.viewResolvers = new ArrayList<>(matchingBeans.values());
				// We keep ViewResolvers in sorted order.
				AnnotationAwareOrderComparator.sort(this.viewResolvers);
			}
		}
		else {
			try {
				ViewResolver vr = context.getBean(VIEW_RESOLVER_BEAN_NAME, ViewResolver.class);
				this.viewResolvers = Collections.singletonList(vr);
			}
			catch (NoSuchBeanDefinitionException ex) {
				// Ignore, we'll add a default ViewResolver later.
			}
		}

		// Ensure we have at least one ViewResolver, by registering
		// a default ViewResolver if no other resolvers are found.
		if (this.viewResolvers == null) {
			this.viewResolvers = getDefaultStrategies(context, ViewResolver.class);
			if (logger.isDebugEnabled()) {
				logger.debug("No ViewResolvers found in servlet '" + getServletName() + "': using default");
			}
		}
	}



doDispatch

processDispatchResult

render:

			View view;
			if (mv.isReference()) {
				// We need to resolve the view name.
				view = resolveViewName(mv.getViewName(), mv.getModelInternal(), locale, request);
				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() + "'");
				}
			}

resolveViewName:

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

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


推荐阅读:
  1. Spring MVC入门 —— 学SpringMVC
  2. Spring Boot实践——SpringMVC视图解析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring viewresolver resolv

上一篇:使用Seek method做分页时offset predicate的注意事项

下一篇:现代中小企业IT基础平台建设 - 完整案例实战(01_案例设计)

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》