Thymeleaf 教程

将值设置为特定属性

现在,你可能会想:

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

上面代码是一段相当难看的标记。在 th:attr 属性值中指定赋值可能非常实用,但如果您必须一直这样做,那么这不是创建模板的最好方法。

Thymeleaf 同意您的观点,这就是为什么模板中很少使用 th:attr。通常,您将使用其他 th:* 属性,其任务是设置特定的标记属性(而不仅仅是 th:attr 之类的任何属性)。

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

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

这看起来好多了!让我们尝试对表单标记中的 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
说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号