1. 程式人生 > 其它 >using thymeleaf之七設定屬性的值(th:attr/th:value/th:alt-title/th:attrappend/th:attrprepend/th:checked)

using thymeleaf之七設定屬性的值(th:attr/th:value/th:alt-title/th:attrappend/th:attrprepend/th:checked)

原文來自:https://blog.csdn.net/sun_jy2011/article/details/40215423

7.1 th:attr

用於設定其他屬性的值,但不是所有屬性的值都能設定,如text。

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

  

th:attr還可以同時設定多個屬性的值,以逗號隔開

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


7.2 th:alt-title

用於設定 alt 和title屬性的值相同的兩個屬性。

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

用th:alt-title後:

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


7.3 th:attrappend和th:attrprepend

th:attrappend屬性值字首,例如一個標籤的類名為a,想要變為“a b”,即增加一個類樣式,可以使用此屬性.

<input type="button" value="Do it!" class="btn" th:attrappend
="class=${' ' + cssStyle}" />


th:attrprepend的用法則相反,

th:attrappend="class=${cssStyle+' '}"  

7.4 th:checked設定複選框的值

此為布林值屬性之一,以後會詳細介紹其他屬性,在此先介紹th:checked。

(1)選中為:

<input type="checkbox" name="active" th:checked="true" /> 

(2)未選中為:

<input type="checkbox" name="noactive" th:checked="false" />  

thymeleaf解析時不會設定th:checked屬性,即解析後為

<input type="checkbox" name="noactive" />