标签:views nal 就是 lin mars 使用 iat got reac
一.什么是内容协商
<!--1、检查扩展名(如my.pdf);2、检查Parameter(如my?format=pdf);3、检查Accept Header-->
<bean id= "contentNegotiationManager" class= "org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<!-- 扩展名至mimeType的映射,即 /user.json => application/json -->
<property name= "favorPathExtension" value= "true" />
<!-- 用于开启 /userinfo/123?format=json 的支持 -->
<property name= "favorParameter" value= "true" />
<property name= "parameterName" value= "format"/>
<!-- 是否忽略Accept Header -->
<property name= "ignoreAcceptHeader" value= "false"/>
<!--扩展名到MIME的映射;favorPathExtension, favorParameter是true时起作用 -->
<property name= "mediaTypes">
<value>
json=application/json
xml=application/xml
html=text/html
</value>
</property>
<!--<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>-->
<!-- 默认的content type ,在没有扩展名和参数时即: "/user/1" 时的默认展现形式 -->
<property name= "defaultContentType" value= "text/html" />
</bean>
视图定义:
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="0"/>
<property name="contentNegotiationManager" ref="contentNegotiationManager"/>
<property name="viewResolvers">
<list>
<!-- 这个类用于jsp视图解析 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
</bean>
<!-- for application/xml -->
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<property name="marshaller">
<bean class="org.springframework.oxm.castor.CastorMarshaller">
<property name="validating" value="false"></property>
</bean>
</property>
</bean>
</list>
</property>
</bean>
在mvc:annotation-driven里面配置使用内容协商
<mvc:annotation-driven
conversion-service= "conversionService"
content-negotiation-manager= "contentNegotiationManager”/>
spring ContentNegotiationManagerFactoryBean 内容协商
标签:views nal 就是 lin mars 使用 iat got reac
原文地址:https://www.cnblogs.com/jtlgb/p/10102868.html