很好用的模板语言,例如可以用来生成发送邮件的模板。使用${value}来封装变量。
以下是从官方说明中截取的用法,非常简单。
<html>
<head>
<title>Welcome!</title>
</head>
<body>
<h1>Welcome ${user}!</h1>
<p>Our latest product:
<a href="${latestProduct.url}">${latestProduct.name}</a>!
</body>
</html>
// Create a configuration instance.
// A Configuration instance is a central place to store the application level settings of FreeMarker.
// Also, it deals with the creation and caching of pre-parsed templates.
//Probably you will do it only once at the beginning of the application (possibly servlet) life-cycle.
Configuration cfg = new Configuration();
// Specify the data source where the template files come from.
// Here I set a file directory for it:
cfg.setDirectoryForTemplateLoading(new File("/where/you/store/templates"));
// Specify how templates will see the data-model. This is an advanced topic...
// but just use this:
cfg.setObjectWrapper(new DefaultObjectWrapper());
// Create a data-model.
// Create the root hash
Map root = new HashMap();
// Put string ``user'' into the root
root.put("user", "Big Joe");
// Create the hash for ``latestProduct''
Map latest = new HashMap();
// and put it into the root
root.put("latestProduct", latest);
// put ``url'' and ``name'' into latest
latest.put("url", "products/greenmouse.html");
latest.put("name", "green mouse");
// Get the template.
Template temp = cfg.getTemplate("test.ftl");
// Merging the template with the data-model.
Writer out = new OutputStreamWriter(System.out);
temp.process(root, out);
out.flush();
参考Spring的FreeMaker Template实现,使用Spring的Resource包装FreeMaker的template directory,重写了下面一个工具类。
import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class TemplateService {
private static Logger logger = LoggerFactory.getLogger(TemplateService.class);
private String templateLoaderPath;
private Configuration configuration;
public String getContent(String type, Map<String, String> model) throws TemplateException, IOException {
Template template = configuration.getTemplate(type);
StringWriter result = new StringWriter();
template.process(model, result);
return result.toString();
}
public String getTemplateLoaderPath() {
return templateLoaderPath;
}
public void setTemplateLoaderPath(String templateLoaderPath) throws IOException {
this.templateLoaderPath = templateLoaderPath;
configuration = new Configuration();
try {
configuration.setDirectoryForTemplateLoading(new ClassPathResource(templateLoaderPath).getFile());
} catch (Exception e) {
logger.error("Template directory is error.", e);
}
configuration.setObjectWrapper(new DefaultObjectWrapper());
}
}
分享到:
相关推荐
Freemarker简介及标签详解大全 Freemarker简介及标签使用说明
freemarker 中文文档,freemarker入门简介,含几个小实例
jsp、freemarker、velocity简介和对比。---如果你已经开发Java Web应用程序一段时间,那么对于 JVM 的 PermGen 问题可能并不陌生。由于 FreeMarker 模板不编译成类,它们不占用 PermGen 空间,并不需要一个新的类...
Freemarker简介及标签详解大全.pdf
新手学习freemarker 快速上手。
学习Freemarker简介及标签详解大全.pdf
Swagger 使用 3.1.说明: Sagger2 是一个可以根据接口定义自动生成接口 API 文档的框架 Freemarker freemarker 是一款比较优秀的模板引擎 5.1.简介 参考:http://freemarker.foofun.cn/ FreeMarker 是一款 模板...
Freemarker入门简介,资料,蛮有用的。
1概念 2指令 if, else, elseif switch, case, default, break list, break include Import compress escape, noescape assign global setting macro, nested, return t, lt, rt ...hashes内置方法
搭建项demo(maven项目[freemarker+easyui+spring+springmvc+mybatic]) ,包括各种功能,技术简介,要自己运行才行 有搭建的整个项目,1,有发短信验证码功能,paypal支付功能,登陆验证功能,excel导入导出功能 ...
什么是FreeMarker? ................................................................................................. 7 我们应该阅读什么内容? .............................................................
FreeMarker页面制作规范 页面规范的重要性 宏的简介 项目中常用宏的使用方法 页面元素规范 FreeMarker常用技巧
• 采用hibernate3+struts2+spring2+freemarker主流技术架构 • 懂html就能建站,提供最便利、合理的使用方式 • 强大、灵活的标签,用户自定义显示内容和显示方式 • 在设计上自身预先做了搜索引擎优化,增强对...
项目简介: MiniDAO是一个基于SpringJDBC和FreeMarker技术的轻量级Java持久层框架。它以简洁的设计理念实现了SQL的分离和逻辑标签功能,与Mybatis的操作体验相媲美,同时能够无缝集成至Spring Boot项目。 技术构成...
FreeMarker是一个模板引擎,一个基于模板生成文本输出的通用工具,使用纯Java编写,有兴趣的可以了解一下
1.1 简介 ............................................................................................................ 9 1.2 模板 + 数据模型 = 输出 ........................................................
1.1 简介 ............................................................................................................ 9 1.2 模板 + 数据模型 = 输出 ........................................................
1.1 简介 ............................................................................................................ 9 1.2 模板 + 数据模型 = 输出 .......................................................
1.1简介91.2 模板 + 数据模型 = 输出 91.3 数据模型一览 101.4 模板一览 13第二章 数值和类型 192.1 基本内容 192.2 类型