1.概述
FreeMarker是一个基于模板的,用来生成输出文本的通用工具。所以我们可以用它来定制属于自己公司专属业务规范的模板,然后通过模板生成html页面,并且生成各种文件
FreeMarker是通过freeMarker.template.Configuration这个对象对木板进行加载的,然后我们通过getTemplate方法就能获取想要的模板,但是上面的Configuration对象在整个应用中必须保证唯一实例。
2.快速入门
1.导依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2.配置文件
# FreeeMarker 模板引擎配置 #是否允许HttpServletRequest属性覆盖(隐藏)控制器生成的同名模型属性。 spring.freemarker.allow-request-override=false #是否启用模板缓存。 spring.freemarker.cache=false #是否检查模板位置是否存在 spring.freemarker.check-template-location=true #模板文件编码 spring.freemarker.charset=UTF-8 spring.freemarker.content-type=text/html #设定所有request的属性在merge到模板的时候,是否要都添加到model中. spring.freemarker.expose-request-attributes=false #是否在merge模板的时候,将HttpSession属性都添加到model中 spring.freemarker.expose-session-attributes=false #设定是否以springMacroRequestContext的形式暴露RequestContext给Spring’s macro library使用 spring.freemarker.expose-spring-macro-helpers=false #模板文件后缀 spring.freemarker.suffix=.ftl #模板文件的存储位置 spring.freemarker.template-loader-path=classpath:/templates/
3.创建模板文件
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
Hello : ${message}
</body>
</html>
4.controller层代码
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class FreeMarkerController {
@GetMapping("/freeMarkerTest")
public String freeMarkerTest(Model model){
model.addAttribute("message", "FreeMarker来喽");
return "hello";//注意这个hello是你ftl文件的前面的名字
}
}
5.结果(使用swagger测试)
3.生成文件
1.生成html文件
controller层代码:
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
@Controller
public class FreeMarkerController {
@GetMapping("/generateHTMLFileTest")
@ResponseBody
public void generateHTMLFileTest(HttpServletRequest req) throws IOException, TemplateException {
//实例化模板对象
Configuration configuration = new Configuration();
configuration.setClassForTemplateLoading(this.getClass(), "/templates/");
configuration.setDefaultEncoding("UTF-8");//设置模板的编码格式
Template template = configuration.getTemplate("hello.ftl");//加载模板对象
//设置数据模型
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("message", "FreeMarker来喽");
//编辑文件的信息
File htmlFile = new File("D:\\idea_saved\\Springboot_jicheng", System.currentTimeMillis() + ".html");//使用时间戳创建文件
//获取文件流,生成文件
FileWriter fileWriter = new FileWriter(htmlFile);
template.process(hashMap,fileWriter);
//关闭流
fileWriter.flush();
fileWriter.close();
return;
}
}
结果: