helloworld.ftl
FreeMarker Template example: ${message}
=======================
=== County List ====
=======================
<#list countries as c>
${c_index + 1}. ${c}
</#list>
public class FTLHelloWorld {
public static void main(String[] args) {
Configuration cfg = new Configuration();
try {
Template template = cfg.getTemplate("src/helloworld.ftl");
// Build the data-model
Map<String, Object> data = new HashMap<String, Object>();
data.put("message", "Hello World!");
//List parsing
List<String> countries = new ArrayList<String>();
countries.add("India");
countries.add("United States");
countries.add("Germany");
countries.add("France");
data.put("countries", countries);
// Console output
Writer out = new OutputStreamWriter(System.out);
template.process(data, out);
out.flush();
// File output
Writer file = new FileWriter (new File("C:\\FTL_helloworld.txt"));
template.process(data, file);
file.flush();
file.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
}
}
}
输出:
FreeMarker Template example: Hello World!
=======================
=== County List ====
=======================
1. India
2. United States
3. Germany
4. France
Thus note how we passed data from Java to FTL and the same get painted. The ${message} got replaced with the message that we populated in Java. Also notice how we passed a country List<String> through Java and inside FTL we used <#list> </#list> to display its values.
原文:http://viralpatel.net/blogs/freemaker-template-hello-world-tutorial/
eclipse工程源代码:http://pan.baidu.com/share/link?shareid=1858728828&uk=3878681452