spring boot 学习七

spring boot 中 thymeleaf基本的用法

标准表达式

  1. 变量表达式:${ }
  2. 选择表达式:*{ }
  3. URL链接表达式:@{ }
  4. 条件表达式:三目运算符(表达式 ?值(then):值(else))
  5. 默认表达式:不带then(默认值为表达式的值,表达式 ?:值(else,表达式为null时))

常用语法

1.赋值、字符串拼接

<p th:text="${userName}">neo</p>
<span th:text="'Welcome to our application, ' + ${userName} + '!'"></span>
<span th:text="|Welcome to our application, ${userName}!|"></span>

2.条件判断

  1. If/Unless
<a th:if="${flag == 'yes'}"  th:href="@{/home}"> home </a>
<a th:unless="${flag != 'no'}" th:href="@{http://www.ityouknow.com/}" >ityouknow</a>

th:unless 与 th:if 相反,只有表达式中的条件不成立,才会显示其内容。

也可以使用 (if) ? (then) : (else) 来判断显示的内容。

  1. Switch
<div th:switch="${user.role}">
  <p th:case="'admin'">User is an administrator</p>
  <p th:case="#{roles.manager}">User is a manager</p>
</div>

3.for 循环

<table>
    <tr  th:each="user,iterStat : ${users}">
        <td th:text="${user.name}">neo</td>
        <td th:text="${user.age}">6</td>
        <td th:text="${user.pass}">213</td>
        <td th:text="${iterStat.index}">index</td>
    </tr>
</table>

iterStat 称作状态变量,属性有

  1. index:当前迭代对象的 index(从 0 开始计算)
  2. count:当前迭代对象的 index(从 1 开始计算)
  3. size:被迭代对象的大小
  4. current:当前迭代变量
  5. even/odd:布尔值,当前循环是否是偶数/奇数(从 0 开始计算)
  6. first:布尔值,当前循环是否是第一个
  7. last:布尔值,当前循环是否是最后一个

4.URL

Thymeleaf 对于 URL 的处理是通过语法 @{…} 来处理的。若需要 Thymeleaf 对 URL 进行渲染,那务必使用 th:href、th:src 等属性。

<a  th:href="@{http://www.ityouknow.com/{type}(type=${type})}">link1</a>
 
<a href="details.html" th:href="@{http://www.ityouknow.com/{pageId}/can-use-springcloud.html(pageId=${pageId})}">view</a>

设置背景:

<div th:style="'background:url(' + @{${img}} + ');'">

根据属性值改变背景:

<div class="media-object resource-card-image"  th:style="'background:url(' + @{(${collect.webLogo}=='' ? 'img/favicon.png' : ${collect.webLogo})} + ')'" ></div>

5.内联 [ [ ] ]

使用时,必须先用在 th:inline=“text/javascript/none” 激活,th:inline 可以在父级标签内使用,甚至作为 body 的标签。

文本内联:

<div th:inline="text" >
    <h1>内联js</h1>
    <p>Hello, [[${userName}]]</p>
    <br/>
</div>

脚本内联:

<script th:inline="javascript">
    var name = [[${userName}]] + ', Sebastian';
    alert(name);
</script>

脚本内联可以在 js 中取到后台传过来的参数。

6.内嵌变量

为了模板更加易用,Thymeleaf 还提供了一系列 Utility 对象(内置于 Context 中),可以通过 # 直接访问:

#dates:java.util.Date 的功能方法类

#calendars: 类似 #dates,面向 java.util.Calendar

#numbers:格式化数字的功能方法类

#strings:字符串对象的功能类,contains、startWiths、prepending/appending 等

#objects:对 objects 的功能类操作

#bools: 对布尔值求值的功能方法

#arrays:对数组的功能类方法

#lists:对 lists 的功能类方法

#sets

#maps

下面用一段代码来举例一些常用的方法:

date

/*
 * Format date with the specified pattern
 * Also works with arrays, lists or sets
 */
${#dates.format(date, 'dd/MMM/yyyy HH:mm')}
${#dates.arrayFormat(datesArray, 'dd/MMM/yyyy HH:mm')}
${#dates.listFormat(datesList, 'dd/MMM/yyyy HH:mm')}
${#dates.setFormat(datesSet, 'dd/MMM/yyyy HH:mm')}

/*
 * Create a date (java.util.Date) object for the current date and time
 */
${#dates.createNow()}

/*
 * Create a date (java.util.Date) object for the current date (time set to 00:00)
 */
${#dates.createToday()}

string

/*
 * Check whether a String is empty (or null). Performs a trim() operation before check
 * Also works with arrays, lists or sets
 */
${#strings.isEmpty(name)}
${#strings.arrayIsEmpty(nameArr)}
${#strings.listIsEmpty(nameList)}
${#strings.setIsEmpty(nameSet)}

/*
 * Check whether a String starts or ends with a fragment
 * Also works with arrays, lists or sets
 */
${#strings.startsWith(name,'Don')}                  // also array*, list* and set*
${#strings.endsWith(name,endingFragment)}           // also array*, list* and set*

/*
 * Compute length
 * Also works with arrays, lists or sets
 */
${#strings.length(str)}

/*
 * Null-safe comparison and concatenation
 */
${#strings.equals(str)}
${#strings.equalsIgnoreCase(str)}
${#strings.concat(str)}
${#strings.concatReplaceNulls(str)}

/*
 * Random
 */
${#strings.randomAlphanumeric(count)}

spring-boot1.4之后,支持thymeleaf3,可以更改版本号来进行修改支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值