Spring MVC 4.2.4.RELEASE 中文文档
  • Introduction
  • Part I: 目录与翻译注记(Table of contents and Notes)
    • 目录
    • 翻译注记
  • Part II: 文档内容(Documentation contents)
    • Spring Web MVC框架简介
      • Spring Web MVC的新特性
      • 允许其他MVC实现
    • DispatcherServlet
      • WebApplicationContext中特殊的Bean类型
      • 默认的DispatcherServlet配置
      • DispatcherServlet的处理流程
    • 控制器的实现
      • 使用@Controller注解定义一个控制器
      • 使用@RequestMapping注解映射请求路径
      • 定义@RequestMapping注解的处理方法
      • 异步请求的处理
      • 对控制器测试
    • 处理器映射
      • 使用HandlerInterceptor拦截请求
    • 视图解析
      • 使用ViewResolver接口解析视图
      • 视图链
      • 视图重定向
      • 内容协商解析器ContentNegotiatingViewResolver
    • 使用闪存属性FlashAttributes
    • URI构造
      • 为控制器和方法指定URI
      • 在视图中为控制器和方法指定URI
    • 地区信息
      • 获取时区信息
      • Accept请求头解析器AcceptHeaderLocaleResolver
      • Cookie解析器CookieLocaleResolver
      • Session解析器SessionLocaleResolver
      • 地区更改拦截器LocaleChangeInterceptor
    • 主题 themes
      • 关于主题:概览
      • 定义主题
      • 主题解析器
    • Spring的multipart(文件上传)支持
      • 概述
      • 使用MultipartResolver与Commons FileUpload传输文件
      • Servlet 3.0下的MultipartResolver
      • 处理表单中的文件上传
      • 处理客户端发起的文件上传请求
    • 异常处理
      • 处理器异常解析器HandlerExceptionHandler
      • @ExceptionHandler注解
      • 处理一般的Spring MVC异常
      • 使用@ResponseStatus注解业务异常
      • Servlet默认容器错误页面的定制化
    • Web安全
    • "约定优于配置"的支持
      • 控制器类名-处理器映射ControllerClassNameHandlerMapping
      • 模型ModelMap(ModelAndView)
      • 视图-请求与视图名的映射
    • HTTP缓存支持
      • HTTP请求头Cache-Control
      • 对静态资源的HTTP缓存支持
      • 在控制器中设置Cache-Control、ETag和Last-Modified响应头
      • 弱ETag
    • 基于代码的Servlet容器初始化
    • 配置Spring MVC
      • 启用MVC Java编程配置或MVC命名空间
      • 默认配置的定制化
      • 转换与格式化
      • 验证
      • 拦截器
      • 内容协商
      • 视图控制器
      • 视图解析器
      • 资源的服务
      • 回到默认的Servlet来进行资源服务
      • 路径匹配
      • 消息转换器
      • 使用MVC Java编程进行高级定制
      • 使用MVC命名空间进行高级定制
Powered by GitBook
On this page
  • 21.5.4 内容协商解析器ContentNegotiatingViewResolver
  • mvc-viewresolver-resolver "21.5.1 Resolving views with the ViewResolver

Was this helpful?

  1. Part II: 文档内容(Documentation contents)
  2. 视图解析

内容协商解析器ContentNegotiatingViewResolver

Previous视图重定向Next使用闪存属性FlashAttributes

Last updated 5 years ago

Was this helpful?

21.5.4 内容协商解析器ContentNegotiatingViewResolver

ContentNegotiatingViewResolver自己并不会解析视图,而是委托给其他的视图解析器去处理。

