- Spring Boot 教程
 - Spring Boot - 主页
 - Spring Boot - 简介
 - Spring Boot - 快速入门
 - Spring Boot - 引导
 - Spring Boot - Tomcat 部署
 - Spring Boot - 构建系统
 - Spring Boot - 代码结构
 - Spring Bean 和依赖注入
 - Spring Boot - 跑步者
 - Spring Boot - 应用程序属性
 - Spring Boot - 日志记录
 - 构建 RESTful Web 服务
 - Spring Boot - 异常处理
 - Spring Boot - 拦截器
 - Spring Boot - Servlet 过滤器
 - Spring Boot - Tomcat 端口号
 - Spring Boot - Rest 模板
 - Spring Boot - 文件处理
 - Spring Boot - 服务组件
 - Spring Boot - Thymeleaf
 - 使用 RESTful Web 服务
 - Spring Boot - CORS 支持
 - Spring Boot - 国际化
 - Spring Boot - 调度
 - Spring Boot - 启用 HTTPS
 - Spring Boot-Eureka 服务器
 - 向 Eureka 注册服务
 - Zuul代理服务器和路由
 - Spring Cloud配置服务器
 - Spring Cloud 配置客户端
 - Spring Boot - 执行器
 - Spring Boot - 管理服务器
 - Spring Boot - 管理客户端
 - Spring Boot - 启用 Swagger2
 - Spring Boot - 创建 Docker 镜像
 - 追踪微服务日志
 - Spring Boot - Flyway 数据库
 - Spring Boot - 发送电子邮件
 - Spring Boot-Hystrix
 - Spring Boot - Web Socket
 - Spring Boot - 批量服务
 - Spring Boot-Apache Kafka
 - Spring Boot - Twilio
 - Spring Boot - 单元测试用例
 - 休息控制器单元测试
 - Spring Boot - 数据库处理
 - 保护 Web 应用程序的安全
 - Spring Boot - 使用 JWT 的 OAuth2
 - Spring Boot - Google 云平台
 - Spring Boot - Google OAuth2 登录
 
- Spring Boot 资源
 - Spring Boot - 快速指南
 - Spring Boot - 有用的资源
 - Spring Boot - 讨论
 
Spring Boot - Thymeleaf
Thymeleaf 是一个基于 Java 的库,用于创建 Web 应用程序。它为 Web 应用程序中的 XHTML/HTML5 服务提供了良好的支持。在本章中,您将详细了解 Thymeleaf。
百里香模板
Thymeleaf 将您的文件转换为格式良好的 XML 文件。它包含 6 种类型的模板,如下所示 -
- XML
 - 有效的 XML
 - XHTML
 - 有效的 XHTML
 - HTML5
 - 旧版 HTML5
 
除旧版 HTML5 之外的所有模板均引用格式良好的有效 XML 文件。旧版 HTML5 允许我们在网页中呈现 HTML5 标签,包括非封闭标签。
Web应用程序
您可以使用 Thymeleaf 模板在 Spring Boot 中创建 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中创建一个 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')
}
您可以创建可执行 JAR 文件,并使用以下 Maven 或 Gradle 命令运行 spring boot 应用程序 -
对于 Maven,使用如下所示的命令 -
mvn clean install
“BUILD SUCCESS”后,您可以在目标目录下找到JAR文件。
对于 Gradle,使用如下所示的命令 -
gradle clean build
“BUILD SUCCESSFUL”后,您可以在build/libs目录下找到JAR文件。
使用此处给出的命令运行 JAR 文件 -
java –jar <JARFILE>
现在,应用程序已在 Tomcat 端口 8080 上启动,如下所示 -
现在在网络浏览器中点击 URL,您可以看到如图所示的输出 -
http://localhost:8080/index
