Thymeleaf th:* 设置/修改属性值详解

属性汇总

属性描述示例
th:text计算其值表达式并将结果设置为标签的标签体< p th:text="${userName}">中国</ p>,值为 null 为空时,整个标签不显示任何内容。
th:utextth:text 会对结果中的特殊字符转义,th:utext 不会转义< p th:utext="${userInfo}">中国</ p>,值为 null 为空时,整个标签不显示任何内容。
th:attr为标签中的任意属性设置,可以一次设置多个属性< a href="" th:attr=“title=‘前往百度’,href=‘http://baidu.com’”>前往百度</ a>
th:*为 html 指定的属性设值,一次设置一个< a href="" th:title=‘前往百度’ th:href="‘http://baidu.com’">前往百度</ a>
th:alt-title同时为 alt 与 title 属性赋值< a href="#" th:alt-title="‘th:A-B’">th:A-B</ a>
th:lang-xmllang同时为 lang 、xmllang 属性赋值< head lang=“en” th:lang-xmllang=“en”>
th:fragment定义模板片段< div th:fragment=“copy”>
th:insert将被引用的模板片段插⼊到自己的标签体中< div th:insert="~{footer :: copy}"></ div>
th:replace将被引用的模板片段替换掉自己< div th:replace=“footer :: copy”></ div>
th:include类似于 th:insert,⽽不是插⼊⽚段,它只插⼊此⽚段的内容< div th:include=“footer :: copy”></ div>
th:remove删除模板中的某些代码片段< tr th:remove=“all”>…
th:each迭代数据,如 数组、List、Map 等< tr th:each=“user : ${userList}”>…
th:if条件为 true 时,显示模板⽚段,否则不显示< p th:if="${isMarry}">已婚1</ p>
th:unless条件为 false 时,显示模板⽚段,否则不显示< p th:unless="!${isMarry}">已婚2</ p>
th:switch与 Java 中的 switch 语句等效,有条件地显示匹配的内容< div th:switch=“1”>
th:case配合 th:switch 使用< div th:switch=“1”>< p th:case=“0”>管理员</ p>< p th:case=“1”>操作员</ p>< p th:case="*">未知用户</ p></ div>
th:with定义局部变量< div th:with=“userFirst=${userList[0]}”>
th:inline禁用内联表达式,内联js,内联css< script type=“text/javascript” th:inline=“javascript”>

th:text 与 th:utext

th:text
属性用于计算其值表达式并将结果设置为标签的标签体。
对表达式中结果中的特殊字符默认会进行转义处理。
th:utext
属性 (unescaped text) 不会对结果进行转义。

th:attr 设置任意属性值

th:attr 通过⼀个表达式即可将值赋给任意的多个属性,th:attr 以逗号分隔的形式来设置多个属性值。

<body>
<!--设置 title 属性-->
<a href="http://baidu.com" th:attr="title='百度'">百度</a>
<!--设置 title、href 多个属性-->
<a href="" th:attr="title='前往百度',href='http://baidu.com'">前往百度</a>
<!--设置 href 属性-->
<a href="userList.html" th:attr="href=@{/user/userHome}">用户首页</a>
<!--设置 id 属性,data-schoolName 属性 Html 本身是没有的,但允许用户自定义 -->
<a href="#" th:attr="id='9527',data-schoolName='护龙山庄'">归海一刀</a>
</body>

在这里插入图片描述

设置指定属性值

项目中通常使⽤ th:* 来特定的属性值,例如设置 value 属性,使⽤ th:value,设置 name 属性,则是 th:name 等等。

<body>
<!--设置 title 属性-->
<a href="http://baidu.com" th:title="百度">百度</a>
 
<!--设置 title、href 多个属性,注意:th:href 中的字符串因为有冒号、斜杠,所以要使用 单引号进行转义-->
<a href="" th:title='前往百度' th:href="'http://baidu.com'">前往百度</a>
 
<!--设置 href 属性-->
<a href="userList.html" th:href="@{/user/userHome}">用户首页</a>
 
<!--设置 id 属性,data-schoolName 属性 Html 本身是没有的,但允许用户自定义 -->
<a href="#" th:id="9525" th:data-schoolName="护龙山庄2号">归海一刀</a>
</body>

效果与上面 th:attr 完全一样。
HTML5 所有的属性,都可以使用 th:* 的形式进行设置值。

th:alt-title 和 th:lang-xmllang

th:alt-title 表示同时为 alt 属性与 title 属性赋值相同的值;th:lang-xmllang 表示同时为 lang 属性 xmllang 属性赋值相同的值。

注意:因为 alt 与 title 表达的意义基本相同,lang 与 xmllang 作用基本相同,所以 Thymeleaf 才为它们创建了这两个属性,对于其它的属性,则不能使用 “-” 进行操作。如 不能使用 th:href-title 来为 href 属性与 title 属性同时赋值相同的值。

< 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}" />
等价于
< img src="…/…/images/gtvglogo.png" th:src="@{/images/gtvglogo.png}" th:alt-title="#{logo}" />

<body>
<a href="#" th:alt-title="'th:A-B'">th:A-B</a>
<a href="#" th:href-title="'th:A-B'">错误示范</a>
</body>

固定值布尔属性

HTML 具有布尔属性的概念,这个布尔属性没有值,并且⼀旦这个布尔属性存在则意味着属性值为 “true”。 但是在 XHTML中,这些属性只取⼀个值,这个属性值就是它本身。

例如,checked 属性:
< input type=“checkbox” name=“option1” checked />
< input type=“checkbox” name=“option21” checked=“checked” />

Thymeleaf 标准⽅⾔允许通过计算条件表达式的结果来设置这些属性的值,如果条件表达式结果为 true,则该属性将被设置为其固定值,如果结果为 false,则不会设置该属性。

<body>
<!--option1、option2 这是平时的写法,两个复选框都会选中-->
<input type="checkbox" name="option1" checked/><span>是否已婚1?</span>
<input type="checkbox" name="option2" checked="checked"/><span>是否已婚2?</span>
 
<!--后台控制器传递了一个:model.addAttribute("isMarry", true);-->
<!--option3、option4 会选中;option5 不会选中-->
<input type="checkbox" name="option3" th:checked="${isMarry}"/><span>是否已婚3?</span>
<input type="radio" name="option4" th:checked="${isMarry}"/><span>是否本科?</span>
<input type="radio" name="option5" th:checked="!${isMarry}"/><span>是否应届生?</span>
</body>

在这里插入图片描述
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

默认属性处理器

Thymeleaf 提供了⼀个默认属性处理器,允许设置任何属性的值,即使在标准⽅⾔中没有为其定义特定的 th:* 处理器,即使属性不是 html 的标准属性,如:

<body>
<!--输出:<p abc="9527">th:abc="9527"</p>-->
<p th:abc="9527">th:abc="9527"</p>
 
<!--输出:<p abc123="华安">th:abc123="华安"</p>-->
<p th:abc123="华安">th:abc123="华安"</p>
</body>
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值