thymeleaf中th:attr用法以及相關的thymeleaf基本表達式


額,有人寫的很好,我直接搬了

thymeleaf中th:attr用法

1、寫死的單個屬性值添加 th:attr="class=btn"
2、寫死的多個屬性值添加 th:attr="class=btn,title=link"
3、當一個屬性的值較多的時候可以用 | th:attr="class=|btn btn-group|"
4、屬性值動態賦值 th:attr="value=#{obj.value},title=#{obj.title}"
5、動態拼接屬性值 th:attr="value=select_val|#{obj.val}|"
6、屬性值中有引號的情況 th:attr="data-am-collapse=|{target:'#collapse-nav5'}|"

最后附上項目用到的代碼
th:attr="data-bg=@{${Globals.webUrlPrefix} + '/images/1-4.png?'+ ${Globals.version}}"

 

thymeleaf的基本表達式

${}變量表達式:用於訪問容器上下文環境中的變量
*{}選擇表達式:選擇表達式與變量表達式有一個重要的區別:選擇表達式顯示的是選定的對象。選擇的對象是一個:th:object對象屬性綁定的對象。

實際上還不如使用${session. user.firstName}

<div th:object ect=" ${session. user}" >

<p>Name: <span th: text=" *{firstName}" >Sebastian</span>. </p>

<p>Surname: <span th: text=" *{lastName}" >Pepper</span>. </p>

<p>Nationality: <span th: text=" *{nationality}" >Saturn</span>. </p>

</div>

#{}消息表達式(井號表達式,資源表達式):通常與th:text屬性一起使用,指明聲明了th:text的標簽的文本是#{}中的key所對應的value,而標簽內的文本將不會顯示。

例如:

新建/WEB-INF/templates/home.html

<p th: text=" #{home. welcome}" >This text will not be show! </p>

新建/WEB-INF/templates/home.properties

home.welcome=this messages is from home.properties!

測試結果:

從測試結果可以看出,消息表達式通常用於顯示頁面靜態文本,將靜態文本維護在properties文件中也方面維護,做國際化等。

實際上還不如使用${home. welcome}

 

@{}超鏈接url表達式:記住可通過網頁獲取的資源都應該用這個。

<script th:src="@{/resources/js/jquery/jquery.json-2.4.min.js}"

#maps工具對象表達式:常用於日期、集合、數組對象的訪問。這些工具對象就像是java對象,可以訪問對應java對象的方法來進行各種操作。

<div th:if="${#maps.size(stuReqBean.students[__${rowStat.index}__].score) != 0}">

<label>${score.key}:</label><input type="text" th:value="${score.value}"></input>

</div>

<div th:if="${#maps.isEmpty(stuReqBean.students[__${rowStat.index}__].score)}"> ...do something... </div>

 

其他工具對象表達式還有:

#dates
#calendars
#numbers
#strings
#objects
#bools
#arrays
#lists
#sets

前端是負責頁面展示的,你要這個多騷操作,前端同事表示js白學了。

實際上根本用不上

 

總結

${}變量表達式和@{}超鏈接url表達式兩個必須掌握

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM