很好用的模板语言,例如可以用来生成发送邮件的模板。使用${value}来封装变量。
以下是从官方说明中截取的用法,非常简单。
参考Spring的FreeMaker Template实现,使用Spring的Resource包装FreeMaker的template directory,重写了下面一个工具类。
以下是从官方说明中截取的用法,非常简单。
<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());
}
}