簡介
這里總結(jié)的是我自己在學(xué)習(xí)時用到的一些語法。由于是用到才會去查日杈,因為太多我記不住朱浴。所以是記錄式學(xué)習(xí),會持續(xù)更新
標(biāo)簽
th:action
Form 表單
<form th:action="@{'/book/'+${book.id}}"></form>
th:value
Input 標(biāo)簽
<input th:value="${book.id}"/>
th:href
a 標(biāo)簽
<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
靜態(tài)資源引入
<script th:src="@{/resources/js/jquery/jquery.json-2.4.min.js}"
th:object
表單數(shù)據(jù)對象的綁定
用于表單數(shù)據(jù)對象綁定达椰,將表單綁定到后臺controller的一個JavaBean參數(shù)翰蠢。常與th:field一起使用進行表單數(shù)據(jù)綁定。
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:include
,th:replace
一起使用
- 聲明模板片段 /WEBINF/templates/footer. html
<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
[[…]]
之間的表達(dá)式在 Thymeleaf 被認(rèn)為是內(nèi)聯(lián)表達(dá)式藕甩,在其中您可以使用任何類型的表達(dá)式施敢,也會有效th:text屬性。
<p>Hello, [[${session.user.name}]]!</p>
等同于:
<p>Hello, <span th:text="${session.user.name}">Sebastian</span>!</p>
為了讓內(nèi)聯(lián)工作,必須激活它使用th:inline
屬性僵娃,它有三個可能的值或模式(text
, javascript
和 none
)概作。
<p th:inline="text">Hello, [[${session.user.name}]]!</p>
如果不使用th:inline="text",
則會被當(dāng)做字符串顯示。th:inline="javascript"
表示能在 js 中使用[ [] ]
取值默怨。
參考:[thymeleaf中的內(nèi)聯(lián) [ ] ]