springboot 中 thymeleaf 常用的语法完整实例

 更新时间:2024年03月28日 11:59:46   作者:人生万事须自为,跬步江山即寥廓。  
在 Spring Boot 项目中,Thymeleaf 是一个常用的模板引擎,它提供了丰富的语法来动态渲染 HTML 页面,下面给大家介绍springboot 中 thymeleaf 常用的语法完整实例,感兴趣的朋友一起看看吧

在 Spring Boot 项目中,Thymeleaf 是一个常用的模板引擎,它提供了丰富的语法来动态渲染 HTML 页面。以下是一些常用的 Thymeleaf 语法,以及一个完整的 Spring Boot + Thymeleaf 示例。

Thymeleaf 常用语法

表达式

<!-- 获取变量值 -->
<p th:text="${message}">默认消息</p>
<!-- 获取对象的属性 -->
<p th:text="${user.name}">默认用户名</p>

选择

<!-- 判断 -->
<p th:if="${user.active}">用户激活</p>
<!-- 选择 -->
<div th:switch="${user.role}">
    <p th:case="'admin'">管理员</p>
    <p th:case="'user'">普通用户</p>
</div>

链接

<!-- 跳转到指定 URL -->
<a th:href="@{/profile}" rel="external nofollow" >查看资料</a>
<!-- 带参数的 URL -->
<a th:href="@{/profile(id=${user.id})}" rel="external nofollow"  rel="external nofollow" >查看资料</a>

属性

<!-- 替换属性 -->
<div th:attr="id=${user.id}, class=${user.active ? 'active' : 'inactive'}">用户信息</div>

片段

<!-- 引入片段 -->
<div th:insert="~{commons :: header}">页面头部</div>
<!-- 替换片段 -->
<div th:replace="~{commons :: header}">页面头部</div>

内容

<!-- 插入内容 -->
<div th:insert="~{commons :: content">
    <!-- 页面内容 -->
</div>
<!-- 替换内容 -->
<div th:replace="~{commons :: content">
    <!-- 页面内容 -->
</div>

完整代码

以下是一个完整的 Spring Boot + Thymeleaf 示例,包括一个简单的控制器和一个 Thymeleaf 模板文件。

pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <!-- 其他依赖 -->
</dependencies>

application.properties

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.cache=false

ThymeleafController.java

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class ThymeleafController {
    @GetMapping("/")
    public String index(Model model) {
        model.addAttribute("message", "欢迎来到 Spring Boot + Thymeleaf 示例!");
        return "index";
    }
}

index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Spring Boot + Thymeleaf 示例</title>
</head>
<body>
    <h1 th:text="${message}">默认消息</h1>
</body>
</html>

application.properties

properties

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.cache=false

index.html (继续)

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Spring Boot + Thymeleaf 示例</title>
</head>
<body>
    <h1 th:text="${message}">默认消息</h1>
    <p th:if="${user.active}">用户已激活</p>
    <p th:unless="${user.active}">用户未激活</p>
    <div th:switch="${user.role}">
        <p th:case="'admin'">管理员</p>
        <p th:case="'user'">普通用户</p>
        <p th:case="*">未知角色</p>
    </div>
    <a th:href="@{/profile(id=${user.id})}" rel="external nofollow"  rel="external nofollow" >查看资料</a>
    <div th:insert="~{commons :: header}">页面头部</div>
    <div th:replace="~{commons :: content}">页面内容</div>
</body>
</html>

Commons.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>公共片段</title>
</head>
<body>
    <div th:fragment="header">页面头部</div>
    <div th:fragment="content">页面内容</div>
</body>
</html>

总结

Thymeleaf 提供了丰富的语法来动态渲染 HTML 页面,包括表达式、选择、链接、属性、片段和内容等。通过这些语法,你可以轻松地在 Spring Boot 应用中实现数据驱动的页面渲染。在实际开发中,你可以根据项目需求灵活运用这些语法,以创建功能丰富且易于维护的 Web 应用。

以上内容涵盖了 Thymeleaf 的基本语法和示例代码,希望这能帮助你了解如何在 Spring Boot 项目中使用 Thymeleaf。

到此这篇关于springboot 中 thymeleaf 常用的语法的文章就介绍到这了,更多相关springboot thymeleaf 语法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java实现一个接口调取另一个接口(接口一调取接口二)

    java实现一个接口调取另一个接口(接口一调取接口二)

    这篇文章主要介绍了java实现一个接口调取另一个接口(接口一调取接口二),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • JavaWeb购物车项目开发实战指南

    JavaWeb购物车项目开发实战指南

    之前没有接触过购物车的东东,也不知道购物车应该怎么做,所以在查询了很多资料,总结一下购物车的功能实现,下面这篇文章主要给大家介绍了关于JavaWeb购物车项目开发的相关资料,需要的朋友可以参考下
    2022-06-06
  • Mysql字段和java实体类属性类型匹配方式

    Mysql字段和java实体类属性类型匹配方式

    这篇文章主要介绍了Mysql字段和java实体类属性类型匹配方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • java调用动态库dll详细示例代码

    java调用动态库dll详细示例代码

    最近应项目的需求,需要通过java调用dll动态库,于是找了一些资料,在此记录一下实现过程,这篇文章主要介绍了java调用动态库dll的相关资料,需要的朋友可以参考下
    2026-02-02
  • SpringBoot详细讲解通过自定义classloader加密保护class文件

    SpringBoot详细讲解通过自定义classloader加密保护class文件

    这篇文章主要介绍了SpringBoot通过自定义classloader加密class文件,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • 如何优雅的实现将Collection转为Map

    如何优雅的实现将Collection转为Map

    这篇文章主要介绍了如何优雅的实现将Collection转为Map,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-03-03
  • Hibernate中5个核心接口知识点整理

    Hibernate中5个核心接口知识点整理

    在本篇文章里小编给大家整理的是一篇关于Hibernate中5个核心接口知识点整理等内容,有兴趣的朋友们跟着学习参考下。
    2021-08-08
  • springboot自动扫描添加的BeanDefinition源码实例详解

    springboot自动扫描添加的BeanDefinition源码实例详解

    这篇文章主要给大家介绍了关于springboot自动扫描添加的BeanDefinition的相关资料,文中通过实例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2022-02-02
  • java struts2框架简介

    java struts2框架简介

    本文主要介绍了java struts2框架的基础知识。具有一定的参考价值,下面跟着小编一起来看下吧
    2017-01-01
  • 分享几个写简洁java代码的小技巧

    分享几个写简洁java代码的小技巧

    成为一个优秀的Java程序员,有着良好的代码编写习惯是必不可少的,下面这篇文章主要给大家介绍了关于写java代码的小技巧,文中通过图文以及实例代码介绍的非常详细,需要的朋友可以参考下
    2022-02-02

最新评论