Thymeleaf实战总结

介绍

以下总结了使用Thymeleaf做项目过程中碰到的有价值的知识点。拿出来分享!

1.配置context-path

  • 在公共模板中添加:
<script type="text/javascript" th:inline="javascript" th:fragment="ctx">
    /*<![CDATA[*/
    var ctx = /*[[@{/}]]*/ '';
    /*]]>*/
</script>

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 在需要ctx的页面中添加
<!--ctx-->
<script th:replace="~{fragment::ctx}"/>

   
   
  • 1
  • 2

2.遍历list

<a th:each="n,newsStat : ${newsList}" th:href="|/news/${n.id}|" 
class="list-group-item">
    <h5 class="mb-1" th:text="${n.getTitle()}">时政微视频丨与光同行</h5>
    <small th:text="${n.getCreateTime()}">2021.12.1,阅读:123</small>
</a>

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 状态变量
<tr th:each="prod,iterStat : ${prods}" th:class="${iterStat.odd}? 'odd'">
  <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>

   
   
  • 1
  • 2
  • 3
  • 4
  • 5

3.th:href字符串和变量混合拼接

th:href="|/list?l=${lid}|"

   
   
  • 1

4.datetime格式化

th:text="${#dates.format(n.createTime,'yyyy-MM-dd HH:mm:ss')}"

   
   
  • 1

5.输出html内容

<div class="col-9" th:utext="${news.content}"></div>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值