Thymeleaf参考指南
  • Introduction
  • I. 使用Thymeleaf
    • 1 介绍Thymeleaf
      • 1.1 Thymeleaf是什么?
      • 1.2 Thymeleaf能处理什么类型的模版?
      • 1.3 方言:标准方言
    • 2 百里香虚拟杂货店
      • 2.1 杂货店网站
      • 2.2 创建和配置模板引擎
        • 模版解析器
        • 模版引擎
    • 3 使用文本
      • 3.1 多种语言显示欢迎
        • 使用th:text以及外部化文本
        • 上下文
        • 执行模版引擎
      • 3.2 与文本和变量有关的更多内容
        • 非转义文本
        • 使用和显示变量
    • 4 标准表达式语法
      • 4.1 信息
      • 4.2 变量
        • 表达式基本对象
        • 表达式工具对象
        • 重新格式化主页里的日期
      • 4.3 选择变量表达式(星号语法)
      • 4.4 链接URL
        • 主页的菜单栏
        • 服务器根相对路径
      • 4.5 分段
      • 4.6 字面量
        • 文本字面量
        • 数字字面量
        • 布尔字面量
        • null字面量
        • 字面量标记
      • 4.7 文本拼接
      • 4.8 字面量置换
      • 4.9 算术运算
      • 4.10 比较和相等
      • 4.11 条件表达式
      • 4.12 默认表达式(猫王运算符)
      • 4.13 无操作标记
      • 4.15 数据转换/格式化
      • 4.14 预处理
    • 5 设置属性值
      • 5.1 设置任何属性的值
      • 5.2 为指定的属性设置值
      • 5.3 一次设置多个值
      • 5.4 附加与前置
      • 5.5 定值布尔属性
      • 5.6 设置任何属性的值(默认的属性处理器)
      • 5.7 对HTML5友好的属性和元素名的支持
    • 6 遍历
      • 6.1 遍历基础
        • 使用th:each
        • 可遍历值
      • 6.2 保持遍历状态
      • 6.3 通过数据的懒惰检索实现优化
    • 7 条件求值
      • 7.1 简单条件:if与unless
      • 7.2 switch声明
    • 8 模版设计
      • 8.1 包含模版片段
        • 定义和引用片段
        • 片段规范语法
        • 不使用th:fragment引用片段
        • th:insert与th:replace(与th:include)的区别
      • 8.2 参数化片段签名
        • 不带片段参数的片段本地变量
        • th:assert——用于模板内断言
      • 8.3 弹性布局:超越片段插入
        • 使用空片段
        • 使用无操作标记
        • 片段的高级条件插入
      • 8.4 移除模版片段
    • 9 本地变量
    • 10 属性优先级
    • 11 注释与块
      • 11.1. 标准的HTML/XML注释
      • 11.2. Thymeleaf解析器级别注释块
      • 11.3. Thymeleaf原型注释块
      • 11.4. 人造th:block标签
    • 12 内联
      • 12.1. 表达式内联
        • 内联对战自然模版
        • 禁用内联
      • 12.2 文本内联
      • 12.3 JavaScript内联
        • JavaScript自然模版
        • 高级内联求值与JavaScript序列化
      • 12.4 CSS内联
        • 高级特性:CSS自然模版等
    • 13 文本模版模式
      • 13.1 文本语法
        • 转义的元素属性
      • 13.2 扩展性
      • 13.3 文本原型注释块:添加代码
      • 13.4 文本解析器级别注释块:移除代码
      • 13.5 自然JavaScript和CSS模版
    • 14 给我们的杂货店添加更多页面
      • 14.1 订单列表
      • 14.2 订单详情
    • 15 更多配置
      • 15.1 模板解析器
        • 链接模板解析器
      • 15.2 信息解析器
        • 标准信息解析器
        • 配置信息解析器
      • 15.3 转换服务
      • 15.4 日志记录
    • 16 模版缓存
    • 17 解耦的模版逻辑
      • 17.1 解耦的逻辑:概念
      • 17.2 配置解耦的模版
        • 启用解耦的模版
        • 混合耦合的与解耦的逻辑
      • 17.3 th:ref属性
      • 17.4 解耦的模板对性能的影响
      • 17.5 解耦的逻辑的解析
    • 18 附录A:表达基本对象
      • 基本对象
      • 请求或会话属性的网络上下文命名空间等
      • 网络上下文对象
    • 19 附录B:表达实用对象
      • 执行信息
      • 信息
      • URI/URL
      • 转换
      • 日期
      • 日历
      • 数字
      • 字符串
      • 对象
      • 布尔值
      • 数组
      • 列表
      • 集合
      • 映射
      • 合计
      • ID
    • 20 附录C:标记选择器语法
      • 多值类的匹配
