FreeMaker 使用简介

很好用的模板语言,例如可以用来生成发送邮件的模板。使用${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());
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值