1. ContentNegotiatingViewResolver,这个视图解析器允许你用同样的内容数据来呈现不同的view。它支持如下面描述的三种方式:
    1. 1)使用扩展名
      http://localhost:8080/employees/nego/Jack.xml  返回结果为XML
      http://localhost:8080/employees/nego/Jack.json 返回结果为JSON
      http://localhost:8080/employees/nego/Jack  使用默认view呈现,比如JSP

      2) HTTP Request Header中的Accept,Accept 分别是 text/jsp,  text/pdf,  text/xml,  text/json,  无Accept 请求头

      3) 使用参数
      http://localhost:8080/employees/nego/Jack?format=xml  返回结果为XML
      http://localhost:8080/employees/nego/Jack?format=json 返回结果为JSON

  2.  ContentNegotiatingViewResolver 可以一个@RequestMapping,返回多个view。
  3. 配置
    1. <property name="favorPathExtension" value="true"></property> 是否启用扩展名支持,默认是true

      <property name="favorParameter" value="false"></property> 是否启用参数支持,默认是true

      <property name="ignoreAcceptHeader" value="true"></property> 是否忽略掉accept header,默认就是false

    2. spring 给我们提供了 ContentNegotiationManagerFactoryBean这是推荐的方式。 配置上类似。
      1. <bean id="cnManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
        <property name="ignoreAcceptHeader" value="true"/>
        <property name="favorPathExtension" value="true"/>
        <property name="defaultContentType" value="text/html"/>
        <property name="favorParameter" value="true"/>
        <property name="mediaTypes">
        <map>
        <entry key="xml" value="application/xml"/>
        <entry key="json" value="text/plain"/>
        <entry key="xls" value="application/vnd.ms-excel"/>
        </map>
        </property>
        </bean>
      2. <bean id="viewResolver"
        class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="contentNegotiationManager" ref="cnManager"/>
        <property name="viewResolvers"><!-- 针对freemarker的视图配置 -->
        <list>
        <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
        <property name="viewNames" value=".ftl"/>
        <property name="contentType" value="text/html; charset=utf-8"/>
        <property name="cache" value="true" />
        <property name="prefix" value="" />
        <property name="suffix" value="" />
        <property name="order" value=""/>
        </bean>
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/jsp/"></property><!-- 配置页面路径 -->
        <property name="suffix" value=".jsp"></property><!-- 文件以value值结尾 -->
        </bean>
        </list>
        </property>
        <property name="defaultViews">
        <list>
        <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
        <!-- <property name="extractValueFromSingleKeyModel" value="true"/> -->
        <property name="prettyPrint" value="true"/>
        <property name="contentType" value="text/plain"/>
        </bean>
        <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
        <constructor-arg ref="jaxb2Marshaller"> </constructor-arg>
        </bean>
        <bean class="com.wonders.stpt.bid.controller.JXLExcelView"/>
        </list>
        </property>
        </bean>
      3. defaultViews的作用:在defaultViews里注册的视图会在ContentNegotiationViewResolver中注册自己支持的内容类型

      4. 由ContentNegotiationManagerFactoryBean的mediaTypes决定响应的内容类型
      5. 当contentNegotiationManager决定好响应的内容类型后,ContentNegotiationViewResolver就会根据该内容类型选择一个兼容的View进行渲染输出
      6. 一旦有View对请求内容匹配,就直接渲染输出,不会进行ViewResolver的查询。
      7. 当注册的内容类型都不兼容时,会查询viewResolver中的ViewResolver是否支持该请求,如果ViewResolver表示支持该请求,那么就由该ViewResolver负责视图渲染,如果ViewResolver表示不支持该请求,则查询下一个ViewResolver,直至所有的ViewResolver查询完毕。
    3. 默认是支持path 后缀拓展方式, 也支持accept 请求头,但不支持 format 参数的

