thymeleaf使用

(1)  在pom.xml中引入thymeleaf

<dependency> 
  <groupId>org.springframework.boot</groupId> 
 
  <artifactId>spring-boot-starter-thymeleaf</artifactId> 
</dependency> 

(2)  关闭thymeleaf缓存,只在开发时关闭

    在application.properties配置

spring.thymeleaf.cache=false

(3) 前端html页面标签中引入如下:

<html xmlns:th="http://www.thymeleaf.org">

(4)在需要的标签里使用th:*

(5)获取变量值

    <p th:text="'Hello!, ' + ${name} + '!'" >3333</p>

    $符号获取变量值。name 是controller层里model.addAttribute保存的键。th:text标签的值替换p标签里面的值,3333会被覆盖


循环关键词:th:each

<tr th:each="prod : ${prods}">
  <td th:text="${prod.name}">Onions</td>
  <td th:text="${prod.price}">2.41</td>  <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>


条件判断,关键词th:if/unless

标签里加入 th:unless,表示:条件不通过时才会显示此标签.例<p th:unless="${name != null}">123</p>

标签里加入 th:if,表示:条件通过时才会显示此标签


Switch

Thymeleaf同样支持多路选择Switch结构:default可以用 * 表示

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

Utilities

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


注释   <!--/* 注释 */-->


选择表达式

*表达式

<div th:object="${user}">
  <p th:text="*{password}"></p>
</div>th:object="${user}"是获取controller层里model.addAttribute所保存的键,如model.addAttribute("user",user);th:text="*{password}" 获取user实体类里字段的值

修改时间格式

<p th:text="${#dates.format(users.datetime,'yyyy-MM-dd')}"></p>

                        users是循环体的别名,datetime是字段名


下拉框

<select name="subId"><option value="">请选择</option><option th:each="channelsub:${subchannels}"        th:selected="${channelsub.id == subId}"        th:value="${channelsub.id}"        th:text="${channelsub.name}"></option></select>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值