概述
Spring MVC的一个概念模型接口,用于完成功能从视图名称获取相应的视图对象,故名View Resolver(“视图解析器”)。本接口只定义了一个方法:
View resolveViewName(String viewName, Locale locale) throws Exception;
该方法根据视图名称和指定的Locale本地化信息获取相应的View对象,如果解析不到指定名称的视图,则返回null。Spring 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;
}
本文介绍了SpringMVC中ViewResolver接口的概念与作用,详细解释了其核心方法resolveViewName的功能,即根据视图名称和本地化信息获取View对象。同时,文章列举了SpringBoot+SpringMVC+Freemarker应用中常见的ViewResolver组件,如ContentNegotiatingViewResolver、BeanNameViewResolver等,并说明了它们的工作机制和相互关系。
1501

被折叠的 条评论
为什么被折叠?



