Spring MVC Rest Json,XML多视图解析 配置

摘要: 使用spring mvc统一构建根据xxx.json,xxx.xml后缀来动态显示页面 配置文件

直接贴代码,访问会根据后缀的不同显示不同的结果

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    <!-- 静态资源映射  -->
	<mvc:resources mapping="/static/**" location="/static/" cache-period="172800"/>
	<!-- 注册mapping,class信息等... -->
	<mvc:annotation-driven />
	<!-- 开启aop -->
	<aop:aspectj-autoproxy />
	<!-- 扫描annotation -->
	<context:component-scan base-package="com.easilycms.blog" />

	<!-- 配置拦截器 -->
	<mvc:interceptors>
		<bean class="org.springframework.web.servlet.mvc.WebContentInterceptor">
			<property name="cacheSeconds" value="0" />
			<property name="useExpiresHeader" value="true" />
			<property name="useCacheControlHeader" value="true" />
			<property name="useCacheControlNoStore" value="true" />
			<property name="alwaysUseFullPath" value="true" />
		</bean>
	</mvc:interceptors>

	<!-- 多视图解析 .json .xml -->
	<bean
		class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
		<property name="order" value="1" />
		<!-- 用于开启 /userinfo/123?format=json 的支持 默认是true -->
		<property name="favorParameter" value="false" />
		<!-- 用于关闭 /userinfo/123.json 的支持 默认是true -->
		<property name="favorPathExtension" value="true" />
		<!-- 设置为true以忽略对Accept Header的支持 -->
		<property name="ignoreAcceptHeader" value="true" />
		<!-- 在没有扩展名时即: "/user/1" 时的默认展现形式 -->
		<property name="defaultContentType" value="text/html" />
		<!-- 扩展名至mimeType的映射,即 /user.json => application/json -->
		<property name="mediaTypes">
			<map>
				<entry key="json" value="application/json" />
				<entry key="xml" value="application/xml" />
			</map>
		</property>
		<property name="defaultViews">
	        <list>
	          <ref bean="jsonView" />
	          <ref bean="xmlViewer" />
	        </list>
	    </property>
	</bean>

	<!-- 设置freeMarker的配置文件路径 -->
	<bean id="freemarkerConfiguration"
		class="org.springframework.beans.factory.config.PropertiesFactoryBean">
		<property name="location" value="classpath:freemarker.properties" />
	</bean>

	<!-- 配置freeMarker的模板路径 -->
	<bean id="freemarkerConfig"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<property name="freemarkerSettings" ref="freemarkerConfiguration" />
		<property name="templateLoaderPath">
			<value>/</value>
		</property>
		<property name="freemarkerVariables">
			<map>
				<entry key="xml_escape" value-ref="fmXmlEscape" />
			</map>
		</property>
	</bean>

	<bean id="freeMarkerView"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
		<property name="order" value="2" />
		<property name="viewClass"
			value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
		<property name="prefix" value="WEB-INF/ftl/pages/" />
		<property name="suffix" value=".ftl" />
		<property name="contentType" value="text/html; charset=utf-8" />
	</bean>

	<bean name="jsonView"
		class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
		<property name="encoding">
			<value type="org.codehaus.jackson.JsonEncoding">UTF8</value>
		</property>
		<property name="extractValueFromSingleKeyModel" value="true"></property>
		<property name="contentType" value="application/json;charset=UTF-8" />
	</bean>
	
	<bean id="xmlViewer" 
		class="org.springframework.web.servlet.view.xml.MarshallingView">
		<constructor-arg>
		  <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
			<property name="classesToBeBound">
				<list>
				</list>
			</property>
		  </bean>
		</constructor-arg>
		<property name="contentType" value="application/xml;charset=UTF-8" />
	</bean>
	
	<!-- 文件上传 -->
	<bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!--1024**1024*5即5M-->
        <property name="maxUploadSize" value="5242880"/>
    </bean>

	<bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" />
</beans>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值