SpringBoot 依赖之 Thymeleaf

Thymeleaf

本文主要介绍模板引擎的入门级使用,包括项目的创建、模板依赖包的引入以及简单demo的代码示例。熟悉MVC架构的程序员们应该都熟悉模板的概念,那这一期分享的就是View层的模板引擎。

适用人群:想要系统学习springboot的码友以及一些刚入门的小白。

Thymeleaf 官方依赖示意
  • 依赖名称: Thymeleaf

  • 功能描述:

  • 英文:A modern server-side Java template engine for both web and standalone environments. Allows HTML to be correctly displayed in browsers and as static prototypes.

  • 中文译文:适用于 Web 和独立环境的现代服务器端 Java 模板引擎。允许 HTML 在浏览器中正确显示并作为静态原型。

  • 依赖名称 示例代码:

  • <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    

那下面详细介绍下 IDEA下创建springboot项目,Thymeleaf依赖的使用,我的IDEA是2021版,学习者可以选择高的版本。

我们要在 IntelliJ IDEA 中创建一个 Spring Boot 项目并使用 Thymeleaf 进行视图渲染,可以按照以下步骤进行:

1. 创建 Spring Boot 项目,这里和前文spring-session相似,特殊定义项目名Artifact来区分。

  1. 打开 IntelliJ IDEA, 并选择 “New Project”

  2. 在左侧的菜单中选择 “Spring Initializr”

  3. 配置项目的基本信息:

    • Group: com.dependencies
    • Artifact: thymeleaf
    • Name: thymeleaf
    • Type: Maven
    • Language: Java
    • Packaging: Jar
    • Java Version: 17
  4. 点击 “Next”

  5. “Dependencies” 页面,添加以下依赖:

    • Spring Web - 用于创建 Web 应用程序。
    • Thymeleaf - 用于模板引擎。
  6. 点击 “Next”,然后点击 “Finish” 来创建项目。
    create springboot Thymeleaf project
    create springboot Thymeleaf project

2. 配置项目的依赖

IDEA 会自动生成一个 pom.xml 文件并添加我们选择的依赖。确保 pom.xml 包含以下依赖:

<dependencies>
  <!-- Spring Boot Starter Thymeleaf -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
  </dependency>
  <!-- Spring Boot Starter Web -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
</dependencies>

3. 创建 Thymeleaf 模板

src/main/resources/templates 目录下创建一个 Thymeleaf 模板文件。例如,创建 index.html 文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="description" content="v:一周一志程序员">
    <title>Thymeleaf模版引擎示例</title>
</head>
<body>

<h1 th:text="'Hello, ' + ${name} + '!'">Hello, 你好!</h1>
<h1 th:text="'Yeap! ' + ${message} + '!'">Hello, 你好!</h1>
</body>
</html>

4. 创建控制器

创建一个控制器类,用于处理请求并返回视图:这里有个注意点是控制器注解@Controller,不要选择@RestController,后者是RestAPI,直接返回数据。

package com.dependencies.thymeleaf.conroller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author zhizhou   2024/7/25 09:05
 */
@Controller
@RequestMapping("/")
public class IndexController {
    
    @GetMapping
    public String index(Model model){
        model.addAttribute("name", "Spring Boot 模板引擎 Thymeleaf");
        model.addAttribute("message", "调试成功");
        return "index";// 直接返回模板名称,不需要添加 .html 后缀
    }
}

5. 创建主应用程序类

创建主应用程序类并注解 @SpringBootApplication

package com.dependencies.thymeleaf;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ThymeleafApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(ThymeleafApplication.class, args);
    }
    
}

6. 运行项目

  1. 在 IntelliJ IDEA 中,找到主应用程序类 ThymeleafApplication
  2. 右键点击该类,并选择 “Run ‘ThymeleafApplication’” 来启动项目。

7. 访问应用

启动 Spring Boot 应用程序后,打开浏览器并访问 http://localhost:8080。我们应该会看到以下内容:

Hello, Spring Boot 模板引擎 Thymeleaf!
Yeap! 调试成功!

在这里插入图片描述

总结

通过以上步骤,我们在 IntelliJ IDEA 中创建了一个 Spring Boot 项目,添加了 Thymeleaf 依赖,并创建了一个简单的 Thymeleaf 模板和控制器来渲染视图。这个示例可以帮助你快速上手使用 Thymeleaf 进行视图渲染。
另外目前市面上的新软件大部分采用前后端分离的方式研发,所以模板引擎适用范围相对变小了,但是业务场景中 比如邮件模板还是需要的,下期我分享一下邮件模板依赖。

  • 44
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值