原文链接:
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}" />
这个例子里给标签设置了三个属性标签:src、title、alt。自带的src会被Thymeleaf处理后扔掉而使用自己的。
这个标签不太优雅,不仅写起来紊乱,读起来也凌乱。所以很少用,一般用其他的代替:
<img src="../../images/gtvglogo.png"
th:src="@{/images/gtvglogo.png}" th:title="#{logo}" th:alt="#{logo}" />
作为th:attr的实例用法,Thymeleaf提供了几乎全部标签的Thymeleaf等价标签:
123 | 123 | 123 |
---|---|---|
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 |
比如:
<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提供了这些标签的等价标签,值可以是判断语句,不为真会删除该标签:
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 |
比如
<input type="checkbox" name="active" th:checked="${user.active}" />