一:在pom.xml中引入freemarker
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
关于freemarker的相关配置
######################################################## ###FREEMARKER (FreeMarkerAutoConfiguration) ######################################################## spring.freemarker.allow-request-override=false
#缓存配置开发时关闭缓存 spring.freemarker.cache=true spring.freemarker.check-template-location=true spring.freemarker.charset=UTF-8 spring.freemarker.content-type=text/html spring.freemarker.expose-request-attributes=false spring.freemarker.expose-session-attributes=false spring.freemarker.expose-spring-macro-helpers=false #spring.freemarker.prefix= #spring.freemarker.request-context-attribute= #spring.freemarker.settings.*= #spring.freemarker.suffix=.ftl #spring.freemarker.template-loader-path=classpath:/templates/ #comma-separated list #spring.freemarker.view-names= # whitelist of view names that can be resolved
代码:
controller:
package com.mt.controller; import java.util.Date; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/thymeleaf") public class ThymeleafController { @RequestMapping("/hello") public String hello(Map<String, Object> map) { map.put("date", new Date()); return "thymeleafDemo"; } @RequestMapping("/demo") public String demo(Map<String, Object> map) { map.put("demo", "freemarker demo "); return "freemarkerDemo"; } @RequestMapping("/rr") public String rr(Map<String, Object> map) { map.put("demo", "freemarker demo "); return "rr"; } }
ftl:
<!DOCTYPE html> <head> <title>Hello World!</title> </head> <body> <h1>Hello.v.2</h1> <p>${demo}</p> </body> </html>
一个走投无路的庄稼汉