1.配置freemarker全局属性
public static Configuration getConfiguration(){
if(configuration==null){
Configuration temp = new Configuration();
temp.setEncoding(Locale.getDefault(), "UTF-8");
temp.setDefaultEncoding("UTF-8");
temp.setDateFormat("yyyy-MM-dd");
temp.setTimeFormat("HH:mm:ss");
temp.setDateTimeFormat("yyyy-MM-dd HH:mm:ss");
temp.setNumberFormat("0.####");
configuration = temp;
}
return configuration;
}
可以在java代码里配置freeMarker的配置文件,也可以在spring整合freeMarker时,在配置文件里配置。
2.生成页面
Template template = null;
String ftlFile = basePath + "/" + fileName + ".ftl";
String htmlFile = basePath + "/" + fileName + ".html";
try {
template = cfg.getTemplate(ftlFile);
} catch (FileNotFoundException e) {
Logger.BIZ_LOG.error("模板文件[" + ftlFile + "]未找到,不生成文件");
return;
}
template.setEncoding("UTF-8");
//先生成html文件
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile), "UTF-8"));
//再根据ftl模板文件填充html
template.process(rootMap, out);
这是根据ftl生成对应的html页面,在使用SpringMVC3时,当freeMarker作为模板时(从Web应用程序发送它返回给Web浏览器(mvc,view层)),看了源码暂时没看懂,慢慢来吧。