先参考文章:【Spring MVC】集成 FreeMarker ,把 FreeMarker jar包 配置好。
1. 使用步骤
- 第一步:创建一个Configuration对象,直接new一个对象。构造方法的参数就是freemarker对应的版本号。
- 第二步:设置模板文件所在的路径。
- 第三步:设置模板文件使用的字符集。一般就是utf-8。
- 第四步:加载一个模板,创建一个模板对象。
- 第五步:创建一个模板使用的数据集,可以是pojo也可以是map,一般是Map。
- 第六步:创建一个Writer对象,一般创建一FileWriter对象,指定生成的文件名。
- 第七步:调用模板对象的process方法输出文件。
- 第八步:关闭流。
@Test
public void makeFile() throws Exception {
// 第一步:创建一个Configuration对象,直接new一个对象。构造方法的参数就是freemarker对应的版本号。
Configuration configuration = new Configuration(Configuration.VERSION_2_3_31);
// 第二步:设置模板文件所在的路径。
configuration.setDirectoryForTemplateLoading(new File("D:/java/IdeaProjects/jianbao/SpringMVC-Demo/src/main/webapp/WEB-INF/view"));
// 第三步:设置模板文件使用的字符集。一般就是utf-8.
configuration.setDefaultEncoding("utf-8");
// 第四步:加载一个模板,创建一个模板对象。
Template template = configuration.getTemplate("hello.ftl");
// 第五步:创建一个模板使用的数据集,可以是pojo也可以是map。一般是Map。
Map<String, Object> dataModel = new HashMap<>();
// 向数据集中添加数据
dataModel.put("hello", "this is my first freemarker test.");
// 第六步:创建一个Writer对象,一般创建一FileWriter对象,指定生成的文件名。
Writer out = new FileWriter(new File("D:/123/hello.html"));
// 第七步:调用模板对象的process方法输出文件。
template.process(dataModel, out);
// 第八步:关闭流。
out.close();
}
2. 模板语法
(1) 访问map的key
${key}
(2) 访问pojo中的属性
Student对象。学号、姓名、年龄、
${key.property}
<ul>
<li>取学生的信息:</li>
<li>学号:${stu.id}</li>
<li>姓名:${stu.name}</li>
<li>年龄:${stu.age}</li>
</ul>
(3) 取集合中的数据
循环使用格式:
<#list 要循环的数据 as 循环后的数据>
</#list>
示例:
<ol>
<dt>学生列表</dt>
<#list studentList as student>
<dd>学号:${student.id}</dd>
<dd>姓名:${student.name}</dd>
<dd>年龄:${student.age}</dd>
</#list>
</ol>
ArrayList<Student> studentList = new ArrayList<>();
Student stu1 = new Student();
stu1.setId(2);
stu1.setName("张三");
stu1.setAge(19);
studentList.add(stu1);
Student stu2 = new Student();
stu2.setId(3);
stu2.setName("李四");
stu2.setAge(20);
studentList.add(stu2);
Student stu3 = new Student();
stu3.setId(4);
stu3.setName("王二");
stu3.setAge(21);
studentList.add(stu3);
dataModel.put("studentList", studentList);
(4) 取循环中的下标
<#list studentList as student>
${student_index}
</#list>
(5) 判断
<#if student_index % 2 == 0> <#else> </#if>
(6) 日期类型格式化
直接取值:${date}(date是属性名)如果传来的是一个Date型数据会报错
${birthday?date} 2016-9-13
${birthday?time} 17:53:55
${birthday?datetime} 2016-9-13 17:53:55
示例:
当前日期:${addTime?date} <br/>
当前时间:${addTime?time} <br/>
当前日期和时间:${addTime?datetime} <br/>
自定义日期格式:${addTime?string("yyyyMM/dd HH:mm:ss")} <br/>
(7) Null 值处理
如果直接取一个不存在的值(值为null)时会报异常
${aaa}
处理:${aaa!"默认值"} 或者 ${aaa!} 代表空字符串
null值处理:${myVal!"myVal为null"} <br/>
null值处理:
<#if myVal2??>
myVal2不为空时。。。
<#else>
myVal2为空时###
</#if>
(8) Include标签
<#include "模板名称">
<#include "hello.ftl"/>
3. 使用Spring的 FreeMarker 配置对象,生成静态页面
(1) 业务逻辑:
- 1、从spring容器中获得FreeMarkerConfigurer对象。
- 2、从FreeMarkerConfigurer对象中获得Configuration对象。
- 3、使用Configuration对象获得Template对象。
- 4、创建数据集
- 5、创建输出文件的Writer对象。
- 6、调用模板对象的process方法,生成文件。
- 7、关闭流。
(2) Java代码
package com.jianbao.controller;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
@Controller
public class HtmlController {
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;
@GetMapping("/make_html")
@ResponseBody
public String makeHtml() throws Exception {
// 1.从Spring容器中获取FreeMakerConfigurer对象。
// 2.从FreeMarkerConfigurer对象中获取Configuration对象
Configuration configuration = freeMarkerConfigurer.getConfiguration();
// 3.使用Configration对象获取Template对象
Template template = configuration.getTemplate("hello.ftl");
// 4.创建数据集
Map<String, Object> dataModel = new HashMap<>();
dataModel.put("title", "用户个人信息");
dataModel.put("userName", "温建宝");
dataModel.put("age", 18);
// 5.创建输出文件的Writer对象。
Writer out = new OutputStreamWriter(new FileOutputStream("D:/123/hello.html"), StandardCharsets.UTF_8);
// 6.调用模板对象的process()方法,生成文件。
template.process(dataModel, out);
// 7.关闭流
out.close();
return "ok";
}
@RequestMapping("/")
@ResponseBody
public String index() {
return "default page";
}
}