1.pom中添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
2.application.yml中添加配置
freemarker: cache: false template-loader-path: classpath:/templates charset: UTF-8 check-template-location: true content-type: text/html expose-request-attributes: true expose-session-attributes: true request-context-attribute: request suffix: .html
3.编写控制器方法
@RequestMapping("/index") public String index(ModelMap map){ map.addAttribute("name","hello"); return "index"; }
4.添加模板文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> ${name} </body> </html>
整体项目结构如图所示