方法一 直接在spring-servlet.xml 中进行配置

    <bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:freemarker.properties" />
</bean>
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPaths" value="/templates" />
<property name="defaultEncoding" value="utf-8" />
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">0</prop>
<prop key="url_escaping_charset">UTF-8</prop>
<prop key="locale">UTF-8</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="date_format">yyyy-MM-dd</prop>
<prop key="number_format">#.##</prop>
<prop key="classic_compatible">true</prop>
</props>
</property>
<property name="freemarkerVariables">
<map>
<entry key="BasePath" value="${base.path}" />
<entry key="IncPath" value="${web.root}" />
<entry key="xml_escape" value-ref="fmXmlEscape" />
</map>
</property>
</bean> <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" />

freemarker.properties

base.path=localhost:8080
web.root=www.springmvc.com
 
 
方法二(推荐)自定义一个视图,继承FreeMarkerView
public class MyFreeMarkerView extends FreeMarkerView {
private static final String CONTEXT_PATH = "base"; @Override
protected void exposeHelpers(Map<String, Object> model, HttpServletRequest request) throws Exception {
model.put(CONTEXT_PATH, request.getContextPath());
model.put("cxb", "caoxiaobo");
super.exposeHelpers(model, request);
}
}
    <!--视图解释器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
     <!-- 自定义视图 -->
     <property name="viewClass">
<value>com.view.freemarker.MyFreeMarkerView</value>
</property>
<property name="cache" value="true" />
<!-- <property name="prefix" value="/" /> -->
<property name="suffix" value=".ftl"/>
<property name="contentType" value="text/html;charset=UTF-8"></property>
</bean>
 
方法三 自定义一个 FreeMarkerConfigurer 继承 FreeMarkerConfigurationFactory 并实现FreeMarkerConfig, InitializingBean, ResourceLoaderAware, ServletContextAware
// 这里其实就是完整的替换了FreeMarkerConfigurer
public class MyFreeMarkerConfigurer extends FreeMarkerConfigurationFactory
implements FreeMarkerConfig, InitializingBean, ResourceLoaderAware, ServletContextAware {
private Configuration configuration; private TaglibFactory taglibFactory; @Override
public void afterPropertiesSet() throws IOException, TemplateException {
if (this.configuration == null) {
this.configuration = createConfiguration();
}
SimpleHash model = new SimpleHash();
model.put("baseUrl", "www.springmvc.com");
this.configuration.setAllSharedVariables(model);
} // ... 省略很多方法
}
    <bean id="freemarkerConfig" class="com.view.freemarker.MyFreeMarkerConfigurer">
<property name="templateLoaderPath" value="/" />
<property name="freemarkerSettings">
<props>
<prop key="locale">zh_CN</prop>
<prop key="template_update_delay">0</prop>
<prop key="default_encoding">UTF-8</prop>
<prop key="number_format">0.##########</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="classic_compatible">true</prop>
<prop key="template_exception_handler">ignore</prop>
</props>
</property>
</bean>

springmvc freemarker 全局变量的三种配置方式的更多相关文章

  1. SpringMVC中HandlerMapping的三种配置方式

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE beans PUBLIC "-/ ...

  2. tomcat下jndi的三种配置方式

    jndi(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API.命名服务将名称和对象联系起来,使得我们可以用 ...

  3. IIS下PHP的三种配置方式比较

    在Windows IIS 6.0下配置PHP,通常有CGI.ISAPI和FastCGI三种配置方式,这三种模式都可以在IIS 6.0下成功运行,下面我就讲一下这三种方式配置的区别和性能上的差异. 1. ...

  4. 【转】tomcat下jndi的三种配置方式

    jndi(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API.命名服务将名称和对象联系起来,使得我们可以用 ...

  5. 【jdbc】【c3p0】c3p0三种配置方式【整理】

    c3p0三种配置方式 c3p0的配置方式分为三种,分别是1.setters一个个地设置各个配置项2.类路径下提供一个c3p0.properties文件3.类路径下提供一个c3p0-config.xml ...

  6. spring Bean的三种配置方式

    Spring Bean有三种配置方式: 传统的XML配置方式 基于注解的配置 基于类的Java Config 添加spring的maven repository <dependency> ...

  7. Hive metastore三种配置方式

    http://blog.csdn.net/reesun/article/details/8556078 Hive的meta数据支持以下三种存储方式,其中两种属于本地存储,一种为远端存储.远端存储比较适 ...

  8. c3p0三种配置方式(automaticTestTable)

    c3p0的配置方式分为三种,分别是http://my.oschina.net/lyzg/blog/551331.setters一个个地设置各个配置项2.类路径下提供一个c3p0.properties文 ...

  9. MyEclipse中web服务器的三种配置方式

    初学Javaweb开发的人们都会遇到一个问题,就是服务器环境的搭建配置问题.下面介绍三种服务器的搭建方式. 直接修改server.xml文件 当你写了一个web应用程序(jsp/servlet),想通 ...

随机推荐

  1. postman(谷歌) httprequester(火狐)

    http://www.cnblogs.com/s380774061/p/4624326.html @an http://www.tuicool.com/articles/67Rnaej 测试文档券栈 ...

  2. PHP学习之路(一)

    先前对PHP的学习研究,今总结写下心得!!

  3. LeetCode第[62]题(Java):Unique Paths 及扩展

    题目:唯一路径(机器人走方格) 难度:Medium 题目内容: A robot is located at the top-left corner of a m x n grid (marked 'S ...

  4. shell read 命令 (转)

    read命令 -p(提示语句) -n(字符个数) -t(等待时间) -s(不回显) 1.基本读取read命令接收标准输入(键盘)的输入,或其他文件描述符的输入(后面在说).得到输入后,read命令将数 ...

  5. 实现QQ抽屉效果

    代码: #coding: utf-8 from PyQt4.QtCore import * from PyQt4.QtGui import * import sys QTextCodec.setCod ...

  6. 图解MySQL 内连接、外连接、左连接、右连接、全连接

    用两个表(a_table.b_table),关联字段a_table.a_id和b_table.b_id来演示一下MySQL的内连接.外连接( 左(外)连接.右(外)连接.全(外)连接). MySQL版 ...

  7. Mac下webpack安装

    最近开始接触构建工具webpack,公司电脑是 windows,而我自己的呢是mac.本来以为在自己电脑安装很简单,但是出了点问题,所以写出来分享下. 这里用npm的方式安装,首先你要安装node.j ...

  8. PostgreSQL.conf文件配置详解[转]

    一.连接配置与安全认证 1.连接Connection Settings   listen_addresses (string) 这个参数只有在启动数据库时,才能被设置.它指定数据库用来监听客户端连接的 ...

  9. objective-c和java下解析对象类型和数组类型JSON字符串

    首先讲objective-c如何实现: 这里需要用到2个插件,一个是JSONKit,另一个是Jastor,一共包含6个文件,3个.h头文件和3个.m实现文件.在ARC的工程中如何导入不支持ARC的第三 ...

  10. JAVA并发全景图1.1版本

    感谢微信群"Spring Boot那些事"兄弟们的热心整理和总结