- 文件目录结构

- maven 配置
应用 使用spring boot构建
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
- 配置 文件
#设定ftl文件路径
spring.freemarker.template-loader-path=classpath:/templates
#设定静态文件路径,js,css等
spring.mvc.static-path-pattern=/static/**
server.servlet.context-path=/freemarker
- controller
@Controller
public class IndexController {
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String init(ModelMap model) {
model.addAttribute("message", "hello freemarker");
return "index";
}
}
- freemarker文件
代码注释中说明了怎么include js文件,以及遇到的问题
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
<meta charset="utf-8"/>
<title>FreeMarker</title>
</head>
<#-- html 引入-->
<script src="static/test.js"></script>
<#-- include 引入 : 也不知道什么原因要放到sub目录下才可以引入,和 index.ftl同级目录就会报错-->
<script>
<#include "sub/hello.js">
</script>
<body>
<h1>${message}</h1>
<h1>
<#include "sub/include.ftl">
</h1>
<h1>
<#include "sub/include.html">
</h1>
</body>
</html>
- 效果图
