在pom.xml中加 spring-boot-starter-freemarker 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
这里写图片描述<build>
<finalName>spring-boot-starter-freemarker</finalName>
</build>
配置application.yml
spring:
freemarker:
request-context-attribute: request #开启request请求,可在页面根据 <#assign contextPath = request.contextPath/>获取项目路径
check-template-location: true
cache: false #关闭缓存,避免热部署,不需要每次修改页面都要重启
charset: utf-8
content-type: text/html
expose-request-attributes: true
expose-session-attributes: true
suffix: .ftl
template-loader-path: classpath:/templates/ #ftl文件的存放路径(resource下的templates)
一个简单的controller
@Controller
@RequestMapping("/test")
public class test{
@RequestMapping("/doScan")
public String doScan(Model model){
model.addAttribute("msg","Hello World~~~");
return "mytest";
}
}
mytest.ftl页面
<!DOCTYPE html>
<html lang="en">
<body>
${msg}
</body>
</html>
运行结果
![这里写图片描述](https://img-blog.csdn.net/20170809193837998?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2oxNjA3MTYyMjUz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)