Spring Boot 简明教程
Spring Boot - Thymeleaf
Thymeleaf 是一个基于 Java 的库,用于创建 Web 应用程序。它为在 Web 应用程序中使用 XHTML/HTML5 提供了良好的支持。在本章中,你将详细了解 Thymeleaf。
Thymeleaf is a Java-based library used to create a web application. It provides a good support for serving a XHTML/HTML5 in web applications. In this chapter, you will learn in detail about Thymeleaf.
Thymeleaf Templates
Thymeleaf 将你的文件转换为格式良好的 XML 文件。它包含 6 种类型的模板,如下所示 −
Thymeleaf converts your files into well-formed XML files. It contains 6 types of templates as given below −
-
XML
-
Valid XML
-
XHTML
-
Valid XHTML
-
HTML5
-
Legacy HTML5
除了传统 HTML5 之外,所有模板都指的是格式良好的有效的 XML 文件。传统 HTML5 允许我们在网页中呈现 HTML5 标签,包括未闭合的标签。
All templates, except Legacy HTML5, are referring to well-formed valid XML files. Legacy HTML5 allows us to render the HTML5 tags in web page including not closed tags.
Web Application
你可以使用 Thymeleaf 模板在 Spring Boot 中创建一个 Web 应用程序。你需要按照以下步骤使用 Thymeleaf 在 Spring Boot 中创建一个 Web 应用程序。
You can use Thymeleaf templates to create a web application in Spring Boot. You will have to follow the below steps to create a web application in Spring Boot by using Thymeleaf.
使用以下代码创建一个 @Controller 类文件,以便将请求 URI 重定向到 HTML 文件 −
Use the following code to create a @Controller class file to redirect the Request URI to HTML file −
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 文件应放置在类路径中的 static 目录下。在所示示例中,我们使用 CSS 文件来更改文本颜色。
In the above example, the request URI is /index, and the control is redirected into the index.html file. Note that the index.html file should be placed under the templates directory and all JS and CSS files should be placed under the static directory in classpath. In the example shown, we used CSS file to change the color of the text.
你可以使用以下代码并在单独的文件夹 css 中创建一个 CSS 文件,并将文件命名为 styles.css −
You can use the following code and created a CSS file in separate folder css and name the file as styles.css −
h4 {
color: red;
}
index.html 文件的代码如下 -
The code for index.html file is given below −
<!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>
项目资源管理器显示在下方的截图中 −
The project explorer is shown in the screenshot given below −

现在,我们需要在我们的构建配置文件中添加 Spring Boot Starter Thymeleaf 依赖。
Now, we need to add the Spring Boot Starter Thymeleaf dependency in our build configuration file.
Maven 用户可以在 pom.xml 文件中添加下列依赖 -
Maven users can add the following dependency into the pom.xml file −
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Gradle 用户可以在 build.gradle 文件中添加下列依赖 -
Gradle users can add the following dependency in the build.gradle file −
compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf'
Spring Boot 主应用程序类文件的代码如下 -
The code for main Spring Boot application class file is given below −
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 的代码如下 -
The code for Maven – pom.xml is given below −
<?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 的代码如下 -
The code for Gradle – build.gradle is given below −
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')
}
您可以创建一个可执行 JAR 文件,并通过使用以下 Maven 或 Gradle 命令运行 Spring Boot 应用程序 -
You can create an executable JAR file, and run the spring boot application by using the following Maven or Gradle commands −
对于 Maven,使用如下所示的命令:
For Maven, use the command as shown below −
mvn clean install
“BUILD SUCCESS”之后,您可以在目标目录中找到 JAR 文件。
After “BUILD SUCCESS”, you can find the JAR file under the target directory.
对于 Gradle,像下面这样使用命令 -
For Gradle, use the command as shown below −
gradle clean build
“BUILD SUCCESSFUL”之后,您可以在 build/libs 目录中找到 JAR 文件。
After “BUILD SUCCESSFUL”, you can find the JAR file under the build/libs directory.
使用此处提供的命令运行 JAR 文件 −
Run the JAR file by using the command given here −
java –jar <JARFILE>
现在,应用程序在 Tomcat 端口 8080 上启动,如下所示 -
Now, the application has started on the Tomcat port 8080 as shown below −

现在在您的浏览器中输入 URL,您可以看到输出如下所示 −
Now hit the URL in your web browser and you can see the output as shown −
