Spring Boot 教程:Thymeleaf

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 文件:
1 package com.tutorialspoint.demo.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 6 @Controller 7 public class WebController { 8 @RequestMapping(value = "/index") 9 public String index() { 10 return "index"; 11 } 12 }
在上述例子中,请求 URI 为 /index,控制重定向到 index.html 文件。注意,index.html 文件应当放到 templates 目录下面,所有的 JS 和 CSS 文件应当放到 classpath 路径下的 static 目录。在示例中,我们使用 CSS 文件改变文本颜色
你可以使用以下代码在一个独立的目录中创建 CSS 文件,文件名为 styles.css:
1 h4 { 2 color: red; 3 }
index.html 文件的代码如下:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset = "ISO-8859-1" /> 5 <link href = "css/styles.css" rel = "stylesheet"/> 6 <title>Spring Boot Application</title> 7 </head> 8 <body> 9 <h4>Welcome to Thymeleaf Spring Boot web application</h4> 10 </body> 11 </html>
项目浏览器的截屏如下所示:

现在,我们需要在我们的构建配置文件中加上 Spring Boot Starter Thymeleaf 依赖。
Maven 用户可以将下面的依赖加入 pom.xml 文件:
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-thymeleaf</artifactId> 4 </dependency>
Gradle 用户可以在 build.gradle 文件中加入以下依赖:
compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf'
主 Spring Boot 应用类代码如下所示:
1 package com.tutorialspoint.demo; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 @SpringBootApplication 7 public class DemoApplication { 8 public static void main(String[] args) { 9 SpringApplication.run(DemoApplication.class, args); 10 } 11 }
Maven – pom.xml 代码如下:
1 <?xml version = "1.0" encoding = "UTF-8"?> 2 <project xmlns = "http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 5 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 6 7 <modelVersion>4.0.0</modelVersion> 8 <groupId>com.tutorialspoint</groupId> 9 <artifactId>demo</artifactId> 10 <version>0.0.1-SNAPSHOT</version> 11 <packaging>jar</packaging> 12 <name>demo</name> 13 <description>Demo project for Spring Boot</description> 14 15 <parent> 16 <groupId>org.springframework.boot</groupId> 17 <artifactId>spring-boot-starter-parent</artifactId> 18 <version>1.5.8.RELEASE</version> 19 <relativePath /> 20 </parent> 21 22 <properties> 23 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 24 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 25 <java.version>1.8</java.version> 26 </properties> 27 28 <dependencies> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter-web</artifactId> 32 </dependency> 33 34 <dependency> 35 <groupId>org.springframework.boot</groupId> 36 <artifactId>spring-boot-starter-test</artifactId> 37 <scope>test</scope> 38 </dependency> 39 40 <dependency> 41 <groupId>org.springframework.boot</groupId> 42 <artifactId>spring-boot-starter-thymeleaf</artifactId> 43 </dependency> 44 </dependencies> 45 46 <build> 47 <plugins> 48 <plugin> 49 <groupId>org.springframework.boot</groupId> 50 <artifactId>spring-boot-maven-plugin</artifactId> 51 </plugin> 52 </plugins> 53 </build> 54 55 </project>
Gradle – build.gradle 代码如下:
1 buildscript { 2 ext { 3 springBootVersion = '1.5.8.RELEASE' 4 } 5 repositories { 6 mavenCentral() 7 } 8 dependencies { 9 classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 10 } 11 } 12 13 apply plugin: 'java' 14 apply plugin: 'eclipse' 15 apply plugin: 'org.springframework.boot' 16 17 group = 'com.tutorialspoint' 18 version = '0.0.1-SNAPSHOT' 19 sourceCompatibility = 1.8 20 21 repositories { 22 mavenCentral() 23 } 24 dependencies { 25 compile('org.springframework.boot:spring-boot-starter-web') 26 compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf' 27 testCompile('org.springframework.boot:spring-boot-starter-test') 28 }
现在你可以使用 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,你将会看到下图所示的输出:


浙公网安备 33010602011771号