Spring MVC : 概念模型 ViewResolver

概述

Spring MVC的一个概念模型接口,用于完成功能从视图名称获取相应的视图对象,故名View Resolver(“视图解析器”)。本接口只定义了一个方法:

View resolveViewName(String viewName, Locale locale) throws Exception;

该方法根据视图名称和指定的Locale本地化信息获取相应的View对象,如果解析不到指定名称的视图,则返回nullSpring MVC约定整个应用运行期间,视图的状态不发生变化,所以各个ViewResolver可以对所解析到的View做缓存。

对整个应用而言,Spring MVC可以同时使用多个ViewResolver,一个ViewResolver返回null并不表示指定的视图不存在,而是有可能会被下一个ViewResolver解析得到。

Spring MVC应用启动时,框架某个部分或者开发人员定义的ViewResolver组件会被注册到容器。然后DispatcherServlet初始化时初始化方法initViewResolvers会搜集到这些ViewResolver组件并记录到自己的属性List<ViewResolver> viewResolvers。随后DispatcherServlet处理请求解析视图时就会用到这些ViewResolver

举例来讲,对于一个使用Spring Boot + Spring MVC + Freemarker的应用来讲,缺省使用如下ViewResolver组件:

  • ContentNegotiatingViewResolver viewResolver

    WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter定义。

  • BeanNameViewResolver beanNameViewResolver

    WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter定义。

  • FreeMarkerViewResolver freeMarkerViewResolver

    FreeMarkerAutoConfiguration/FreeMarkerServletWebConfiguration定义。

  • ViewResolverComposite mvcViewResolver

    WebMvcAutoConfiguration$EnableWebMvcConfiguration(继承自WebMvcConfigurationSupport)定义。

  • InternalResourceViewResolver defaultViewResolver

    WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter定义。

源代码

源代码版本 : spring-webmvc-5.1.5.RELEASE

package org.springframework.web.servlet;

import java.util.Locale;

import org.springframework.lang.Nullable;

/**
 * Interface to be implemented by objects that can resolve views by name.
 *
 * View state doesn't change during the running of the application,
 * so implementations are free to cache views.
 *
 * Implementations are encouraged to support internationalization,
 * i.e. localized view resolution.
 *
 * @author Rod Johnson
 * @author Juergen Hoeller
 * @see org.springframework.web.servlet.view.InternalResourceViewResolver
 * @see org.springframework.web.servlet.view.ResourceBundleViewResolver
 * @see org.springframework.web.servlet.view.XmlViewResolver
 */
public interface ViewResolver {

	/**
	 * Resolve the given view by name.
	 * Note: To allow for ViewResolver chaining, a ViewResolver should
	 * return null if a view with the given name is not defined in it.
	 * However, this is not required: Some ViewResolvers will always attempt
	 * to build View objects with the given name, unable to return null
	 * (rather throwing an exception when View creation failed).
	 * @param viewName name of the view to resolve
	 * @param locale the Locale in which to resolve the view.
	 * ViewResolvers that support internationalization should respect this.
	 * @return the View object, or null if not found
	 * (optional, to allow for ViewResolver chaining)
	 * @throws Exception if the view cannot be resolved
	 * (typically in case of problems creating an actual View object)
	 */
	@Nullable
	View resolveViewName(String viewName, Locale locale) throws Exception;

}

参考文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值