方法一 直接在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. maven更换下载镜像源-解决下载慢问题

    Maven是当前流行的项目管理工具,但官方的库在国外经常连不上,连上也下载速度很慢.国内oschina的maven服务器很早之前就关了.今天发现阿里云的一个中央仓库,亲测可用. 1 <mirro ...

  2. AIDL与Binder的区别

    Binder是一个远程对象的基础类,核心部分是远程调用机制,这部分是由IBinder定义的. 它是对IBinder类的实现,其中IBinder类提供了这样一个类的标准的本地化实现方式. 大多数开发者不 ...

  3. 未能加载文件或程序集 Ninject.Web.Common, Version=3.2.0.0

    在Web MVC4中使用NuGet添加NInject.MVC4后,编译报了个警告: 发现同一依赖程序集的不同版本之间存在冲突 然后在运行时就运行到var kernel = new StandardKe ...

  4. Flume-NG中的Channel与Transaction关系(原创)

    在sink和source中(不管是内置还是自定义的),基本都有如下代码,这些代码在sink中的process方法中,而在source中自己不需要去写,在source中getChannelProcess ...

  5. Sub-process /usr/bin/dpkg returned an error code (1) 如何解决

    cd /var/lib/dpkg sudo mv info info.bak sudo mkdir info sudo dpkg --configure -a sudo apt-get install ...

  6. Strust2遇到的问题

    前端发一次请求,后台执行execute方法多次,最后发现Acion类继承ActionSupport类,且覆盖了execute,当用户数量一上来就会出现执行多次的BUG,所以千万要注意不能给此方法加An ...

  7. JVM安全退出(如何优雅的关闭java服务)

    https://tech.imdada.cn/2017/06/18/jvm-safe-exit/?utm_source=tuicool&utm_medium=referral 背景 用户:货都 ...

  8. 遇到不确定的json格式

    我们在调用webservice接口,或者http接口时,返回的json数据,有时候会因为情况不同,返回的数据格式也不一样. 比如我在调用增加档案接口时,传入要添加的档案id,如果成功了,success ...

  9. 扫描局域网中Gogs服务器(ruby)

    scanGogs.rb #!/usr/bin/env ruby require 'net/http' require 'thread' require 'english' # config CONFI ...

  10. IOS-内存分析

    一.内存分析 1.静态分析(Analyze) 不运行程序, 直接检测代码中是否有潜在的内存问题(不一定百分百准确, 仅仅是提供建议) 结合实际情况来分析, 是否真的有内存问题 2.动态分析(Profi ...