Spring 梳理 - ContentNegotiatingViewResolver的更多相关文章

  1. Spring 梳理 - filter、interceptor、aop实现与区别 -第二篇

    spring mvc中的Interceptor可以理解为是Spring MVC框架对AOP的一种实现方式.一般简单的功能又是通用的,每个请求都要去处理的,比如判断token是否失效可以使用spring ...

  2. Spring 梳理 - filter、interceptor、aop实现与区别 -第一篇

    前言 项目中我们经常需要对RESTful api进行拦截,主流实现方法有filter.interceptor.aop,先说一下他们各自的实现. Filter AnimalFilter实现javax.s ...

  3. Spring 梳理 - View - JSON and XML View

    实际案例json <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www. ...

  4. Spring 梳理 - 视图解析器 VS 视图(View,ViewResolver)

    View View接口表示一个响应给用户的视图,例如jsp文件,pdf文件,html文件等 该接口只有两个方法定义,分别表明该视图的ContentType和如何被渲染 Spring中提供了丰富的视图支 ...

  5. Spring 梳理 - javaConfig在App和webApp中的应用

    package com.dxz.demo.configuration; import org.springframework.context.annotation.Configuration; @Co ...

  6. Spring 梳理 - @Component

    使用@Component注解,表示该类定义为Spring管理Bean,使用默认value(可选)属性表示Bean标识符.如果不指定标识符,默认为首字母小写类名.例如类UserController的标识 ...

  7. Spring 梳理 - @Autowired VS @Resource

    Autowired @Autowired顾名思义,就是自动装配,其作用是为了消除代码Java代码里面的getter/setter与bean属性中的property.当然,getter看个人需求,如果私 ...

  8. Spring 梳理 - AOP那些学术概念—通知、增强处理连接点(JoinPoint)切面(Aspect)

    Spring  AOP那些学术概念—通知.增强处理连接点(JoinPoint)切面(Aspect)   1.我所知道的AOP 初看起来,上来就是一大堆的术语,而且还有个拉风的名字,面向切面编程,都说是 ...

  9. Spring 梳理 - JavaConfig、SPI、SCI、SpringSCI、WebApplicationInitializer、AbstractAnnotationConfigDispatcherServletInitializer、WebMvcConfigurationSupport

    总结1: SCI:Servlet容器(Tomcat)提供的初始化Servlet容器本身的接口,可替换web.xml SpringSCI:SpringServletContainerInitialize ...

随机推荐

  1. Windows下Python 3.6 + VS2017 + Anaconda 解决Unable to find vcvarsall.bat问题

    Python 3.6 + VS2017 + Anaconda 解决Unable to find vcvarsall.bat问题 已经安装了VS2017,需要将项目中的C代码翻译为Python代码,在编 ...

  2. JavaScript 小游戏 贪吃蛇

    贪吃蛇 代码: <!DOCTYPE html><html><head> <meta charset="UTF-8"> <met ...

  3. Java连载26-方法(语法结构)

    一.方法 1.返回值类型如果不是void,表示这个方法执行结束之后必须返回一个具体的数值,当方法执行结束的时候没有返回任何数值,编译器会报错,怎么返回值呢?并且要求“值”的数据类型必须和“方法的返回值 ...

  4. 洛谷P1352没有上司的舞会+树形二维DP

    传送门 题意:上司和直接下属,不能同时去一个聚会,问可邀请到的人的快乐值最大是多少: 参考:https://www.luogu.org/blog/mak2333/solution-p1352 思路: ...

  5. Codeforces 889F Letters Removing(二分 + 线段树 || 树状数组)

    Letters Removing 题意:给你一个长度为n的字符串,然后进行m次删除操作,每次删除区间[l,r]内的某个字符,删除后并且将字符串往前补位,求删除完之后的字符串. 题解:先开80个set ...

  6. Codeforces Round #483 (Div. 2) B. Minesweeper

    题目地址:http://codeforces.com/contest/984/problem/B 题目大意:扫雷游戏,给你一个n*m的地图,如果有炸弹,旁边的八个位置都会+1,问这幅图是不是正确的. ...

  7. poj3984 迷宫问题(简单的输出路径的bfs)

    题目链接 http://poj.org/problem?id=3984 中文题题意不解释了 反正就是简单的结构体套结构体存一下路径就行了 #include <iostream> #incl ...

  8. codeforces 766 C. Mahmoud and a Message(简单dp)

    题目链接:http://codeforces.com/contest/766/problem/C 题意:给你一个长度为n的字符串,这个字符串只包含小写字母,然后让你把这个字符串进行分割,形成若干个小的 ...

  9. poj 1417 True Liars(并查集+背包dp)

    题目链接:http://poj.org/problem?id=1417 题意:就是给出n个问题有p1个好人,p2个坏人,问x,y是否是同类人,坏人只会说谎话,好人只会说实话. 最后问能否得出全部的好人 ...

  10. Redis缓存穿透、缓存雪崩、并发问题分析与解决方案

    (一)缓存和数据库间数据一致性问题 分布式环境下(单机就不用说了)非常容易出现缓存和数据库间的数据一致性问题,针对这一点的话,只能说,如果你的项目对缓存的要求是强一致性的,那么请不要使用缓存.我们只能 ...