Spring Boot 教程:Thymeleaf
【注】本文译自:https://www.tutorialspoint.com/spring_boot/spring_boot_thymeleaf.htm

Thymeleaf 是一个基于 Java 的库【译注:模板引擎】,可用于创建 web 应用。它对于 web 应用中的 XHTML/HTML5 提供了良好的支持。在本文中,你将学会有关 Thymeleaf 细节。
Thymeleaf 模板
Thymeleaf 将你的文件转换成格式良好的 XML 文件。它包含以下 6 种类型的模板:
XML
有效 XML
XHTML
有效 XHTML
HTML5
遗留 HTML5
除了遗留 HTML5 以外的所有模板都可参阅有效的 XML 文件。遗留 HTML5 允许我们在 web 页中渲染 HTML5 标签,包括没闭合的标签。
Web 应用
你可以使用 Thymeleaf 模板在 Spring Boot 中创建 web 应用,需要以下几个步骤。
使用以下代码创建一个 @Controller 类文件以重定向请求 URI 到 HTML 文件:
package com.tutorialspoint.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class WebController {
@RequestMapping(value = "/index")
public String index() {
return "index";
}
}
在上述例子中,请求 URI 为 /index,控制重定向到 index.html 文件。注意,index.html 文件应当放到 templates 目录下面,所有的 JS 和 CSS 文件应当放到 classpath 路径下的 static 目录。在示例中,我们使用 CSS 文件改变文本颜。
你可以使用以下代码在一个独立的目录中创建 CSS 文件,文件名为 styles.css:
h4 {
color: red;
}
index.html 文件的代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset = "ISO-8859-1" />
<link href = "css/styles.css" rel = "stylesheet"/>
<title>Spring Boot Application</title>
</head>
<body>
<h4>Welcome to Thymeleaf Spring Boot web application</h4>
</body>
</html>
项目浏览器的截屏如下所示:

现在,我们需要在我们的构建配置文件中加上 Spring Boot Starter Thymeleaf 依赖。
Maven 用户可以将下面的依赖加入 pom.xml 文件:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Gradle 用户可以在 build.gradle 文件中加入以下依赖:
compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf'
主 Spring Boot 应用类代码如下所示:
package com.tutorialspoint.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Maven – pom.xml 代码如下:
<?xml version = "1.0" encoding = "UTF-8"?>
<project xmlns = "http://maven.apache.org/POM/4.0.0"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tutorialspoint</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Gradle – build.gradle 代码如下:
buildscript {
ext {
springBootVersion = '1.5.8.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group = 'com.tutorialspoint'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf'
testCompile('org.springframework.boot:spring-boot-starter-test')
}
现在你可以使用 Maven 或 Gradle 命令创建可执行 executable JAR 文件并运行 Spring Boot 应用了:
Maven 命令如下:
mvn clean install
在 “BUILD SUCCESS” 之后,你可以在 target 目录下找到 JAR 文件。
Gradle 可以使用以下命令:
gradle clean build
在 “BUILD SUCCESSFUL” 之后,你可以在 build/libs 目录下找到 JAR 文件。
使用以下命令运行 JAR 文件:
java –jar <JARFILE>
现在应用已在 Tomcat 8080 端口启动,如下图所示:

在浏览器中输入以下 URL,你将会看到下图所示的输出:
http://localhost:8080/index
