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),想通 ...
随机推荐
- bootstrap下拉列表重置联动
/**添加&修改时--获取机柜号**/ function BindSelectJgh(jiguiColumnIdD, jiguiNumberIdD) { var jid = !jiguiCol ...
- QT 进度条 QProgressDialog
//默认构造函数 参数依次为,对话框正文,取消按钮名称,进度条范围,及所属 QProgressDialog *progressDlg=new QProgressDialog( QStringLiter ...
- throws和throw的用法例子以及检测和非检查异常
throws E1,E2,E3 只是告诉程序这个方法可能会抛出这些个异常,方法的调用者可能要处理这些异常.而这些异常E1,E2,E3可能是该函数体产生的. 而throw是明确之处这个地方要抛出这个异常 ...
- DPDK在OpenStack中的实现
随着云计算与大数据的快速发展,其对数据中心网络的性能和管理提出了更高的要求,但传统云计算架构存在多个I/O瓶颈,由于云平台基本上是采用传统的X86服务器加上虚拟化方式组建,随着40G.100G高速网卡 ...
- 配置mybatis-config.xml出现过很诡异的现象
1 首先得保证包的导入正确 2 然后如果把mybatis-config.xml放在src的某个文件夹下,最后能够build path 3 之后一直报 Archive for required libr ...
- MongoDB在linux下的启动
最近公司数据库用到MongoDB,而之前只关注知道它是分布式非关系数据库,数据以文档的形式存储,数据格式是类似json的bson格式.而对于具体用法以及java如何调用并没有过多接触,今天花费一天的时 ...
- LeetCode OJ:Jump Game(跳跃游戏)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- SpringCloud 教程 | 第十四篇: 服务注册(consul)
版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 ,博主地址:http://blog.csdn.net/forezp. http://blog.csdn.net/forezp/art ...
- 初识Linux(五)--VI/VIM编辑器
我们操作文件,终究离不开编辑文件,对文件内容的编辑,Linux系统下,我们通常使用VI/VIM来编辑文件.VI是每个Linux都会自带的文本编辑器,VIM是VI的增强版,可能有些发行版本没有自带,可以 ...
- Flask的简单认识
Flask的简单认识 Flask是轻量级的框架,适用于简单的程序 与Django的比较: Django: 无socket,中间件,路由,视图(CBV,FBV),模板,ORM, cookie,sessi ...