可能是最全的Thymeleaf参考手册(五):设置属性值

 

本文将说明在Thymeleaf中,如何在标记中设置、或修改属性值的方法。

 

 

设置任何属性的值


假设有以下模板代码:

<form action="subscribe.html">
  <fieldset>
    <input type="text" name="email" />
    <input type="submit" value="Subscribe!" />
  </fieldset>
</form>

与Thymeleaf一样,此模板更像是静态原型,而不是Web应用程序的模板。首先,表单中的 action 属性静态链接到模板文件本身,因此没有地方进行有用的URL重写。其次,提交按钮中的 value 属性的值以英语文本显示,但我们希望将其国际化。

输入 th:attr 属性,引入其更改设置标签属性值的能力:

<form action="subscribe.html" th:attr="action=@{/subscribe}">
  <fieldset>
    <input type="text" name="email" />
    <input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/>
  </fieldset>
</form>

这个概念非常简单:th:attr 只是简单的为一个属性分配值的表达式。创建了相应的控制器和消息文件后,处理该文件的结果将是:

<form action="/gtvg/subscribe">
  <fieldset>
    <input type="text" name="email" />
    <input type="submit" value="¡Suscríbe!"/>
  </fieldset>
</form>

除了新的属性值,还可以看到应用程序上下文名称已自动添加为URL前缀,如/gtvg/subscribe

如果我们想一次设置多个属性怎么办?由于XML规则不允许在标记中设置属性两次,因此 th:attr 将采用逗号分隔的分配列表,例如:

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

给定所需的消息文件,将输出:

<img src="/gtgv/images/gtvglogo.png" title="Logo de Good Thymes" alt="Logo de Good Thymes" />

 

为特定属性设置值

 

如前面所述,为属性设置值的做法如下所示:

<input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/>

但是,这么做比较繁琐,还难看。不如在其对应属性内设置值直接且方便。况且,这也不是创建模块最优雅的方法。这就是为啥Thymeleaf模板中 th:attr 很少使用的原因。通常都会使用其对应的 th:* 属性,而这些属性的任务就是为特定属性设置值(但不仅仅是像 th:attr 这样的任何属性)。

例如,要设置 value 属性,则使用 th:value

<input type="submit" value="Subscribe!" th:value="#{subscribe.submit}"/>

再如:

<form action="subscribe.html" th:action="@{/subscribe}">

还有很多这样的属性,每个属性都针对特定的HTML5属性。

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:fragment
th:frameth:frameborderth:headers
th:heightth:highth:href
th:hreflangth:hspaceth:http-equiv
th:iconth:idth:inline
th:keytypeth:kindth:label
th:langth:listth:longdesc
th:lowth:manifestth:marginheight
th:marginwidthth:maxth:maxlength
th:mediath:methodth:min
th:nameth:onabortth:onafterprint
th:onbeforeprintth:onbeforeunloadth:onblur
th:oncanplayth:oncanplaythroughth:onchange
th:onclickth:oncontextmenuth:ondblclick
th:ondragth:ondragendth:ondragenter
th:ondragleaveth:ondragoverth:ondragstart
th:ondropth:ondurationchangeth:onemptied
th:onendedth:onerrorth:onfocus
th:onformchangeth:onforminputth:onhashchange
th:oninputth:oninvalidth:onkeydown
th:onkeypressth:onkeyupth:onload
th:onloadeddatath:onloadedmetadatath:onloadstart
th:onmessageth:onmousedownth:onmousemove
th:onmouseoutth:onmouseoverth:onmouseup
th:onmousewheelth:onofflineth:ononline
th:onpauseth:onplayth:onplaying
th:onpopstateth:onprogressth:onratechange
th:onreadystatechangeth:onredoth:onreset
th:onresizeth:onscrollth:onseeked
th:onseekingth:onselectth:onshow
th:onstalledth:onstorageth:onsubmit
th:onsuspendth:ontimeupdateth:onundo
th:onunloadth:onvolumechangeth:onwaiting
th:optimumth:patternth:placeholder
th:posterth:preloadth:radiogroup
th:relth:revth:rows
th:rowspanth:rulesth:sandbox
th:schemeth:scopeth:scrolling
th:sizeth:sizesth:span
th:spellcheckth:srcth:srclang
th:standbyth:startth:step
th:styleth:summaryth:tabindex
th:targetth:titleth:type
th:usemapth:valueth:valuetype
th:vspaceth:widthth:wrap
th:xmlbaseth:xmllangth:xmlspace

 

一次设置多个值

 

有两个比较特殊的属性 th:alt-title 和 th:lang-xmllang 可用于同时设置两个属性相同的值。特别:

  • th:alt-title 将设置 alt 和 title

  • th:lang-xmllang 将设置 lang 和 xml:lang

例如:

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

或与其等价的:

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

有了 th:alt-title 属性,则可以很方便的同时设置值:

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

 

 

追加和前置

 

Thymeleaf还提供 th:attrappend 和 th:attrprepend 属性,将对它们求值的结果附加(后缀)或前缀(前缀)到现有属性值。

例如,您可能希望将要添加(未设置,只是添加)的CSS类的名称存储在上下文变量中,因为要使用的特定CSS类将取决于用户所做的操作之前:

<input type="button" value="Do it!" class="btn" th:attrappend="class=${' ' + cssStyle}" />

如果将 cssStyle 变量设置为来处理此模板 "warning",则会得到:

<input type="button" value="Do it!" class="btn warning" />

标准方言中还有两个特定的附加属性:th:classappend 和 th:styleappend 属性,用于在元素上添加CSS类或样式片段而不覆盖现有属性的值:

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

 

固定布尔属性值

 

HTML具有布尔属性的概念,即没有值的属性,并且值为 1 即表示为“ true”。在XHTML中,这些属性仅有一个值,就是它本身。

例如checked:

<input type="checkbox" name="option2" checked /> <!-- HTML -->
<input type="checkbox" name="option1" checked="checked" /> <!-- XHTML -->

标准方言存在一些属性,这些属性可以通过评估条件来设置这些属性,因此,如果评估为 true,则该属性将设置为其固定值;如果评估为 false,则将不设置该属性:

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

标准方言中存在以下固定值布尔属性:

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

 

设置任何属性的值(默认属性处理器)

 

Thymeleaf提供了一个默认的属性处理器,即使我们在标准方言中没有为其定义任何特定的 th:* 处理器,它也允许我们设置任何属性的值 。

例如:

<span th:whatever="${user.name}">...</span>

将解析为:

<span whatever="John Apricot">...</span>

 

有好的支持HTML5属性和元素名

 

也可以使用完全不同的语法,以更友好的以HTML5方式将处理器应用于模板。

<table>
    <tr data-th-each="user : ${users}">
        <td data-th-text="${user.login}">...</td>
        <td data-th-text="${user.name}">...</td>
    </tr>
</table>

data-{prefix}-{name} 是HTML5中标准的自定义属性语法,而无需开发人员使用任何像 th:* 一样的命名空间名称。Thymeleaf使此语法自动适用于所有方言(不仅限于标准方言)。

还有一种用于指定自定义标记的语法:{prefix}-{name},它遵循W3C自定义元素规范(更大的W3C Web组件规范的一部分)。例如,这可以用于 th:block 元素(或 th-block),这将在后面的文章中进行说明。

重要提示:此语法是命名空间语法的补充 th:*,它不会替代它。完全没有打算将来弃用命名空间的语法。

 

 

笔者开通了个人微信公众号【银河架构师】,分享工作、生活过程中的心得体会,填坑指南,技术感悟等内容,会比博客提前更新,欢迎订阅。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值