1.在springboot项目的pom.xml 中加入freemarker的依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
2.在application.properties中配置模板
#设定ftl文件路径 spring.freemarker.template-loader-path=classpath:/templates #设定静态文件路径,js css 等 spring.mvc.static-path-pattern=/static/**
3.在Resources 下的templates文件夹下创建freemarker.ftl 文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>FreeMarker</title> </head> <body> <h1>${msg}</h1> </body> </html>
4.测试controller
import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class TestController { @RequestMapping("/test") public String testFreemarker(ModelMap modelMap){ modelMap.addAttribute("msg","Hello this is freemaker"); return "freemarker"; } }
5.在浏览器中访问