1、引依赖的故事
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
从依赖上可以看出,有thymeleaf的启动器starter,就一定会有默认配置。
自动配置了thymeleaf模板的位置classpath:/templates/,及后缀名.html。
比着葫芦画个飘,根据springboot依赖,找其他技术的默认配置属性,就是这个样子。
2、创建thymeleaf文件
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1 th:text="${'多喝热水:'+msg}">thymeleaf模板页</h1>
</body>
</html>
hello.html文件
可以看得出来,基本上就是html文件,并没有什么乱七八糟的新东西。
唯一的区别就是多了命名空间.
xmlns:th="http://www.thymeleaf.org"
3、后台代码
整体结构
application.yml文件内容
关闭缓存,因为Thymeleaf默认开启页面缓存,提高页面并发能力,但这样会导致我们修改页面不能立即展示效果,即热部署。
server:
port: 8089
spring:
thymeleaf:
cache: false
ThymeleafStarter启动类
package com.laoben.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ThymeleafStarter {
public static void main(String[] args) {
SpringApplication.run(ThymeleafStarter.class,args);
}
}
HelloController文件
package com.laoben.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
@GetMapping("hello")
public String hello(Model model){
model.addAttribute("msg", "Hello, Thymeleaf!");
return "hello";
}
}
4、测试
················································································
你学废了吗