1.添加依赖
<!--freemarker依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
2.添加配置文件
spring.freemarker.allow-request-override=false spring.freemarker.check-template-location=true spring.freemarker.charset=utf-8 spring.freemarker.content-type=text/html spring.freemarker.cache=true spring.freemarker.expose-request-attributes=false spring.freemarker.expose-session-attributes=false spring.freemarker.expose-spring-macro-helpers=false #spring.freemarker.prefix= spring.freemarker.suffix=.ftl #spring.freemarker.request-context-attribute= #spring.freemarker.settings.*= #spring.freemarker.template-loader-path=classpath:/template/ #comma-separated list #spring.freemarker.view-names=#whitelist of view names that can be resolved
3.添加页面helloFtl.ftl
<!DOCTYPE html> <head> <meta charset="UTF-8"> <title>Hello World!</title> </head> <body> Hello Freemarker ${name} </body> </html>
4.添加controller
package com.huawei.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import java.util.Map; /** * 注意: * 1.在Thymeleaf模板文件中,标签是需要闭合的,3.0之前是需要闭合的 * 2.thymeleaf3.0+是可以不强制要求闭合的。 * * 3.同时支持使用多个模板引擎,比如thymeleaf和freemarker */ @Controller @RequestMapping("/templates") public class TemplatesController { @RequestMapping("/test") public String hello(Map<String, Object> map) { map.put("hello", "hello"); return "hello"; } @RequestMapping("/helloFtl") public String helloFtl(Map<String, Object> map) { map.put("name", "wwww"); return "helloFtl"; } } 运行结果: