Thymeleaf模板引擎

th属性

  • th:text 文本替换

  • th:value 值替换

  • th:object 父标签选择对象,子标签使用*{}取值

    1
    2
    3
    <div th:object="${session.user}">
    <p th:text="*{name}">name</p>
    </div>
  • th:each 遍历

    1
    2
    3
    4
    5
    <div th:each="c:${category[0]}" th:text="${c.categoryname}"></div>

    <div th:each="c:${category}">
    <label><input type="checkbox" th:value="${c.name}">[[${c.name}]]</label>
    </div>
  • th:if 条件判断

    1
    <a th:if="${id==collect.id}"></a>
  • th:selected 选择框选中

    1
    2
    3
    <select>
    <option th:selected="${name=='a'}"></option>
    </select>
  • th:src 替换src

    1
    <img th:src="@{/img/test.png}" width="350px" height="250px">
  • th:action 替换表单提交地址

    1
    <form th:action="@{/login}" th:method="post"></form>
  • th:href 替换链接路径

    1
    <a th:href="@{/task/task_add}"></a>
  • th:fragment 定义片段名字,可以被引用

  • th:replace/insert 使用th:fragment定义的片段替换/插入当前标签

    1
    2
    3
    <footer th:fragment="footer" id="test"></footer>
    <div th:replace="~{common/footer::footer(Url='task/task_add')}"></div>
    <div th:replace="common/footer::#test(Url='task/task_add')"></div>

SpringBoot整合Thymeleaf(通过添加模板属性)

1
2
3
4
5
6
7
import org.springframework.ui.Model;
@RequestMapping("/login")
public String toLogin(Model m) {
List<User> user = userMapper.getUser();
m.addAttribute("users",user);
return "login";
}
1
<div th:text="{users[0].name}"></div>
Author: Giftbear
Link: https://giftbear.github.io/2022/08/01/Thymeleaf模板引擎/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.