首页
/ Thymeleaf教程:属性值设置详解

Thymeleaf教程:属性值设置详解

2025-06-27 21:27:07作者:胡唯隽

Thymeleaf作为一款现代化的Java模板引擎,提供了丰富的属性设置功能,让开发者能够灵活地操作HTML元素的属性。本文将深入讲解Thymeleaf中设置属性值的各种方式。

基础属性设置

th:attr通用属性设置

th:attr是Thymeleaf中最基础的属性设置方式,可以设置任意HTML属性:

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

渲染结果:

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

th:attr支持同时设置多个属性,用逗号分隔:

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

专用属性设置器

虽然th:attr很强大,但Thymeleaf为常见HTML属性提供了专门的设置器,使代码更加简洁:

<!-- 使用专用属性设置器 -->
<input type="submit" th:value="#{subscribe.submit}"/>
<form th:action="@{/subscribe}">...</form>

Thymeleaf几乎为所有HTML5属性都提供了对应的th:*设置器,包括:

  • th:idth:nameth:value等基础属性
  • th:classth:style等样式属性
  • th:onclickth:onchange等事件属性
  • th:srcth:href等资源属性

高级属性操作

多属性同时设置

Thymeleaf提供了两个特殊的复合属性设置器:

  1. th:alt-title:同时设置alttitle属性
  2. th:lang-xmllang:同时设置langxml:lang属性
<img th:src="@{/logo.png}" th:alt-title="#{logo}"/>

属性值追加

Thymeleaf提供了属性值追加功能:

  • th:attrappend:在现有属性值后追加内容
  • th:attrprepend:在现有属性值前添加内容
<input class="btn" th:attrappend="class=${' '+status}"/>
<!-- 当status='warning'时渲染为 -->
<input class="btn warning"/>

对于class和style属性,还有专门的追加器:

  • th:classappend:追加class
  • th:styleappend:追加style
<tr class="row" th:classappend="${odd}? 'odd'">

布尔属性处理

HTML中的布尔属性(如checked、disabled等)在Thymeleaf中有特殊处理方式:

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

user.active为true时渲染为:

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

为false时则不会添加checked属性。

Thymeleaf支持的所有布尔属性包括:

  • th:checkedth:disabledth:readonly等表单相关属性
  • th:autofocusth:required等HTML5属性
  • th:loopth:controls等媒体相关属性

HTML5友好语法

Thymeleaf支持HTML5的data-*属性语法,使得模板更加符合现代Web标准:

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

这种语法与传统的th:*语法完全等效,但更加符合HTML5规范。

默认属性处理器

当Thymeleaf遇到未定义的th:*属性时,会使用默认属性处理器,直接将计算结果设置为属性值:

<span th:custom-attr="${value}">...</span>
<!-- 渲染为 -->
<span custom-attr="some value">...</span>

总结

Thymeleaf提供了多种灵活的方式来设置和操作HTML属性:

  1. 使用th:attr进行通用属性设置
  2. 使用专用th:*属性设置器简化代码
  3. 使用复合属性设置器同时设置多个属性
  4. 使用属性追加器动态修改属性值
  5. 特殊处理布尔属性
  6. 支持HTML5友好的data-th-*语法
  7. 默认属性处理器提供最大灵活性

掌握这些属性设置技巧,能够让你在Thymeleaf模板开发中更加得心应手,编写出既简洁又强大的模板代码。

登录后查看全文
热门项目推荐

最新内容推荐

项目优选

收起
ohos_react_nativeohos_react_native
React Native鸿蒙化仓库
C++
178
262
RuoYi-Vue3RuoYi-Vue3
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
867
513
openGauss-serveropenGauss-server
openGauss kernel ~ openGauss is an open source relational database management system
C++
129
183
openHiTLSopenHiTLS
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
265
305
HarmonyOS-ExamplesHarmonyOS-Examples
本仓将收集和展示仓颉鸿蒙应用示例代码,欢迎大家投稿,在仓颉鸿蒙社区展现你的妙趣设计!
Cangjie
398
371
CangjieCommunityCangjieCommunity
为仓颉编程语言开发者打造活跃、开放、高质量的社区环境
Markdown
1.07 K
0
ShopXO开源商城ShopXO开源商城
🔥🔥🔥ShopXO企业级免费开源商城系统,可视化DIY拖拽装修、包含PC、H5、多端小程序(微信+支付宝+百度+头条&抖音+QQ+快手)、APP、多仓库、多商户、多门店、IM客服、进销存,遵循MIT开源协议发布、基于ThinkPHP8框架研发
JavaScript
93
15
note-gennote-gen
一款跨平台的 Markdown AI 笔记软件,致力于使用 AI 建立记录和写作的桥梁。
TSX
83
4
cherry-studiocherry-studio
🍒 Cherry Studio 是一款支持多个 LLM 提供商的桌面客户端
TypeScript
598
57
GitNextGitNext
基于可以运行在OpenHarmony的git,提供git客户端操作能力
ArkTS
10
3