java代码
package com.s2freemarker.exer.test;
import java.io.File;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
public class HelloFreeMarker {
private Configuration cfg ;
public void init() throws Exception {
cfg = new Configuration();
cfg.setDirectoryForTemplateLoading( new File( "src\\com\\s2freemarker\\exer\\templates" ));
}
public void process() throws Exception{
Map<Object,Object> root = new HashMap<Object,Object>();
root.put( "name" , "FreeMarker" );
root.put( "msg" , "First example for freemarker ." );
Template t = cfg.getTemplate( "test.ftl" );
t.process(root, new OutputStreamWriter(System.out)); }
/**
* @param args
*/
public static void main(String[] args) throws Exception{
HelloFreeMarker hfm = new HelloFreeMarker();
hfm.init();
hfm.process();
}
}
ftl 模板test.ftl
<html>
<head><title>FreeMarker</title></head>
<body>
${message}
</body>
</html>