1 回答

TA貢獻(xiàn)2039條經(jīng)驗 獲得超8個贊
<!-- ______________________________________多視圖處理器______________________________________
-->
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<!-- 根據(jù)URL中的文件擴(kuò)展名確定MIME(如userList.xml,userList.json) -->
<property name="favorPathExtension" value="true" />
<!-- 則根據(jù)請求參數(shù)的值確定MIME類型,默認(rèn)的請求參數(shù)是format,可以通過parameterName屬性指定一個自定義的參數(shù) -->
<property name="favorParameter" value="true" />
<!-- 則采用Accept請求報文頭的值確定MIME類型。由于不同的瀏覽器產(chǎn)生的Accept頭都是不一樣的,所以一般不建議采用Accept確定MIME類型 -->
<property name="ignoreAcceptHeader" value="true" />
<property name="useJaf" value="false" />
<property name="defaultContentType" value="text/html" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</map>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="contentNegotiationManager" ref="contentNegotiationManager" />
<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/views/" />
<property name="suffix" value=".jsp" />
<property name="contentType" value="text/html; charset=UTF-8"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<!-- for application/json -->
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
<property name="extractValueFromSingleKeyModel" value="true" />
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="serializationInclusion">
<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
</property>
</bean>
</property>
</bean>
<!-- for application/xml -->
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<property name="marshaller">
<bean class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="autodetectAnnotations" value="true"/>
<property name="streamDriver">
<bean class="com.thoughtworks.xstream.io.xml.StaxDriver" />
</property>
</bean>
</property>
</bean>
</list>
</property>
</bean>
添加回答
舉報