原文鏈接:
http://somefuture.iteye.com/blog/2253761
Thymeleaf是另一個Java視圖模板引擎,使用上和FreeMarker各有千秋,不了解的可以從其他博文里學習一下。我這里主要記錄一下它的內置屬性。
本文不是Thymeleaf入門教程,也不是對其標簽進行全面講解只對其屬性等價標簽進行記錄,以為辭典。
Thymeleaf提供了一個標簽th:attr,可以把多個DOM標簽用逗號分隔后寫進去:
- <img src="../../images/gtvglogo.png"
- th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" />
這個例子里給<img>標簽設置了三個屬性標簽:src、title、alt。<img>自帶的src會被Thymeleaf處理后扔掉而使用自己的。
這個標簽不太優雅,不僅寫起來紊亂,讀起來也凌亂。所以很少用,一般用其他的代替:
- <img src="../../images/gtvglogo.png"
- th:src="@{/images/gtvglogo.png}" th:title="#{logo}" th:alt="#{logo}" />
作為th:attr的實例用法,Thymeleaf提供了幾乎全部標簽的Thymeleaf等價標簽:
|
比如:
- <form action="subscribe.html" th:action="@{/subscribe}">
- <a href="product/list.html" th:href="@{/product/list}">Product List</a>
這里使用了th:action和th:href標簽。
Thymeleaf還提供了兩個合成標簽:
th:alt-title | th:lang-xmllang |
用於同時設置兩個屬性,比如;
- <img src="../../images/gtvglogo.png"
- th:src="@{/images/gtvglogo.png}" <strong>th:alt-title</strong>="#{logo}" />
還有兩個CSS標簽:
th:classappend | th:styleappend |
意思顯而易見,用法如下:
- <tr th:each="prod : ${prods}" class="row" th:classappend="${prodStat.odd}? 'odd'">
對於判斷性的標簽,比如checked
- <input type="checkbox" name="option1" checked="checked" />
只能使用checked作為其值,即使使用true都不好使。Thymeleaf提供了這些標簽的等價標簽,值可以是判斷語句,不為真會刪除該標簽:
|
比如:
- <input type="checkbox" name="active" th:checked="${user.active}" />