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

? ? Thymeleaf 是一個(gè)基于 Java 的庫【譯注:模板引擎】,可用于創(chuàng)建 web 應(yīng)用。它對于 web 應(yīng)用中的 ?XHTML/HTML5 提供了良好的支持。在本文中,你將學(xué)會有關(guān) Thymeleaf 細(xì)節(jié)。
Thymeleaf 模板
? ?Thymeleaf 將你的文件轉(zhuǎn)換成格式良好的 XML 文件。它包含以下 6 種類型的模板:
XML
有效 XML
XHTML
有效 XHTML
HTML5
遺留 HTML5
? ?除了遺留?HTML5 以外的所有模板都可參閱有效的?XML 文件。遺留 HTML5 允許我們在 web 頁中渲染 HTML5 標(biāo)簽,包括沒閉合的標(biāo)簽。
Web 應(yīng)用
? ?你可以使用?Thymeleaf 模板在?Spring Boot 中創(chuàng)建 web 應(yīng)用,需要以下幾個(gè)步驟。
? ?使用以下代碼創(chuàng)建一個(gè)?@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 文件應(yīng)當(dāng)放到 templates 目錄下面,所有的 JS 和 CSS 文件應(yīng)當(dāng)放到?classpath?路徑下的 static 目錄。在示例中,我們使用 CSS 文件改變文本顏。
? ?你可以使用以下代碼在一個(gè)獨(dú)立的目錄中創(chuàng)建?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>
? ?項(xiàng)目瀏覽器的截屏如下所示:

? ?現(xiàn)在,我們需要在我們的構(gòu)建配置文件中加上?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 應(yīng)用類代碼如下所示:
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')
}
? ?現(xiàn)在你可以使用 Maven 或 Gradle 命令創(chuàng)建可執(zhí)行?executable JAR 文件并運(yùn)行 Spring Boot 應(yīng)用了:
? ?Maven 命令如下:
mvn clean install
? ?在 “BUILD SUCCESS” 之后,你可以在 target 目錄下找到 JAR 文件。
? ?Gradle 可以使用以下命令:
gradle clean build
? ?在 “BUILD SUCCESSFUL” 之后,你可以在?build/libs 目錄下找到?JAR 文件。
? ?使用以下命令運(yùn)行 JAR 文件:
java –jar <JARFILE>
? ?現(xiàn)在應(yīng)用已在?Tomcat 8080 端口啟動,如下圖所示:

? ?在瀏覽器中輸入以下?URL,你將會看到下圖所示的輸出:
http://localhost:8080/index
