基本项目结构:

Thymeleaf配置:
spring.thymeleaf.mode=LEGACYHTML5 spring.thymeleaf.cache=false spring.thymeleaf.prefix=classpath:/static/ spring.thymeleaf.suffix=.html
spring默认的模板映射路径是:src/main/resources/templates
ftl路径的hello.html:
<!DOCTYPE html>
<html lang="zh-CN"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>helloWorld</title>
</head>
<body>
<h1>Hello : <b th:text="${name}">姓名</b></h1>
</body>
</html>
TestController:
package com.eshore.ismp.web;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class TestController {
@RequestMapping(value = "test", method = RequestMethod.GET)
public String test(Model model) {
model.addAttribute("name", "MERCY");
return "/ftl/hello";
}
}
运行结果:

要注意的点:
spring.thymeleaf.cache要设置为true,以方便开发及时看到页面刷新
springboot直接引入:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
非springboot项目引入:
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>2.1.4</version>
</dependency>
LEGACYHTML5属性值需要搭配一个额外的库NekoHTML才可用。主要作用是设置Thymeleaf格式检查没这么严格,默认属性值是HTML5
















