springmvc freemarker 全局变量的三种配置方式
方法一 直接在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
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
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 全局变量的三种配置方式的更多相关文章
- SpringMVC中HandlerMapping的三种配置方式
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE beans PUBLIC "-/ ...
- tomcat下jndi的三种配置方式
jndi(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API.命名服务将名称和对象联系起来,使得我们可以用 ...
- IIS下PHP的三种配置方式比较
在Windows IIS 6.0下配置PHP,通常有CGI.ISAPI和FastCGI三种配置方式,这三种模式都可以在IIS 6.0下成功运行,下面我就讲一下这三种方式配置的区别和性能上的差异. 1. ...
- 【转】tomcat下jndi的三种配置方式
jndi(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API.命名服务将名称和对象联系起来,使得我们可以用 ...
- 【jdbc】【c3p0】c3p0三种配置方式【整理】
c3p0三种配置方式 c3p0的配置方式分为三种,分别是1.setters一个个地设置各个配置项2.类路径下提供一个c3p0.properties文件3.类路径下提供一个c3p0-config.xml ...
- spring Bean的三种配置方式
Spring Bean有三种配置方式: 传统的XML配置方式 基于注解的配置 基于类的Java Config 添加spring的maven repository <dependency> ...
- Hive metastore三种配置方式
http://blog.csdn.net/reesun/article/details/8556078 Hive的meta数据支持以下三种存储方式,其中两种属于本地存储,一种为远端存储.远端存储比较适 ...
- c3p0三种配置方式(automaticTestTable)
c3p0的配置方式分为三种,分别是http://my.oschina.net/lyzg/blog/551331.setters一个个地设置各个配置项2.类路径下提供一个c3p0.properties文 ...
- MyEclipse中web服务器的三种配置方式
初学Javaweb开发的人们都会遇到一个问题,就是服务器环境的搭建配置问题.下面介绍三种服务器的搭建方式. 直接修改server.xml文件 当你写了一个web应用程序(jsp/servlet),想通 ...
随机推荐
- Sublime Text3 使用记录
Sublime Text 3 使用记录 来看下本文的大纲吧 介绍下,我的环境 操作系统:win10 64bit sublime Text 3 版本:3143 那么就开始啦. 一. ...
- HTML5笔记学习(canvas)
来源于<HTML5高级程序设计> css3圆角 border-radius旋转变换 transform:rotate(); 变换 transformation动画 animation过度 ...
- python 爬虫001-http请求过程
HTTP 请求流程 一次完整的HTTP请求过程从TCP三次握手建立连接成功后开始,客户端按照指定的格式开始向服务端发送HTTP请求,服务端接收请求后,解析HTTP请求,处理完业务逻辑,最后返回一个HT ...
- Codeforces Round #394 (Div. 2) D. Dasha and Very Difficult Problem
D. Dasha and Very Difficult Problem time limit per test:2 seconds memory limit per test:256 megabyte ...
- scrapy的简单使用以及相关设置属性的介绍
0. 楔子(一个最简单的案例) 1.scrapy.Spider scrapy.spiders.Spider name allowed_domains start_urls custom_setting ...
- springmvc跨域(转)
跨域资源共享 CORS 详解 原文链接:http://www.ruanyifeng.com/blog/2016/04/cors.html 作者: 阮一峰 日期: 2016年4月12日 CORS是 ...
- PostgreSQL内存配置记录
PostgreSQL内存配置,参考了其他人的总结,再加上自己的一些体会,做个记录. postgresql的内存分配主要由shared_buffers.temp_buffers.work_mem.mai ...
- java-正则表达式判断手机号
要更加准确的匹配手机号码只匹配11位数字是不够的,比如说就没有以144开始的号码段, 故先要整清楚现在已经开放了多少个号码段,国家号码段分配如下: 移动:134.135.136.137.138.139 ...
- Pro Android学习笔记 ActionBar(1):Home图标区
Pro Android学习笔记(四八):ActionBar(1):Home图标区 2013年03月10日 ⁄ 综合 ⁄ 共 3256字 ⁄ 字号 小 中 大 ⁄ 评论关闭 ActionBar在A ...
- Android OpenCSV
OpenCSV https://sourceforge.net/projects/opencsv/ 使用参考 http://stackoverflow.com/questions/16672074/i ...