Powered by GitBook
On this page

Was this helpful?

  1. I. 使用Thymeleaf
  2. 5 设置属性值

5.2 为指定的属性设置值

到目前为止,你可能觉得像:

<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属性也做相同的操作:

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

你还记得那些我们之前放进home.html的th:href吗?它们完全是同类属性:

<li><a href="product/list.html" th:href="@{/product/list}">Product List</a></li>

有类似的相当多的属性,它们中的每一个都对应了一个特定的HTML5属性:

th:abbr

th:accept

th:accept-charset

th:accesskey

th:action

th:align

th:alt

th:archive

th:audio

th:autocomplete

th:axis

th:background

th:bgcolor

th:border

th:cellpadding

th:cellspacing

th:challenge

th:charset

th:cite

th:class

th:classid

th:codebase

th:codetype

th:cols

th:colspan

th:compact

th:content

th:contenteditable

th:contextmenu

th:data

th:datetime

th:dir

th:draggable

th:dropzone

th:enctype

th:for

th:form

th:formaction

th:formenctype

th:formmethod

th:formtarget

th:fragment

th:frame

th:frameborder

th:headers

th:height

th:high

th:href

th:hreflang

th:hspace

th:http-equiv

th:icon

th:id

th:inline

th:keytype

th:kind

th:label

th:lang

th:list

th:longdesc

th:low

th:manifest

th:marginheight

th:marginwidth

th:max

th:maxlength

th:media

th:method

th:min

th:name

th:onabort

th:onafterprint

th:onbeforeprint

th:onbeforeunload

th:onblur

th:oncanplay

th:oncanplaythrough

th:onchange

th:onclick

th:oncontextmenu

th:ondblclick

th:ondrag

th:ondragend

th:ondragenter

th:ondragleave

th:ondragover

th:ondragstart

th:ondrop

th:ondurationchange

th:onemptied

th:onended

th:onerror

th:onfocus

th:onformchange

th:onforminput

th:onhashchange

th:oninput

th:oninvalid

th:onkeydown

th:onkeypress

th:onkeyup

th:onload

th:onloadeddata

th:onloadedmetadata

th:onloadstart

th:onmessage

th:onmousedown

th:onmousemove

th:onmouseout

th:onmouseover

th:onmouseup

th:onmousewheel

th:onoffline

th:ononline

th:onpause

th:onplay

th:onplaying

th:onpopstate

th:onprogress

th:onratechange

th:onreadystatechange

th:onredo

th:onreset

th:onresize

th:onscroll

th:onseeked

th:onseeking

th:onselect

th:onshow

th:onstalled

th:onstorage

th:onsubmit

th:onsuspend

th:ontimeupdate

th:onundo

th:onunload

th:onvolumechange

th:onwaiting

th:optimum

th:pattern

th:placeholder

th:poster

th:preload

th:radiogroup

th:rel

th:rev

th:rows

th:rowspan

th:rules

th:sandbox

th:scheme

th:scope

th:scrolling

th:size

th:sizes

th:span

th:spellcheck

th:src

th:srclang

th:standby

th:start

th:step

th:style

th:summary

th:tabindex

th:target

th:title

th:type

th:usemap

th:value

th:valuetype

th:vspace

th:width

th:wrap

th:xmlbase

th:xmllang

th:xmlspace

Previous5.1 设置任何属性的值Next5.3 一次设置多个值

Last updated 6 years ago

Was this helpful?