设置属性值-Thymeleaf常见用法(三)

来源:互联网 发布:tp5框架架构源码下载 编辑:程序博客网 时间:2024/05/17 09:37

设置属性值

任何属性

使用 th:attrib 设置某属性的值

<form action=“ subscribe.html” th:attr=“ action=@{/subscribe}” >

结果这样:

<form action= “ /subscribe” > 赋值或者重新设置值

重新设置img标签的属性值

<img src=“ ../../images/gtvglogo.png”

th:attr=“ src=@{/images/gtvglogo.png}, title=#{logo}, alt=#{logo}” />

设定特定属性的值

使用 th:* ,其中的*是想设置到特定属性。

比如打算设置value,那么使用 th:value,设置action的值,使用th:action即可

内置很多属性,详见使用手册 5.2 章节

一次性设置多个值

使用 th:arrtib1-attrib2

th:alt-title will set alt and title .

th:lang-xmllang will set lang and xml: lang .

附加

使用 th:attrappend 后面附加 th:attrprepend 前面附加

比如添加另外一个css样式,添加到class里面。

可以使用 th:classappend and th:styleappend,前者添加一个css的样式,后者添加style内容

固定值的布尔类型

在html中,有一种标签,没有值,按照一定的方式出现,它的值 true,但是在xhtml中,它的值是它本身。比如表单中的checked属性。

<input type=“ checkbox” name=“ option2” checked /> <! - - HTML - - >

<input type=“ checkbox” name=“ option1” checked=“ checked” /> <! - - XHTML - - >

当user.active值为true的时候,checked被设置成true,否则不设置

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

设置任意属性 使用默认的处理器

th:* 就算*中属性不存在于html tags中,仍然设置,例如:

<span th:whatever=“ ${user.name}” >…</span>

结果如下:

<span whatever=“ John Apricot” >…</span>

友好的支持html5标签和元素名

<table>

<tr data- th- each=“ user : ${users}” >

<td data- th- text=“ ${user.login}” >…</td>

<td data- th- text=“ ${user.name}” >…</td>

</tr>

</table>

data-{prefix}-{name}是一种标准的方式来定义h5属性,不需要使用namespace,th:

th:text= 等价于 th-text=

0 0
原创粉丝点击