Spring Boot 3 集成 Thymeleaf

简介: Thymeleaf是一款用于Web和独立环境的现代化服务器端Java模板引擎。它能够处理HTML、XML、JavaScript、CSS甚至纯文本。Thymeleaf的语法简单易懂,它允许开发者在模板中嵌入表达式,以便动态地渲染数据。

在现代的Web开发中,构建灵活、动态的用户界面是至关重要的。Spring Boot和Thymeleaf的结合为开发者提供了一种简单而强大的方式来创建动态的Web应用。本文将介绍如何在Spring Boot项目中集成Thymeleaf,并展示一些基本的使用方法。

themeleaf.jpg

什么是Thymeleaf?

Thymeleaf是一款用于Web和独立环境的现代化服务器端Java模板引擎。它能够处理HTML、XML、JavaScript、CSS甚至纯文本。Thymeleaf的语法简单易懂,它允许开发者在模板中嵌入表达式,以便动态地渲染数据。

官网地址:https://www.thymeleaf.org/

官方文档:https://www.thymeleaf.org/doc/tutorials/3.1/usingthymeleaf.html

github地址:https://github.com/thymeleaf

开始集成Thymeleaf

首先,确保你的Spring Boot项目已经建立。接下来,我们将添加Thymeleaf的依赖。在pom.xml文件中,添加以下依赖:

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

这将引入Spring Boot Thymeleaf Starter,它包含了Thymeleaf的所有必要依赖。

配置Thymeleaf

在Spring Boot应用中,Thymeleaf的默认配置通常已经足够满足大多数需求。然而,你也可以通过在application.properties或application.yml文件中进行配置来修改默认设置。以下是一个基本的Thymeleaf配置示例:

spring:
  # 配置thymeleaf的相关信息
  thymeleaf:
    # 开启视图解析
    enabled: true
    #编码格式
    encoding: UTF-8
    #前缀配置
    prefix: classpath:/templates/
    # 后缀配置
    suffix: .html
    #是否使用缓存 开发环境时不设置缓存
    cache: false
    # 格式为 HTML 格式
    mode: HTML5
    # 配置类型
    servlet:
      content-type: text/html

创建Thymeleaf模板

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

<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>修己</title>
</head>
<body>
<span th:text="${name}" style="font-size: 60px;font-weight: bold;color: #00C957;font-family: 楷体,cursive">xj</span>
</body>
</html>

修改 html 标签用于引入 thymeleaf 引擎,这样才可以在其他标签里使用 th:* 语法,声明如下:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

创建Controller

现在,我们需要一个Controller来处理请求并提供数据给Thymeleaf模板。创建一个简单的Controller类:

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


@Controller
public class TestController {
   
   
    @GetMapping({
   
   "/","/index"})
    public String  listUser(Model model){
   
   
        model.addAttribute("name","修己");
        return "index";
    }
}

注:

我们前后端分离的项目在controller上一般都会使用@RestController注解,@RestController 注解是 @Controller 和 @ResponseBody 注解的结合,它的作用相当于在每个方法上都添加了 @ResponseBody,用于构建 RESTful Web服务。使用 @RestController 注解的类,每个方法的返回值都会被直接写入HTTP响应体中,而不会经过视图解析器进行渲染。默认情况下,返回的是JSON格式的数据,但可以通过其他注解配置以返回不同格式的数据。

@Controller 通常用于传统的MVC应用程序,其中控制器负责处理HTTP请求,并返回一个视图(HTML页面)或者通过视图解析器解析的模型数据。Thymeleaf通常与@Controller一起使用,因为Thymeleaf模板引擎负责渲染HTML视图。

运行应用

现在你可以运行你的Spring Boot应用程序。访问http://localhost:8000 或者 http://localhost:8000/index,你应该能够看到渲染后的页面,上面显示着动态设置的name。

b2c3450a2ac4a54cd8c29f97bd22cf2.png

语法

Thymeleaf 是一款现代化的服务器端Java模板引擎,专为Web应用开发而设计。其语法清晰、易读,广泛支持HTML、XML、JavaScript等多种模板类型。尽管我之前对Thymeleaf的页面开发经验有限,但最近在网络上发现了一篇介绍Thymeleaf基本语法和特性的博客。我觉得这篇博客内容非常有价值,于是决定将其分享给家人,以便更多人能够受益。如果你也对Thymeleaf感兴趣,可以在这里查看博客内容:https://fanlychie.github.io/post/thymeleaf.html

