【注】本文译自:https://www.tutorialspoint.com/spring_boot/spring_boot_thymeleaf.htmcss
Thymeleaf 是一个基于 Java 的库【译注:模板引擎】,可用于建立 web 应用。它对于 web 应用中的 XHTML/HTML5 提供了良好的支持。在本文中,你将学会有关 Thymeleaf 细节。html
Thymeleaf 将你的文件转换成格式良好的 XML 文件。它包含如下 6 种类型的模板:java
除了遗留 HTML5 之外的全部模板均可参阅有效的 XML 文件。遗留 HTML5 容许咱们在 web 页中渲染 HTML5 标签,包括没闭合的标签。web
你能够使用 Thymeleaf 模板在 Spring Boot 中建立 web 应用,须要如下几个步骤。spring
使用如下代码建立一个 @Controller 类文件以重定向请求 URI 到 HTML 文件:apache
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:bash
h4 { color: red; }
index.html 文件的代码以下:app
<!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>
项目浏览器的截屏以下所示:eclipse
如今,咱们须要在咱们的构建配置文件中加上 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](http://maven.apache.org/POM/4.0.0)" xmlns:xsi = "[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)" xsi:schemaLocation = "[http://maven.apache.org/POM/4.0.0](http://maven.apache.org/POM/4.0.0) [http://maven.apache.org/xsd/maven-4.0.0.xsd](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,你将会看到下图所示的输出: