当前位置: 代码网 > it编程>编程语言>Java > springboot 中 thymeleaf 常用的语法完整实例

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

2024年05月20日 Java 我要评论
在 spring boot 项目中,thymeleaf 是一个常用的模板引擎,它提供了丰富的语法来动态渲染 html 页面。以下是一些常用的 thymeleaf 语法,以及一个完整的 spring b

在 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 语法内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com