(本篇博客已于2019-08-28优化更新)
1、pom
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
2、application.yml
spring: freemarker: allow-request-override: false cache: true check-template-location: true charset: UTF-8 content-type: text/html expose-request-attributes: false expose-session-attributes: false expose-spring-macro-helpers: false suffix: .html profiles: active: dev
如果你使用的是ftl后缀,那么更改suffix为ftl,或者不用写,默认是ftl,如果你用html开发,那么suffix标注html
3、编写controller
@GetMapping(value = "/test2") public String test2(ModelMap modelMap){ modelMap.put("name","木九天"); return "/helloworld"; }
4、创建html
在resources/template下创建helloworld.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> ${name} </body> </html>
5、测试
6、疑惑解惑
5.1 :使用freemaker的时候,我们在controller返回的是一个路径,同时我们也使用了ModeMap,我们在ModeMap添加了数据,然后返回路径到具体html页面,${name} 就是我们ModeMap里面的数据,希望大家不要迷惑输出结果:木九天怎么来的! 5.2: 返回具体页面/找对应页面的时候,千万不能使用@RestController 和@ResponseBody,因为使用它们之后返回的是一个字符串而不是一个具体页面了,谨记。