对于最新的稳定版本,请使用 Spring Framework 7.0.6!spring-doc.cadn.net.cn

视图解析

Spring MVC 定义了 ViewResolverView 接口,这些接口允许你在浏览器中渲染模型而不将你绑定到特定的视图技术。ViewResolver 提供了视图名称和实际视图之间的映射。View 解决了在将数据交给特定视图技术之前的数据准备问题。spring-doc.cadn.net.cn

以下表格提供了有关ViewResolver层次结构的更多详细信息:spring-doc.cadn.net.cn

表 1. ViewResolver 实现
ViewResolver 描述

AbstractCachingViewResolverspring-doc.cadn.net.cn

AbstractCachingViewResolver 的子类会缓存它们解析的视图实例。 某些视图技术的性能可以通过缓存得到提升。你可以通过将 cache 属性设置为 false 来关闭缓存。此外,如果你必须在运行时刷新某个视图(例如,当 FreeMarker 模板被修改时), 可以使用 removeFromCache(String viewName, Locale loc) 方法。spring-doc.cadn.net.cn

UrlBasedViewResolverspring-doc.cadn.net.cn

ViewResolver 接口的简单实现,该实现直接将逻辑视图名称解析为URL,而无需显式映射定义。 这在你的逻辑名称与视图资源名称直接匹配的情况下是合适的, 而无需任意映射。spring-doc.cadn.net.cn

InternalResourceViewResolverspring-doc.cadn.net.cn

UrlBasedViewResolver 的便捷子类,支持 InternalResourceView(实际上即 Servlets 和 JSPs)及其子类(例如 JstlView)。您可通过 setViewClass(..) 参数指定此解析器生成的所有视图的视图类。详情请参阅 UrlBasedViewResolver 的 javadoc 文档。spring-doc.cadn.net.cn

FreeMarkerViewResolverspring-doc.cadn.net.cn

Convenient subclass of UrlBasedViewResolver that supports FreeMarkerView and custom subclasses of them.spring-doc.cadn.net.cn

ContentNegotiatingViewResolverspring-doc.cadn.net.cn

Implementation of the ViewResolver interface that resolves a view based on the request file name or Accept header. See 内容协商.spring-doc.cadn.net.cn

BeanNameViewResolverspring-doc.cadn.net.cn

实现 ViewResolver 接口,该接口将视图名称解释为当前应用上下文中的 bean 名称。这是一种非常灵活的变体,允许根据不同的视图名称混合和匹配不同的视图类型。每个这样的 View 都可以定义为 bean,例如在 XML 或配置类中。spring-doc.cadn.net.cn

处理

您可以链接视图解析器,方法是声明多个解析器bean,并且如果必要,通过设置order属性来指定顺序。请记住,order属性越高,视图解析器在链中的位置越靠后。spring-doc.cadn.net.cn

The contract of a ViewResolver specifies that it can return null to indicate that the view could not be found. However, in the case of JSPs and InternalResourceViewResolver, the only way to figure out if a JSP exists is to perform a dispatch through RequestDispatcher. Therefore, you must always configure an InternalResourceViewResolver to be last in the overall order of view resolvers.spring-doc.cadn.net.cn

配置视图解析器就像在您的Spring配置中添加ViewResolver个bean一样简单。MVC配置提供了一个专门的配置API,用于视图解析器和添加无逻辑的视图控制器,这对于HTML模板渲染而无需控制器逻辑非常有用。spring-doc.cadn.net.cn

重定向

特殊前缀 redirect: 在视图名称中允许你执行重定向。UrlBasedViewResolver(及其子类)将其识别为需要重定向的指令。视图名称的其余部分是重定向URL。spring-doc.cadn.net.cn

最终效果与控制器返回 RedirectView 相同,但现在控制器本身可以使用逻辑视图名称进行操作。逻辑视图名称(如 redirect:/myapp/some/resource)相对于当前的 Servlet 上下文进行重定向,而名称如 redirect:https://myhost.com/some/arbitrary/path 则重定向到绝对 URL。spring-doc.cadn.net.cn

转发

您还可以使用一个特殊的forward:前缀用于视图名称,这些视图名称最终由UrlBasedViewResolver及其子类解析。这会创建一个InternalResourceView,它执行一个RequestDispatcher.forward()。 因此,这个前缀对于InternalResourceViewResolverInternalResourceView(用于JSP)没有用处,但如果您使用另一种视图技术但仍希望强制资源转发到Servlet/JSP引擎处理,可能会有所帮助。请注意,您也可以链接多个视图解析器。spring-doc.cadn.net.cn

内容协商

ContentNegotiatingViewResolver does not resolve views itself but rather delegates to other view resolvers and selects the view that resembles the representation requested by the client. The representation can be determined from the Accept header or from a query parameter (for example, "/path?format=pdf")。spring-doc.cadn.net.cn

The ContentNegotiatingViewResolver 选择一个合适的 View 来处理请求,通过比较请求的媒体类型与每个 ViewResolvers 关联的 View 支持的媒体类型(也称为 Content-Type)。列表中的第一个具有兼容 Content-TypeView 将表示返回给客户端。如果 ViewResolver 链无法提供兼容的视图,则会咨询通过 DefaultViews 属性指定的视图列表。后一种选项适用于可以渲染当前资源适当表示形式的单例 Views,而无需考虑逻辑视图名称。Accept 标头可以包含通配符(例如 text/*),在这种情况下,View 其中 Content-Typetext/xml 是一个兼容的匹配项。spring-doc.cadn.net.cn

请参阅 视图解析器MVC配置 下的配置详情。spring-doc.cadn.net.cn