freemarker 全局变量
场景:Spring MVC配备Freemarker全局变量,实现应用全路径
Spring MVC配置Freemarker全局变量,实现应用全路径
config.properties文件:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/test?useUnicode\=true&characterEncoding\=utf8
jdbc.username=root
jdbc.password=root
webRoot=http\://127.0.0.1\:8080/freemarker/
jsRoot=${webRoot}resources/
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
ftl页面引用全局变量:
Spring MVC配置Freemarker全局变量,实现应用全路径
<context:property-placeholder location="classpath:config.properties" ignore-unresolvable="true"/>
<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> <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> </bean> <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" />
config.properties文件:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/test?useUnicode\=true&characterEncoding\=utf8
jdbc.username=root
jdbc.password=root
webRoot=http\://127.0.0.1\:8080/freemarker/
jsRoot=${webRoot}resources/
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
ftl页面引用全局变量:
<script type="text/javascript" src="${jsRoot}jquery-1.4.2.min.js"></script>