Spring 梳理 - ContentNegotiatingViewResolver
- ContentNegotiatingViewResolver,这个视图解析器允许你用同样的内容数据来呈现不同的view。它支持如下面描述的三种方式:
- 1)使用扩展名
http://localhost:8080/employees/nego/Jack.xml 返回结果为XML
http://localhost:8080/employees/nego/Jack.json 返回结果为JSON
http://localhost:8080/employees/nego/Jack 使用默认view呈现,比如JSP2) 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
- 1)使用扩展名
- ContentNegotiatingViewResolver 可以一个@RequestMapping,返回多个view。
- 配置
<property name="favorPathExtension" value="true"></property> 是否启用扩展名支持,默认是true
<property name="favorParameter" value="false"></property> 是否启用参数支持,默认是true
<property name="ignoreAcceptHeader" value="true"></property> 是否忽略掉accept header,默认就是false
- spring 给我们提供了 ContentNegotiationManagerFactoryBean,这是推荐的方式。 配置上类似。
<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><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>defaultViews的作用:在defaultViews里注册的视图会在ContentNegotiationViewResolver中注册自己支持的内容类型
- 由ContentNegotiationManagerFactoryBean的mediaTypes决定响应的内容类型
- 当contentNegotiationManager决定好响应的内容类型后,ContentNegotiationViewResolver就会根据该内容类型选择一个兼容的View进行渲染输出
- 一旦有View对请求内容匹配,就直接渲染输出,不会进行ViewResolver的查询。
- 当注册的内容类型都不兼容时,会查询viewResolver中的ViewResolver是否支持该请求,如果ViewResolver表示支持该请求,那么就由该ViewResolver负责视图渲染,如果ViewResolver表示不支持该请求,则查询下一个ViewResolver,直至所有的ViewResolver查询完毕。
- 默认是支持path 后缀拓展方式, 也支持accept 请求头,但不支持 format 参数的
Spring 梳理 - ContentNegotiatingViewResolver的更多相关文章
- Spring 梳理 - filter、interceptor、aop实现与区别 -第二篇
spring mvc中的Interceptor可以理解为是Spring MVC框架对AOP的一种实现方式.一般简单的功能又是通用的,每个请求都要去处理的,比如判断token是否失效可以使用spring ...
- Spring 梳理 - filter、interceptor、aop实现与区别 -第一篇
前言 项目中我们经常需要对RESTful api进行拦截,主流实现方法有filter.interceptor.aop,先说一下他们各自的实现. Filter AnimalFilter实现javax.s ...
- Spring 梳理 - View - JSON and XML View
实际案例json <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www. ...
- Spring 梳理 - 视图解析器 VS 视图(View,ViewResolver)
View View接口表示一个响应给用户的视图,例如jsp文件,pdf文件,html文件等 该接口只有两个方法定义,分别表明该视图的ContentType和如何被渲染 Spring中提供了丰富的视图支 ...
- Spring 梳理 - javaConfig在App和webApp中的应用
package com.dxz.demo.configuration; import org.springframework.context.annotation.Configuration; @Co ...
- Spring 梳理 - @Component
使用@Component注解,表示该类定义为Spring管理Bean,使用默认value(可选)属性表示Bean标识符.如果不指定标识符,默认为首字母小写类名.例如类UserController的标识 ...
- Spring 梳理 - @Autowired VS @Resource
Autowired @Autowired顾名思义,就是自动装配,其作用是为了消除代码Java代码里面的getter/setter与bean属性中的property.当然,getter看个人需求,如果私 ...
- Spring 梳理 - AOP那些学术概念—通知、增强处理连接点(JoinPoint)切面(Aspect)
Spring AOP那些学术概念—通知.增强处理连接点(JoinPoint)切面(Aspect) 1.我所知道的AOP 初看起来,上来就是一大堆的术语,而且还有个拉风的名字,面向切面编程,都说是 ...
- Spring 梳理 - JavaConfig、SPI、SCI、SpringSCI、WebApplicationInitializer、AbstractAnnotationConfigDispatcherServletInitializer、WebMvcConfigurationSupport
总结1: SCI:Servlet容器(Tomcat)提供的初始化Servlet容器本身的接口,可替换web.xml SpringSCI:SpringServletContainerInitialize ...
随机推荐
- Egret白鹭开发微信小游戏分享功能
今天给大家分享一下微信分享转发功能,话不多说,直接干 方法一: 1.在egret中打开Platfrom.ts文件,添加代码如下(当然,你也可以直接复制粘贴) /** * 平台数据接口. * 由于每款游 ...
- C#数据结构_基本概念及线性表
常见的4类数据结构: 1.集合. 2.线性结构.3.树形结构.4.图状结构. 数据结构(Data Structure)简记为 DS,是一个二元组,DS = (D,R) 其中:D 是数据元素的有限集合, ...
- HDU 6044
题意略. 思路: I.对于整个区间a1,....,an,必然有一个区间[1,n]与之对应,因为a1,...,an是1,...,n的一个排列,所以在[1,n]中定然有一个最小的数字1, 如果最大的区间[ ...
- HDU 6055
题意略. 思路:要你找出所有正多边形,其实是唬人的,整点的正多边形只有正方形,具体证明可以参考 2017国家队论文集-<正多边形>-杨景钦 详见代码: #include<bi ...
- java.lang.Thread类详解
java.lang.Thread类详解 一.前言 位于java.lang包下的Thread类是非常重要的线程类,它实现了Runnable接口,今天我们来学习一下Thread类,在学习Thread类之前 ...
- Jenkins教程——从安装到部署Docker服务(二)声明式流水线HelloWorld
前言 本文通过一个声明式流水线的HelloWorld程序做一下流水线基础入门,对常用的流水线参数进行简要说明 什么是流水线 现实中的流水线 流水线比较好理解,类比于现实生活中的生产流水线,每个流程只做 ...
- Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II)
Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II) 假设按照升序排序的数组在预先未知的某个点上进 ...
- JSP学习笔记(3)——JSTL 标签库
JSP Standard Tag Lib,名为JSP标准标签库,设计的目的主要用来方便我们将数据输出,而不是使用JSP中的语法<% %> <%= %> <%! %> ...
- JAVA 泛型中的通配符 T,E,K,V,?
前言 Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许开发者在编译时检测到非法的类型. 泛型的本质是参数化类型,也就是说所操作的数据 ...
- TypeError: 'in <string>' requires string as left operand, not int
报错 Traceback (most recent call last): File "D:/PyCharm 5.0.3/WorkSpace/2.NLP/9.DL在NLP中的应用/4. Ve ...