Spring Boot - Thymeleaf

Spring Boot - Thymeleaf 首页 / Spring Boot入门教程 / Spring Boot - Thymeleaf

Thymeleaf是用于创建Web应用程序的基于Java的库。它为在Web应用程序中提供XHTML/ HTML5 提供了良好的支持。

Thymeleaf 模板

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

  • XML
  • Valid XML
  • XHTML
  • Valid XHTML
  • HTML5
  • Legacy HTML5

除旧版HTML5之外,所有模板均引用格式正确的有效XML文件。旧版HTML5允许在网页中呈现HTML5标签,包括未关闭的标签。

Web应用程序

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

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

package com.learnfk.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文件应放置在模板目录下,所有JS和CSS文件应放置在classpath中的静态目录下。在所示的示例中,无涯教程使用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>

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

Project Explorer Screenshot

现在需要在构建配置文件中添加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.learnfk.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.learnfk</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.learnfk'
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文件-

链接:https://www.learnfk.comhttps://www.learnfk.com/spring-boot/spring-boot-thymeleaf.html

来源:LearnFk无涯教程网

java jar <JARFILE> 

现在,应用程序已在Tomcat端口8080上启动,如下所示-

Started Application on Tomcat Port_8080

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

http:// localhost:8080/index

Spring Boot Thymleaf web Application

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

程序员进阶攻略 -〔胡峰〕

深入浅出计算机组成原理 -〔徐文浩〕

透视HTTP协议 -〔罗剑锋(Chrono)〕

如何看懂一幅画 -〔罗桂霞〕

PyTorch深度学习实战 -〔方远〕

反爬虫兵法演绎20讲 -〔DS Hunter〕

说透低代码 -〔陈旭〕

深入浅出可观测性 -〔翁一磊〕

结构会议力 -〔李忠秋〕

好记忆不如烂笔头。留下您的足迹吧 :)