Thymeleaf的内置属性(转)

https://somefuture.iteye.com/blog/2253761


Thymeleaf的内置属性

博客分类: 

SpringBoot

Thymeleaf

血泪体验

thymeleaf

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等价标签:

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 

 比如:

<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

 用于同时设置两个属性,比如;

 还有两个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: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

比如:

<input type="checkbox" name="active" th:checked="${user.active}" />

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎,类似JSP,Velocity,Fre...
    Reg8888阅读 10,825评论 1 3
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,748评论 1 45
  • 《泥土》 ——瘦桶 我不是金子 不会光芒四射 没有被埋没的风险 我只是一把泥土 简单而平凡 有些人在我身上...
    瘦桶阅读 449评论 0 59
  • 一些电商的思维导图,大家有用的拿去。(ps:图都是比较清晰的,电脑端查看点“查看大图”,手机端双击放大) ​​​
    星球里的外星人阅读 375评论 0 0
  • 你只是昙花一现 出现在我的梦中 我希望一直是你 但是 你不会这么想 从来不会 你是我的初恋 我呢? 是你的谁 初恋...
    翠翠wjl阅读 450评论 0 0