中北软院创新实验室

Thymeleaf 语法 (持续更新)

2017-11-06  本文已影响32人  HikariCP

简介

这里总结的是我自己在学习时用到的一些语法。由于是用到才会去查,因为太多我记不住。所以是记录式学习,会持续更新


标签

th:action

Form 表单
<form th:action="@{'/book/'+${book.id}}"></form> 

th:value

Input 标签
<input th:value="${book.id}"/>

th:href

a 标签
<a th:href="@{'/book/'+${book.id}}"></a>

th:each

table 表格
<tr th:each="book: ${books}"></tr>

th:text

table 表格
<td th:text="${book.id}"></td>

th:if

div 体展示
<div th:if="${books != null}"></div>

th:src

静态资源引入
<script th:src="@{/resources/js/jquery/jquery.json-2.4.min.js}"

th:object

表单数据对象的绑定

用于表单数据对象绑定,将表单绑定到后台controller的一个JavaBean参数。常与th:field一起使用进行表单数据绑定。

public class LoginBean implements Serializable{...}{}
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(@ModelAttribute(value = "loginBean") LoginBean loginBean,ModelMap model){
        ...
    }
}
<form id="login-form" th:action="@{/login}" th:object="${loginBean}">...</form>

th:field

表单字段绑定,属性绑定、集合绑定。

注意操作符是 *

public class LoginBean implements Serializable {

    private String username;
    private List<User> user;

...}

---
public class User implements Serializable {

    private String username;;

}
---

@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@ModelAttribute(value = "loginBean") LoginBean loginBean,ModelMap model) {
  
}
<form id="login-form" th:action="@{/login}" th:object="${loginBean}">
    <input type="text" value="" th:field="*{username}"></input>
    <input type="text" value="" th:field="*{user[0].username}"></input>
</form>

th:id

div id声明
<div class="student" th:id = "stu+(${rowStat.index}+1)"></div>

th:fragment

声明定义该属性的div为模板片段。常用与头文件,页尾文件的引入。常与th:includeth:replace一起使用

<div th: fragment="copy" >
    © 2011 The Good Thymes Virtual Grocery
</div>
<div th: include=" /templates/footer : : copy" ></div>
<div th: replace=" /templates/footer : : copy" ></div>

th:inline

[[…]]之间的表达式在 Thymeleaf 被认为是内联表达式,在其中您可以使用任何类型的表达式,也会有效th:text属性。

<p>Hello, [[${session.user.name}]]!</p>

等同于:

<p>Hello, <span th:text="${session.user.name}">Sebastian</span>!</p>

为了让内联工作,必须激活它使用th:inline 属性,它有三个可能的值或模式(text, javascriptnone)。

<p th:inline="text">Hello, [[${session.user.name}]]!</p>

如果不使用th:inline="text",则会被当做字符串显示。th:inline="javascript"表示能在 js 中使用[ [] ]取值。

参考:[thymeleaf中的内联 [ ] ]

上一篇 下一篇

猜你喜欢

热点阅读