设置属性值
原始表单
- 1
- 2
- 3
- 4
- 5
- 6
我们可以通过th:attr来设置input或者form或者其他标签的属性。如:
- 1
- 2
- 3
- 4
- 5
- 6
他会替换相同属性的值。结果如下:
- 1
- 2
- 3
- 4
- 5
- 6
当然,有的时候我们需要一次设置多个属性。怎么办?通过“,”隔开:
- 1
- 2
结果是:
- 1
通过特殊属性设置
上面的设置值方式很通用,但是很丑陋。
- 1
我们提供th:*这样的方式来替代th:attr。th:attr在项目中一般不建议使用。比如th:value
- 1
是不是比th:attr要简单些,同事避免你打错属性。同样的。
- 1
- 1
Thymeleaf属性列表
有很多这样的属性,它们每个都针对特定的XHTML或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:frame
th:frameborder
th:headers
th:height
th:high
th:href
th:hreflang
th:hspace
th:http-equiv
th:icon
th:id
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: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
同时设置多个属性值
- 1
- 2
或者这样
- 1
- 2
追加后面和追加前面的属性
th:attrappend 后面追加
th:attr
前面追加
例如:cssStyle=warning,追加后面
- 1
即为
- 1
这里还有两种类似的特殊属性th:classappend 和th:styleappend
- 1
固定值,boolean属性
有一些特殊属性的值是固定的。比如checked:
- 1
- 2
用法:
- 1
固定值,boolean类型属性列表如下:
th:async
th:autofocus
th:autoplay
th:checked
th:controls
th:declare
th:default
th:defer
th:disabled
th:formnovalidate
th:hidden
th:ismap
th:loop
th:multiple
th:novalidate
th:nowrap
th:open
th:pubdate
th:readonly
th:required
th:reversed
th:scoped
th:seamless
th:selected
html5的data语法支持
属性可以在HTML5中用这种方式写:data-{prefix}-{name}
- 1
- 2
- 3
- 4
- 5
- 6