增加maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
注入freemarkTemplate
import freemarker.template.Configuration;
@Resource
private Configuration freemarkerConfig;
编写生成静态文件方法
@GetMapping(“/static/{gid}”)
public String doStatic(@PathVariable(“gid”) Long gid) throws IOException, TemplateException {
Template template = freemarkerConfig.getTemplate(“goods.ftl”);
Map param = new HashMap();
param.put(“goods”, goodsService.getGoods(gid));
param.put(“covers”, goodsService.findCovers(gid));
param.put(“details”, goodsService.findDetails(gid));
param.put(“params”, goodsService.findParams(gid));
File targetFile = new File(“d:/babytun/goods/” + gid + “.html”);
FileWriter out = new FileWriter(targetFile);
template.process(param, out);
out.close();
return targetFile.getPath();
}