您的位置:首页 > 其它

Thymeleaf的内置属性

2015-11-17 17:52 337 查看



博客分类:

Thymeleaf是另一个Java视图模板引擎,使用上和FreeMarker各有千秋,不了解的可以从其他博文里学习一下。我这里主要记录一下它的内置属性。

本文不是Thymeleaf入门教程,也不是对其标签进行全面讲解只对其属性等价标签进行记录,以为辞典。

Thymeleaf提供了一个标签th:attr,可以把多个DOM标签用逗号分隔后写进去:

Html代码


<img src="../../images/gtvglogo.png"

th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" />

这个例子里给<img>标签设置了三个属性标签:src、title、alt。<img>自带的src会被Thymeleaf处理后扔掉而使用自己的。

这个标签不太优雅,不仅写起来紊乱,读起来也凌乱。所以很少用,一般用其他的代替:


伦理片 http://www.dotdy.com/

Html代码

<img src="../../images/gtvglogo.png"

th:src="@{/images/gtvglogo.png}" th:title="#{logo}" th:alt="#{logo}" />

作为th:attr的实例用法,Thymeleaf提供了几乎全部标签的Thymeleaf等价标签:
th:abbrth:acceptth:accept-charset
th:accesskeyth:actionth:align
th:altth:archiveth:audio
th:autocompleteth:axisth:background
th:bgcolorth:borderth:cellpadding
th:cellspacingth:challengeth:charset
th:citeth:classth:classid
th:codebaseth:codetypeth:cols
th:colspanth:compactth:content
th:contenteditableth:contextmenuth:data
th:datetimeth:dirth:draggable
th:dropzoneth:enctypeth:for
th:formth:formactionth:formenctype
th:formmethodth:formtargetth:frame
th:frameborderth:headersth:height
th:highth:hrefth:hreflang
th:hspaceth:http-equivth:icon
th:idth:keytypeth:kind
th:labelth:langth:list
th:longdescth:lowth:manifest
th:marginheightth:marginwidthth:max
th:maxlengthth:mediath:method
th:minth:nameth:optimum
th:patternth:placeholderth:poster
th:preloadth:radiogroupth:rel
th:revth:rowsth:rowspan
th:rulesth:sandboxth:scheme
th:scopeth:scrollingth:size
th:sizesth:spanth:spellcheck
th:srcth:srclangth:standby
th:startth:stepth:style
th:summaryth:tabindexth:target
th:titleth:typeth:usemap
th:valueth:valuetypeth:vspace
th:widthth:wrapth:xmlbase
th:xmllangth:xmlspace
比如:

Html代码


<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-titleth:lang-xmllang
用于同时设置两个属性,比如;

Html代码


<img src="../../images/gtvglogo.png"

th:src="@{/images/gtvglogo.png}" <strong>th:alt-title</strong>="#{logo}" />

还有两个CSS标签:

th:classappendth:styleappend
意思显而易见,用法如下:

Html代码


<tr th:each="prod : ${prods}" class="row" th:classappend="${prodStat.odd}? 'odd'">

影音先锋电影 http://www.iskdy.com/
对于判断性的标签,比如checked

Html代码


<input type="checkbox" name="option1" checked="checked" />

只能使用checked作为其值,即使使用true都不好使。Thymeleaf提供了这些标签的等价标签,值可以是判断语句,不为真会删除该标签:

th:asyncth:autofocusth:autoplay
th:checkedth:controlsth:declare
th:defaultth:deferth:disabled
th:formnovalidateth:hiddenth:ismap
th:loopth:multipleth:novalidate
th:nowrapth:openth:pubdate
th:readonlyth:requiredth:reversed
th:scopedth:seamlessth:selected
比如:

Html代码


<input type="checkbox" name="active" th:checked="${user.active}" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: