Thymeleaf的內置屬性(轉)

原文連接:html

http://somefuture.iteye.com/blog/2253761app

Thymeleaf是另外一個Java視圖模板引擎,使用上和FreeMarker各有千秋,不瞭解的能夠從其餘博文裏學習一下。我這裏主要記錄一下它的內置屬性。less

 本文不是Thymeleaf入門教程,也不是對其標籤進行全面講解只對其屬性等價標籤進行記錄,覺得辭典。async

 

Thymeleaf提供了一個標籤th:attr,能夠把多個DOM標籤用逗號分隔後寫進去:oop

Html代碼   收藏代碼
  1. <img src="../../images/gtvglogo.png"  
  2. th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" />  

 這個例子裏給<img>標籤設置了三個屬性標籤:src、title、alt。<img>自帶的src會被Thymeleaf處理後扔掉而使用本身的。post

 

這個標籤不太優雅,不只寫起來紊亂,讀起來也凌亂。因此不多用,通常用其餘的代替:學習

 

Html代碼   收藏代碼
  1. <img src="../../images/gtvglogo.png"  
  2. th:src="@{/images/gtvglogo.png}" th:title="#{logo}" th:alt="#{logo}" />  

 做爲th:attr的實例用法,Thymeleaf提供了幾乎所有標籤的Thymeleaf等價標籤:ui

th:abbr th:accept th:accept-charset
th:accesskey th:action th:align
th:alt th:archive th:audio
th:autocomplete th:axis th:background
th:bgcolor th:border th:cellpadding
th:cellspacing th:challenge th:charset
th:cite th:class th:classid
th:codebase th:codetype th:cols
th:colspan th:compact th:content
th:contenteditable th:contextmenu th:data
th:datetime th:dir th:draggable
th:dropzone th:enctype th:for
th:form th:formaction th:formenctype
th:formmethod th:formtarget th:frame
th:frameborder th:headers th:height
th:high th:href th:hreflang
th:hspace th:http-equiv th:icon
th:id th:keytype th:kind
th:label th:lang th:list
th:longdesc th:low th:manifest
th:marginheight th:marginwidth th:max
th:maxlength th:media th:method
th:min th:name th:optimum
th:pattern th:placeholder th:poster
th:preload th:radiogroup th:rel
th:rev th:rows th:rowspan
th:rules th:sandbox th:scheme
th:scope th:scrolling th:size
th:sizes th:span th:spellcheck
th:src th:srclang th:standby
th:start th:step th:style
th:summary th:tabindex th:target
th:title th:type th:usemap
th:value th:valuetype th:vspace
th:width th:wrap th:xmlbase
th:xmllang th:xmlspace  

 好比:spa

Html代碼   收藏代碼
  1. <form action="subscribe.html" th:action="@{/subscribe}">  
  2. <href="product/list.html" th:href="@{/product/list}">Product List</a>  

 這裏使用了th:action和th:href標籤。code

 

Thymeleaf還提供了兩個合成標籤:

th:alt-title th:lang-xmllang

 用於同時設置兩個屬性,好比;

Html代碼   收藏代碼
  1. <img src="../../images/gtvglogo.png"  
  2. th:src="@{/images/gtvglogo.png}" <strong>th:alt-title</strong>="#{logo}" />  

 還有兩個CSS標籤:

 

 th:classappend  th:styleappend

意思顯而易見,用法以下:

Html代碼   收藏代碼
  1. <tr th:each="prod : ${prods}" class="row" th:classappend="${prodStat.odd}? 'odd'">  

 

 

對於判斷性的標籤,好比checked

Html代碼   收藏代碼
  1. <input type="checkbox" name="option1" checked="checked" />  

 只能使用checked做爲其值,即便使用true都很差使。Thymeleaf提供了這些標籤的等價標籤,值能夠是判斷語句,不爲真會刪除該標籤:

 

th:async th:autofocus th:autoplay
th:checked th:controls th:declare
th:default th:defer th:disabled
th:formnovalidate th:hidden th:ismap
th:loop th:multiple th:novalidate
th:nowrap th:open th:pubdate
th:readonly th:required th:reversed
th:scoped th:seamless th:selected

好比:

Html代碼   收藏代碼
  1. <input type="checkbox" name="active" th:checked="${user.active}" />  
相關文章
相關標籤/搜索