总结

通过集成Thymeleaf,我们能够在Spring Boot应用中创建动态且灵活的用户界面。Thymeleaf的简单语法和与Spring Boot的无缝集成使得开发者能够轻松构建功能丰富的Web应用。

目录
相关文章
|
移动开发 前端开发 JavaScript
SpringBoot3 整合Thymeleaf 模板引擎
Thymeleaf 是一个基于 Java 的现代模板引擎,支持 HTML 原型,文件后缀为 .html,可直接在浏览器中查看静态效果。它与 Spring Boot 完美整合,默认配置即可使用,无需额外视图解析器设置。Thymeleaf 支持多种表达式(如变量、链接、国际化等)和 th 属性(如 th:text、th:if 等),适用于 Web 和非 Web 应用开发。通过 th:fragment、th:insert、th:replace 和 th:include 等属性,可以抽取和复用公共页面片段,并支持参数传递。
1956 12
|
Java 应用服务中间件 nginx
SpringBoot入门(九)
SpringBoot入门(九)
|
XML 缓存 前端开发
SpringBoot集成Thymeleaf从入门到精通(全)
目录SpringBoot集成Thymeleaf1. 关闭缓存2. 表达式3. 常用属性4. 遍历元素5. 条件判断6. 字面量7. 字符串拼接8. 数学运算 SpringBoot集成Thymeleaf Thymeleaf 是一个流行的模板引擎,该模板引擎采用 Java 语言开发 Thymeleaf 是另外的一种模板技术,它本身并不属于 Spring Boot,Spring Boot只是很好地集成这种模板技术,作为前端页面的数据展示 在创建模板的时候还需要多选择一个这个 之后默认会自动添加这些依赖 Sp
454 0
SpringBoot集成Thymeleaf从入门到精通(全)
|
JSON 前端开发 Java
微服务——SpringBoot使用归纳——Spring Boot集成Thymeleaf模板引擎——Thymeleaf 的使用
本文介绍了 Thymeleaf 在 Spring Boot 项目中的使用方法,包括访问静态页面、处理对象和 List 数据、常用标签操作等内容。通过示例代码展示了如何配置 404 和 500 错误页面,以及如何在模板中渲染对象属性和列表数据。同时总结了常用的 Thymeleaf 标签,如 `th:value`、`th:if`、`th:each` 等,并提供了官方文档链接以供进一步学习。
869 0
微服务——SpringBoot使用归纳——Spring Boot集成Thymeleaf模板引擎——Thymeleaf 的使用
|
前端开发 JavaScript Java
Springboot整合Thymeleaf并详解热部署
Springboot整合Thymeleaf并详解热部署
1066 0
|
Java 网络架构
springboot配合thymeleaf,调用接口不跳转页面只显示文本
springboot配合thymeleaf,调用接口不跳转页面只显示文本
725 0
|
9月前
|
缓存 JSON 前端开发
第07课:Spring Boot集成Thymeleaf模板引擎
第07课:Spring Boot集成Thymeleaf模板引擎
798 0
第07课:Spring Boot集成Thymeleaf模板引擎
|
安全 Java 数据库
Spring Security 权限管理详解与案例
Spring Security 是 Spring 框架中用于提供认证和访问控制的模块。它保护了成千上万的应用程序,使其免受未经授权的访问。本文将详细介绍 Spring Security 的权限管理功能,并通过一个实际案例来展示其用法。
1788 1
|
7月前
|
存储 消息中间件 NoSQL
【Redis】常用数据结构之List篇:从常用命令到典型使用场景
本文将系统探讨 Redis List 的核心特性、完整命令体系、底层存储实现以及典型实践场景,为读者构建从理论到应用的完整认知框架,助力开发者在实际业务中高效运用这一数据结构解决问题。
|
安全 前端开发 Java
Spring Authorization Server 1.1 扩展实现 OAuth2 密码模式与 Spring Cloud 的整合实战(下)
Spring Authorization Server 1.1 扩展实现 OAuth2 密码模式与 Spring Cloud 的整合实战(下)