public static void main(String[] args) {
// Create your Configuration instance, and specify if up to what FreeMarker
version (here 2.3.22) do you want to apply the fixes that are not 100%
backward-compatible. See the Configuration JavaDoc for details.
Configuration configuration = new Configuration(Configuration.getVersion());
// 编码格式
configuration.setDefaultEncoding("UTF-8");
try {
// FTL(freemarker templete language)模板所在的文件夹
configuration.setDirectoryForTemplateLoading(new File("E:\\freemarker"));
// 异常梳理
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
Map<String,Object>root = new HashMap<>();
Map<String,String>user = new HashMap<>();
root.put("key",user);
user.put("name","测试");
// FTL(freemarker templete language)模板所在的文件夹的名称
Template template = configuration.getTemplate("testFreeMarker.ftl");
OutputStreamWriter writer = new OutputStreamWriter(System.out);
File file = new File("E:\\freemarker\\testFreeMarker.html");
FileWriter fileWriter = new FileWriter(file);
try {
template.process(root,fileWriter);
fileWriter.flush();
fileWriter.close();
} catch (TemplateException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}