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


[url=http://alvez.blog.51cto.com/7711135/1412690]【SpringMVC】根据请求处理资源表述[/url]
[url=http://www.360doc.com/content/13/0514/17/11947209_285403916.shtml]Spring mvc3 jackson输出null为空字符串、单引号、字段和数字加引号问题[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值