大纲
(1)在pom.xml中引入freemarker;
(2)如何关闭freemarker缓存
(3)编写模板文件.ftl
(4)编写访问文件的controller
在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
编写模板文件.ftl
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello.v.2</h1>
<p>${hello}</p>
</body>
</html>
编写访问文件的controller
@RequestMapping("/helloFtl")
public String helloFtl(Map<String,Object> map){
map.put("hello","from TemplateController.helloFtl");
return "/helloFtl";
}
代码如下:
Controller类
package com.kfit; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * 注意: * 1.在Thymeleaf 模板文件中,标签是需要闭合的,3.0之前是需要闭合的 * 2. thymeleaf 3.0+ 是可以不强制要求闭合的。 * * 3. 支持同时使用多个模板引擎,比如:thymeleaf和freemarker 可以并存。 * */ @Controller @RequestMapping("/templates") public class TemplatesController { /** * 映射地址是:/templates/hello * @return */ @RequestMapping("/hello") public String hello(Map<String,Object> map){ //返回的是ModelAndView对象; // ModelAndView mv = new ModelAndView("hello"); // return mv; map.put("name","Andy"); return "hello"; } @RequestMapping("/helloFtl") public String helloFtl(Map<String,Object> map){ map.put("name","Andy"); return "helloFtl"; } }
模板
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
hello ${name}
</body>
</html>
配置
######################################################## ###FREEMARKER (FreeMarkerAutoConfiguration) ######################################################## spring.freemarker.allow-request-override=false spring.freemarker.cache=false 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