Spring整合FreeMarker配置

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<!-- 配置freeMarker的模板路径 -->
	<bean id="freemarkerConfig"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<property name="templateLoaderPath" value="/WEB-INF/view/" />
		<property name="freemarkerVariables">
			<map>
				<entry key="xml_escape" value-ref="fmXmlEscape" />
				<entry key="webRoot" value="${webRoot}"></entry>
				<entry key="jsRoot" value="${jsRoot}"></entry>
			</map>
		</property>
		<!-- FreeMarker默认每隔5秒检查模板是否被更新,如果已经更新了,就会重新加载并分析模板。 但经常检查模板是否更新可能比较耗时。如果你的应用运行在生产模式下,而且你预期模板不会经常更新, 
			则可以将更新的延迟时间延长至一个小时或者更久。 可以通过为freemarkerSettings属性设置template_update_delay达到这一目的 -->
		<property name="freemarkerSettings">
			<props>
				<prop key="template_update_delay">3600</prop>
				<prop key="tag_syntax">auto_detect</prop><!-- 设置标签类型 两种:[] 和 <> 。[] 这种标记解析要快些 -->
				<prop key="default_encoding">UTF-8</prop>
				<prop key="output_encoding">UTF-8</prop>
				<prop key="locale">zh_CN</prop>
				<prop key="date_format">yyyy-MM-dd</prop>
				<prop key="time_format">HH:mm:ss</prop>
				<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
				<prop key="number_format">#</prop><!-- 设置数字格式 以免出现 000.00 -->
				<prop key="classic_compatible">true</prop><!-- 可以满足一般需要。默认情况变量为null则替换为空字符串,如果需要自定义,写上${empty!"EmptyValue of fbysss"}的形式即可  -->
				<prop key="template_exception_handler">html_debug</prop><!-- ignore,debug,html_debug,rethrow -->
			</props>
		</property>
		<!-- 一下语句可以也可以配置freemarkerSettings属性,代码更为简洁 -->
		<!-- <property name="freemarkerSettings" ref="freemarkerConfiguration"></property> -->
	</bean>
	
	<bean id="freemarkerConfiguration"
		class="org.springframework.beans.factory.config.PropertiesFactoryBean">
		<property name="location" value="classpath:freemarker.properties" />
	</bean>

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

	<!-- 配置freeMarker视图解析器 -->
	<!-- FreeMarker视图解析 如返回student,在这里配置后缀名ftl和视图解析器 -->
	<bean id="viewResolver"	class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">

		<property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
		<property name="cache" value="true"/>
		<property name="suffix" value=".ftl" />
		<property name="contentType" value="text/html;charset=UTF-8" />
		<property name="requestContextAttribute" value="request" />
		<!-- 将请求和会话属性作为变量暴露给FreeMarker模板使用。要做到这一点,可以设置exposeRequestAttributes或者exposeSessionAttributes为true -->
		<property name="exposeRequestAttributes" value="true" />
		<property name="exposeSessionAttributes" value="true" />
		<!-- 使用这些宏,必须设置FreeMarkerViewResolver的exposeMacroHelpers属性为true -->
		<property name="exposeSpringMacroHelpers" value="true" />
	</bean>

	<context:component-scan base-package="cn.com.tcgroup.freemarker">
		<context:include-filter type="regex" expression=".*.web.*" />
	</context:component-scan>

	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

	<context:component-scan base-package="cn.com.tcgroup.freemarker.*.dao.impl" />
	<context:component-scan base-package="cn.com.tcgroup.freemarker.*.service.impl" />

</beans>


freemarker.properties文件:

tag_syntax=auto_detect
template_update_delay=60
default_encoding=UTF-8
output_encoding=UTF-8
locale=zh_CN
date_format=yyyy-MM-dd
time_format=HH:mm:ss
datetime_format=yyyy-MM-dd HH:mm:ss
classic_compatible=true
template_exception_handler=ignore


【SpringMVC】根据请求处理资源表述
Spring mvc3 jackson输出null为空字符串、单引号、字段和数字加引号问题

猜你喜欢

转载自songjianyong.iteye.com/blog/1462208
今日推荐