MySQL, Oracle, Linux, 软件架构及大数据技术知识分享平台

网站首页 > 精选文章 / 正文

Spring Boot - Thymeleaf

2025-02-27 15:24 huorong 精选文章 1 ℃ 0 评论

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

Thymeleaf模板

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

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

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

Web 应用程序

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

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

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 中的静态目录下。在所示示例中,我们使用 CSS 文件来更改文本的颜色。

您可以使用以下代码并在单独的文件夹 css 中创建 CSS 文件,并将该文件命名为 styles.css

h4 {
   color: red;
}

index.html文件的代码如下



   
      
      
      Spring Boot Application
   
   
      

Welcome to Thymeleaf Spring Boot web application

项目资源管理器如下图所示:

现在,我们需要在构建配置文件中添加 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应用程序类文件的主代码如下:

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.tutorialspoint
   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.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  

现在,应用程序已在 Tomcat 端口 8080 上启动,现在点击Web浏览器中的URL,您可以看到如图所示的输出 :
http://localhost:8080/index

Tags:thymeleaf th:onclick

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言