Spring Boot Thymeleaf,Thymeleaf 是一个基于 Java 的库,用于创建 Web 应用程序。它为在 Web 应用程序中提供 XHTML/HTML5 服务提供了良好的支持。在本章中,您将详细了解 Thymeleaf。

Thymeleaf 模板

Thymeleaf 将您的文件转换为格式良好的 XML 文件。它包含 6 种类型的模板,如下所示 –

  • XML
  • 有效的 XML
  • XHTML
  • 有效的 XHTML
  • HTML5
  • 旧版 HTML5

除了 Legacy HTML5 之外,所有模板都指的是格式正确的有效 XML 文件。旧版 HTML5 允许我们在网页中呈现 HTML5 标签,包括非闭合标签。

Web应用程序

您可以使用 Thymeleaf 模板在 Spring Boot 中创建 Web 应用程序。您必须按照以下步骤使用 Thymeleaf 在 Spring Boot 中创建 Web 应用程序。

使用以下代码创建一个 @Controller 类文件以将请求 URI 重定向到 HTML 文件 –

package com.zuze.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 文件的代码如下 –



   
      
      
      Spring Boot Application
   
   
      

Welcome to Thymeleaf Spring Boot web application

项目资源管理器显示在下面给出的屏幕截图中 –

project_explorer_screenshot

现在,我们需要在构建配置文件中添加 Spring Boot Starter Thymeleaf 依赖项。

Maven 用户可以将以下依赖项添加到 pom.xml 文件中 –


   org.springframework.boot
   spring-boot-starter-thymeleaf

Gradle 用户可以在 build.gradle 文件中添加以下依赖项 –

compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf'

主要 Spring Boot 应用程序类文件的代码如下 –

package com.zuze.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 如下 –



   
   4.0.0
   com.zuze
   demo
   0.0.1-SNAPSHOT
   jar
   demo
   Demo project for Spring Boot

   
      org.springframework.boot
      spring-boot-starter-parent
      1.5.8.RELEASE
      
   

   
      UTF-8
      UTF-8
      1.8
   

   
      
         org.springframework.boot
         spring-boot-starter-web
      

      
         org.springframework.boot
         spring-boot-starter-test
         test
      

      
         org.springframework.boot
         spring-boot-starter-thymeleaf
      
   

   
      
         
            org.springframework.boot
            spring-boot-maven-plugin
         
      
   
   

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.zuze'
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  

现在,应用程序已在 Tomcat 端口 8080 上启动

现在在您的网络浏览器中点击 URL,您可以看到如下所示的输出 –

http://localhost:8080/index

Spring Boot Thymeleaf

Spring Boot Thymeleaf推荐

Flutter和React Native比较

在线看片