SpringBoot2.0学习之整合freemarker模板引擎
1. 在pom.xml中引入相关依赖
<!--freemarker模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2. application.yml 中添加相关配置
Ps:没有特别的需要可以不添加配置信息,默认值一样,如果想改可以在这里另做配置。
### freemarker配置
spring:
freemarker:
# 设置模板后缀名
suffix: .ftl
# 设置页面类型
content-type: text/html
# 设置页面编码格式
charset: UTF-8
# 设置页面是否开启缓存
cache: false
# 设置文件存放路径
template-loader-path: classpath:/templates
3. 测试一下:
目录结构:
index.ftl
<!DOCTYPE>
<html>
<head>
<title>test-freemarker</title>
</head>
<body>
<h1>hello freemarker! ${name}</h1>
</body>
</html>
controller
@Controller
public class IndexController {
@RequestMapping("/index")
public String index(Model model){
model.addAttribute("name", "张三");
return "index";
}
}
访问http://localhost:8080/index测试结果:
希望你可以花更多时间做自己!