【Thymeleaf】Thymeleaf 学习笔记

深碍√TFBOYSˉ_ 2023-02-15 13:57 83阅读 0赞

目录

一、Thymeleaf 介绍

1.1 Thymeleaf 概念

1.2 Thyeleaf 特点

二、Thymeleaf 使用

2.1 基本语法

2.1.1 输出文本内容 th:text

2.1.2 表单提交 th:action

2.1.3 for循环 th:each

2.1.4 Map 输出

2.1.5 时间输出

2.1.6 条件判断

2.1.7 定义模块

2.1.8 图片识别

2.1.9 url 跳转


一、Thymeleaf 介绍

1.1 Thymeleaf 概念

thymeleaf 是一个模板引擎工具,主要用于页面渲染操作(页面数据填充操作),可以取代之前的 jsp 操作。

thymeleaf 是一个XML/XHTML/HTML5模板引擎,可以用于 Web 与非 Web 环境中的应用开发。在应用开发中,你可以使用 thymeleaf 来完全代替 JSP 或其他的模板引擎,如 Velocity、FreeMarker等。

thymeleaf 的主要目标在于提供一种可被浏览器正确显示的、格式良好的模板创建方式,因此也称为静态建模,你可以使用它创建经过验证的 XML/HTML 模板,开发者只需将标签属性添加到模板中即可,这些设定好的标签属性会在 DOM 上执行预先制定好的逻辑。

1.2 Thyeleaf 特点

开箱即用,支持处理六种模板:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0NDE2MzMx_size_16_color_FFFFFF_t_70

二、Thymeleaf 使用

2.1 基本语法

2.1.1 输出文本内容 th:text

  1. java:
  2. @GetMapping("/hello")
  3. public String hello(Model model) {
  4. model.addAttribute("message", "hello thymeleaf");
  5. return "demo1";
  6. }
  7. html:
  8. <div th:text="${message}"></div>
  9. <!-- 能够识别到html标签并显示 -->
  10. <div th:utext="${message}"></div>

2.1.2 表单提交 th:action

  1. <form id="login-form" th:action="@{/test/hello}">
  2. <button>提交</button>
  3. </form>

2.1.3 for循环 th:each

  1. <table>
  2. <tr>
  3. <th>下标</th>
  4. <th>ID</th>
  5. <th>姓名</th>
  6. <th>地址</th>
  7. </tr>
  8. <!--
  9. 第一个参数 user: 当前被循环的对象 item
  10. 第二个参数 state: 当前被循环对象的状态记录,如下标、第几条
  11. -->
  12. <tr th:each="user,state:${users}">
  13. <td th:text="${state.count}"></td>
  14. <td th:text="${user.id}"></td>
  15. <td th:text="${user.name}"></td>
  16. <td th:text="${user.address}"></td>
  17. </tr>
  18. </table>

2.1.4 Map 输出

  1. --------------------- .java ------------------
  2. Map<String, Object> dataMap = new HashMap<String, Object>();
  3. dataMap.put("num", "105");
  4. dataMap.put("name", "小王");
  5. dataMap.put("address", "深圳");
  6. model.addAttribute("dataMap", dataMap);
  7. --------------------- .html ------------------
  8. <div>
  9. <!--
  10. 读取map的种方式
  11. 1. 知道 map key,根据 key 直接获取数据
  12. 2. 不知道 key,使用循环方式获取key,然后获取数据
  13. -->
  14. <h1>方式一</h1>
  15. <div>获取num的值:<span th:text="${dataMap.num}"></span></div>
  16. <div>获取name的值:<span th:text="${dataMap.name}"></span></div>
  17. <div>获取address的值:<span th:text="${dataMap.address}"></span></div>
  18. <h1>方式二</h1>
  19. <div th:each="item:${dataMap}">
  20. <div><span th:text="${item.key}"></span><span th:text="${item.value}"></span></div>
  21. </div>
  22. </div>

2.1.5 时间输出

  1. <span th:text="${#dates.format(now, 'yyyy-MM-dd HH:mm:ss')}"></span>

2.1.6 条件判断

下面这两句话是等同的

  1. # 当条件成立 th:if
  2. <span th:if="${age>18}">成年人</span>
  3. # 当条件不成立 th:unless
  4. <span th:unless="${age>18}">成年人</span>

2.1.7 定义模块

th: fragment 用于定义一个独立的模块,可以被其他页面引用

定义一个 footer.html

  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>子模块</title>
  6. </head>
  7. <body>
  8. <!-- 定义一个模块 -->
  9. <div th:fragment="bar">
  10. 关于我们<br/>
  11. </div>
  12. </body>
  13. </html>

在展示页面中使用 th:include=”[文件名称]::[片段名称]“引用

  1. <div th:include="footer::bar"></div>

2.1.8 图片识别

  1. <img th:src="${item.image}" />

2.1.9 url 跳转

url表达式有其特殊的格式:

  1. @{}

如下面实现点击跳转:

使用 @{}表明这是修改url ${url}可以获取到当前url 括号中可以是扩充的内容

  1. <a th:href="@{${url}(category=${category})}" th:text="${category}"></a><em th:unless="${state.last}"></em>

20200607133821548.png

发表评论

表情:
评论列表 (有 0 条评论,83人围观)

还没有评论,来说两句吧...

相关阅读

    相关 学习笔记

    \ajax: 1、概念:异步的JavaScript 和 xml 1.1异步和同步:客户端和服务器端相互通信的基础上 \客户端必须等待服务器端的响应。在等待的期间客户