22_4 Spring MVC - ViewResolver系列 - BeanNameViewResolver

22_4 Spring MVC - ViewResolver系列 - BeanNameViewResolver

一、简介

BeanNameViewResolver视图解析器跟XmlViewResolver类似,也是根据Controller返回的视图名去匹配定义好的视图Bean对象。不同点是BeanNameViewResolver将视图Bean对象的定义配置在springmvc.xml文件中。

二、源码解析

BeanNameViewResolver重写了抽象方法 resolveViewName来实现View对象的创建。

	@Override
	@Nullable
	public View resolveViewName(String viewName, Locale locale) throws BeansException {
		ApplicationContext context = obtainApplicationContext();
		if (!context.containsBean(viewName)) {
			if (logger.isDebugEnabled()) {
				logger.debug("No matching bean found for view name '" + viewName + "'");
			}
			// Allow for ViewResolver chaining...
			return null;
		}
		if (!context.isTypeMatch(viewName, View.class)) {
			if (logger.isDebugEnabled()) {
				logger.debug("Found matching bean for view name '" + viewName +
						"' - to be ignored since it does not implement View");
			}
			// Since we're looking into the general ApplicationContext here,
			// let's accept this as a non-match and allow for chaining as well...
			return null;
		}
		return context.getBean(viewName, View.class);
	}
  • 基本合法性判断完成后,通过上下文对象获取视图Bean
posted @ 2020-09-13 15:39  在线打工者  阅读(166)  评论(0编辑  收藏  举报