The ContentNegotiatingViewResolver does not resolve views itself but rather delegates to other view resolvers, selecting the view that resembles the representation requested by the client. Two strategies exist for a client to request a representation from the server:

  • Use a distinct URI for each resource, typically by using a different file extension in the URI. For example, the URI <http://www.example.com/users/fred.pdf> requests a PDF representation of the user fred, and <http://www.example.com/users/fred.xml> requests an XML representation.

  • Use the same URI for the client to locate the resource, but set the Accept HTTP request header to list the that it understands. For example, an HTTP request for <http://www.example.com/users/fred> with an Accept header set to application/pdf requests a PDF representation of the user fred, while <http://www.example.com/users/fred> with an Accept header set to text/xml requests an XML representation. This strategy is known as .

Note

One issue with the Accept header is that it is impossible to set it in a web browser within HTML. For example, in Firefox, it is fixed to:

    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

For this reason it is common to see the use of a distinct URI for each representation when developing browser based web applications.

To support multiple representations of a resource, Spring provides the ContentNegotiatingViewResolver to resolve a view based on the file extension or Accept header of the HTTP request. ContentNegotiatingViewResolver does not perform the view resolution itself but instead delegates to a list of view resolvers that you specify through the bean property ViewResolvers.

The ContentNegotiatingViewResolver selects an appropriate View to handle the request by comparing the request media type(s) with the media type (also known as Content-Type) supported by the View associated with each of its ViewResolvers. The first View in the list that has a compatible Content- Type returns the representation to the client. If a compatible view cannot be supplied by the ViewResolver chain, then the list of views specified through the DefaultViews property will be consulted. This latter option is appropriate for singleton Views that can render an appropriate representation of the current resource regardless of the logical view name. The Accept header may include wild cards, for example text/*, in which case a View whose Content-Type was text/xml is a compatible match.

To support custom resolution of a view based on a file extension, use a ContentNegotiationManager: see .

Here is an example configuration of a ContentNegotiatingViewResolver:

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="viewResolvers">
        <list>
            <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="prefix" value="/WEB-INF/jsp/"/>
                <property name="suffix" value=".jsp"/>
            </bean>
        </list>
    </property>
    <property name="defaultViews">
        <list>
            <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
        </list>
    </property>
</bean>

<bean id="content" class="com.foo.samples.rest.SampleContentAtomView"/>

The InternalResourceViewResolver handles the translation of view names and JSP pages, while the BeanNameViewResolver returns a view based on the name of a bean. (See "[Resolving views with the ViewResolver interface](mvc.html

mvc-viewresolver-resolver "21.5.1 Resolving views with the ViewResolver

interface" )" for more details on how Spring looks up and instantiates a view.) In this example, the content bean is a class that inherits from AbstractAtomFeedView, which returns an Atom RSS feed. For more information on creating an Atom Feed representation, see the section Atom Views.

In the above configuration, if a request is made with an .html extension, the view resolver looks for a view that matches the text/html media type. The InternalResourceViewResolver provides the matching view for text/html. If the request is made with the file extension .atom, the view resolver looks for a view that matches the application/atom+xml media type. This view is provided by the BeanNameViewResolver that maps to the SampleContentAtomView if the view name returned is content. If the request is made with the file extension .json, the MappingJackson2JsonView instance from the DefaultViews list will be selected regardless of the view name. Alternatively, client requests can be made without a file extension but with the Accept header set to the preferred media-type, and the same resolution of request to views would occur.

Note

If `ContentNegotiatingViewResolver's list of ViewResolvers is not configured explicitly, it automatically uses any ViewResolvers defined in the application context.

The corresponding controller code that returns an Atom RSS feed for a URI of the form <http://localhost/content.atom> or <http://localhost/content> with an Accept header of application/atom+xml is shown below.

@Controller
public class ContentController {

    private List<SampleContent> contentList = new ArrayList<SampleContent>();

    @RequestMapping(path="/content", method=RequestMethod.GET)
    public ModelAndView getContent() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("content");
        mav.addObject("sampleContentList", contentList);
        return mav;
    }

}

media types
content negotiation
Section 21.16.6, "Content Negotiation"
[Note]
[Note]