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表达式两个必须掌握

 

原文地址:https://www.cnblogs.com/ydymz/p/9378637.html