Thymeleaf常用语法总结

发布时间:2021-08-20 19:54:39 作者:chen
来源:亿速云 阅读:166

Thymeleaf常用语法总结

Thymeleaf 是一个现代化的服务器端 Java 模板引擎,适用于 Web 和独立环境。它能够处理 HTML、XML、JavaScript、CSS 甚至纯文本。Thymeleaf 的主要目标是提供一种优雅且高度可维护的模板创建方式。本文将总结 Thymeleaf 的常用语法,帮助开发者快速上手和使用。

1. 基本语法

1.1 引入 Thymeleaf 命名空间

在 HTML 文件中使用 Thymeleaf 时,首先需要引入 Thymeleaf 的命名空间:

<html xmlns:th="http://www.thymeleaf.org">

1.2 表达式语法

Thymeleaf 提供了多种表达式语法,用于在模板中嵌入动态内容。

用于访问模型中的变量或属性。

  <p th:text="${user.name}">User Name</p>

用于选择当前对象中的属性。

  <div th:object="${user}">
      <p th:text="*{name}">User Name</p>
  </div>

用于国际化消息。

  <p th:text="#{welcome.message}">Welcome Message</p>

用于生成 URL。

  <a th:href="@{/user/profile}">Profile</a>

用于引用模板片段。

  <div th:replace="~{fragments/header :: header}"></div>

2. 常用属性

2.1 th:text

用于设置元素的文本内容。

<p th:text="${user.name}">User Name</p>

2.2 th:utext

用于设置元素的文本内容,且不转义 HTML 标签。

<p th:utext="${user.bio}">User Bio</p>

2.3 th:ifth:unless

用于条件渲染。

<p th:if="${user.isAdmin}">Admin User</p>
<p th:unless="${user.isAdmin}">Regular User</p>

2.4 th:each

用于循环遍历集合。

<ul>
    <li th:each="user : ${users}" th:text="${user.name}">User Name</li>
</ul>

2.5 th:object

用于指定当前对象。

<div th:object="${user}">
    <p th:text="*{name}">User Name</p>
</div>

2.6 th:hrefth:src

用于设置链接和资源路径。

<a th:href="@{/user/profile}">Profile</a>
<img th:src="@{/images/logo.png}" alt="Logo">

2.7 th:classth:style

用于动态设置 CSS 类和样式。

<div th:class="${user.isActive} ? 'active' : 'inactive'">User Status</div>
<div th:style="'color:' + ${user.color}">User Color</div>

3. 常用操作符

3.1 算术操作符

支持基本的算术操作符:+, -, *, /, %

<p th:text="${user.age + 5}">Age in 5 years</p>

3.2 比较操作符

支持比较操作符:>, <, >=, <=, ==, !=

<p th:if="${user.age > 18}">Adult</p>

3.3 逻辑操作符

支持逻辑操作符:and, or, not

<p th:if="${user.isActive and user.age > 18}">Active Adult</p>

3.4 条件操作符

支持三元条件操作符:condition ? then : else

<p th:text="${user.isActive} ? 'Active' : 'Inactive'">User Status</p>

4. 常用工具对象

Thymeleaf 提供了一些内置的工具对象,方便在模板中使用。

4.1 #dates

用于处理日期和时间。

<p th:text="${#dates.format(user.birthDate, 'yyyy-MM-dd')}">Birth Date</p>

4.2 #strings

用于处理字符串。

<p th:text="${#strings.toUpperCase(user.name)}">User Name</p>

4.3 #lists

用于处理列表。

<p th:text="${#lists.size(users)}">Number of Users</p>

4.4 #maps

用于处理映射。

<p th:text="${#maps.get(user.preferences, 'theme')}">User Theme</p>

5. 模板布局

5.1 片段引用

可以使用 th:replaceth:include 引用模板片段。

<div th:replace="~{fragments/header :: header}"></div>

5.2 布局继承

可以使用 th:fragment 定义可重用的模板片段。

<!-- fragments/header.html -->
<header th:fragment="header">
    <h1>Welcome to My Website</h1>
</header>

然后在其他模板中引用:

<div th:replace="~{fragments/header :: header}"></div>

6. 总结

Thymeleaf 是一个功能强大且灵活的模板引擎,适用于各种 Java Web 应用场景。通过掌握其常用语法和属性,开发者可以高效地创建和维护动态 Web 页面。本文总结了 Thymeleaf 的基本语法、常用属性、操作符、工具对象以及模板布局等内容,希望能够帮助读者快速上手 Thymeleaf。

推荐阅读:
  1. MySQL常用语法
  2. SpringBoot 之Thymeleaf模板

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

thymeleaf

上一篇:RHEL7.3怎么安装11G grid和Rdbms

下一篇:JS怎么用正则去除字符串最后的逗号

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》