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动态代理,力求更易被理解。Java是JDK5中新加的机制,大家都知道Spring是用Java的动态代理实现的,那这个动态代理是什么东东呢,首先他肯定是个代理,我们先讲代理,把代理弄明白了,动态代理就好说了
    2014-02-02
  • Spring Boot中的微信支付全过程(小程序)

    Spring Boot中的微信支付全过程(小程序)

    微信支付是企业级项目中经常使用到的功能,作为后端开发人员,完整地掌握该技术是十分有必要的。今天通过本文给大家介绍Spring Boot中的微信支付全过程,感兴趣的朋友一起看看吧
    2022-05-05
  • JAVAEE中用Session简单实现购物车功能示例代码

    JAVAEE中用Session简单实现购物车功能示例代码

    本篇文章主要介绍了JAVAEE中用Session简单实现购物车功能示例代码,非常具有实用价值,需要的朋友可以参考下。
    2017-03-03
  • SpringMVC高级开发功能实现过程解析

    SpringMVC高级开发功能实现过程解析

    这篇文章主要介绍了SpringMVC高级开发功能实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • IDEA中的yml文件与properties互相转换

    IDEA中的yml文件与properties互相转换

    这篇文章主要介绍了IDEA中的yml文件与properties互相转换方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-10-10
  • 浅析Java中为什么要设计包装类

    浅析Java中为什么要设计包装类

    我们知道Java是一个面相对象的编程语言,基本类型并不具有对象的性质,为了让基本类型也具有对象的特征,就出现了包装类型,它相当于将基本类型“包装起来”,使得它具有了对象的性质,并且为其添加了属性和方法,丰富了基本类型的操作
    2021-06-06
  • spring-boot-maven-plugin报红解决方案(亲测有效)

    spring-boot-maven-plugin报红解决方案(亲测有效)

    本文主要介绍了spring-boot-maven-plugin报红解决方案,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • 在SpringBoot中,如何使用Netty实现远程调用方法总结

    在SpringBoot中,如何使用Netty实现远程调用方法总结

    我们在进行网络连接的时候,建立套接字连接是一个非常消耗性能的事情,特别是在分布式的情况下,用线程池去保持多个客户端连接,是一种非常消耗线程的行为.那么我们该通过什么技术去解决上述的问题呢,那么就不得不提一个网络连接的利器——Netty,需要的朋友可以参考下
    2021-06-06
  • SpringBoot 开发提速神器 Lombok+MybatisPlus+SwaggerUI

    SpringBoot 开发提速神器 Lombok+MybatisPlus+SwaggerUI

    这篇文章主要介绍了SpringBoot 开发提速神器 Lombok+MybatisPlus+SwaggerUI,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-03-03
  • Java Swing 多线程加载图片(保证顺序一致)

    Java Swing 多线程加载图片(保证顺序一致)

    这篇文章主要为大家详细介绍了Java Swing 多线程加载图片,保证顺序一致,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12

最新评论