SpringMVC多视图配置

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"/>

 

<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">

<!-- 用于开启 /userinfo/123?format=json的支持 默认为false -->

<property name="favorParameter" value="true" />

 

<!-- 在没有扩展名和参数和accept头时的默认展现形式 -->

<property name="defaultContentType" value="text/html" />

 

<property name="useJaf" value="true"/>

 

<!-- 参数值至mimeType的映射,即 /rest?format=json json是key,application/json,并且支持标准的类型扩展名定义 -->

<property name="mediaTypes">

<value>

json=application/json

xml=application/xml

</value>

</property>

</bean>

 

<!-- 多视图 -->

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">

<property name="contentNegotiationManager" ref="contentNegotiationManager"/>

<property name="viewResolvers">

<list>

<!-- 视图解析器,根据视图的名称new ModelAndView(name),在配置文件查找对应的bean配置 -->

<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/views/jsp/" />

<property name="suffix" value=".jsp" />

</bean>

</list>

</property>

<property name="defaultViews">

<list>

<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">

<!-- 重新设置 objectMapper -->

<property name="objectMapper">

<bean class="com.fasterxml.jackson.databind.ObjectMapper">

<!-- 设置objectMapper的serializationConfig的serializationInclusion属性,以忽略null对象 -->

<property name="serializationInclusion">

<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>

</property>

</bean>

</property>

<!-- 只处理单结果对象 -->

<property name="extractValueFromSingleKeyModel" value="true" />

</bean>

<bean class="org.springframework.web.servlet.view.xml.MappingJackson2XmlView">

</bean>

</list>

</property>

</bean>




打赏

发表评论

电子邮件地址不会被公开。 必填项已用*标注

This site uses Akismet to reduce spam. Learn how your comment data is processed.