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),想通 ...
随机推荐
- SVM-支持向量机原理详解与实践
前言 去年由于工作项目的需要实际运用到了SVM和ANN算法,也就是支持向量机和人工神经网络算法,主要是实现项目中的实时采集图片(工业高速摄像头采集)的图像识别的这一部分功能,虽然几经波折,但是还好最终 ...
- LeetCode第[36]题(Java):Valid Sudoku
题目:有效的数独表 难度:Medium 题目内容: Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be ...
- 找出此产品描述中包含N个关键字的长度最短的子串
阿里巴巴笔试题:给定一段产品的英文描述,包含M个英文字母,每个英文单词以空格分隔,无其他标点符号:再给定N个英文关键词,请说明思路并变成实现方法. String extractSummary(Stri ...
- mysql 转移数据目录
由于MySql的数据库文件和日志文件比较大,导致磁盘空间不够,在添加新的磁盘之后,需要把MySql的数据转移到新挂载的目录下. 1.停止MySql服务: /etc/rc.d/init.d/mysql ...
- socket 关于同一条TCP链接数据包到达顺序的问题
转:http://blog.csdn.net/l1008610/article/details/52197602 以前作者也一直以为数据包先发的不一定先到,直到今天才意识这个问题的缺陷,数据包是不一定 ...
- python中多线程,多进程,队列笔记(一)
threading简介:If you want your application to make better use of the computational resources of multi- ...
- WPF的外观装饰类——Border
public class Border : System.Windows.Controls.Decorator 说明:在另一个元素的周围绘制边框.背景或同时绘制二者.
- java中int i 会出现i+1i吗
Java中int是32,范围是-2147483648到2147483647 所以i+1 < i 或者 i-1 > i是会出现的. int i=(int) Math.pow(2, 32); ...
- Mybatis输入和输出映射(parameterType和resultType的区别)
parameterType resultType ...
- IOS-真机相关
真机调试 Certificates 证书 Identifiers 标示符 Profiles 描述文件 一. 证书,安装在电脑上,只有安装了证书的电脑,才有可能进行真机调试. - All - De ...