1 public String content() {
2 try {
3 Configuration configuration = new Configuration(Configuration.getVersion());
4
5 // ’加载模板文件
6 // ‘模板内容,这里测试时使用简单的字符串作为模板
7 String templateString = "" + "<html>\n" + " <head></head>\n" + " <body>\n"
8 + " <font color='red' size='30'>名称:${name}</font>\n" + " </body>\n" + "</html>";
9
10 // ’创建模板加载器
11 StringTemplateLoader templateLoader = new StringTemplateLoader();
12
13 // ‘ 存入模板
14 templateLoader.putTemplate("template", templateString); // template = 虚拟名称, 用来当作获取静态文件的key
15
16 // ’加载模板加载器
17 configuration.setTemplateLoader(templateLoader);
18
19 // ‘ 得到模板
20 Template template = configuration.getTemplate("template", "utf-8");
21
22 // ’创建模型数据
23 Map<String, Object> map = new HashMap<String, Object>();
24
25 map.put("name", "张三");
26
27 // ‘执行静态化 方式1 获取静态化内容
28 String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
29 System.out.println(content);// 静态化内容
30 return content;
31 } catch (Exception e) {
32 return null;
33 }
34
35 }