thymeleaf进阶使用

thymeleaf毕竟是一个脚本语言,在生成html时有一些特殊的字符串需要通过特定的拼接才能完成,本文主要介绍下在thymeleaf下字符串的常用操作

字符串拼接

<span th:text="'Welcome to our application, ' + ${user.name} + '!'">
<span th:text="|Welcome to our application, ${user.name}!|">
<span th:text="${onevar} + ' ' + |${twovar}, ${threevar}|">

url地址拼接

<!-- Will produce 'http://localhost:8080/gtvg/order/details?orderId=3&name=asd' (plus rewriting) -->
<a href="details.html"
   th:href="@{http://localhost:8080/gtvg/order/details(orderId=${o.id},name=${o.name})}">view</a>
<!-- Will produce '/gtvg/order/details?orderId=3' (plus rewriting) -->
<a href="details.html" th:href="@{/order/details(orderId=${o.id})}">view</a>
<!-- Will produce '/user/3/details?name=123&type=1' (plus rewriting) -->
<a href="details.html" th:href="@{/user/{id}/details(id=${user.id},name=${user.name},type=${user.userType})}">view</a>
<!-- /details/123?orderId=1 -->
<a th:href="@{'/details/'+${user.login}(orderId=${o.id})}">view</a>

判断

<span th:text="*{age}?: '(no age specified)'">27</span
<span th:text="*{age != null}? *{age} : '(no age specified)'">27</span>

以前两种是等价的 ?:表示在不等于null的情况下,输出值为判断条件,否则为定义的默认值
具体参考上篇文章

  • If-then: (if) ? (then)
  • If-then-else: (if) ? (then) : (else)
  • Default: (value) ?: (defaultvalue)

appending 和prepending

<input type="button" value="Do it!" class="btn" th:attrappend="class=${' ' + cssStyle}" />
<!-- If you process this template with the cssStyle variable set to "warning" , you will get: -->
<input type="button" value="Do it!" class="btn warning" />

<tr th:each="prod : ${prods}" class="row" th:classappend="${prodStat.odd}? 'odd'">

th:remove

当html作为模板使用时,会根据模板中定义的变量等等来生成html页面,但是如果直接作为静态页面来查看,他的显示效果并不好,所以出现了th:remove的标签
例如:

<table> <tr>
    <th>NAME</th>
    <th>PRICE</th>
    <th>IN STOCK</th>
    <th>COMMENTS</t
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Thymeleaf进阶方面,有一些资源可以帮助您深入学习和掌握Thymeleaf的高级功能和技巧。首先,您可以参考Thymeleaf的GitHub主页,其中提供了该项目的源代码和文档,了解最新版本的功能和更新。此外,您还可以查阅一些专门关于Thymeleaf的教程和指南,这些资源可以帮助您更好地理解Thymeleaf的用法和特性。 对于Thymeleaf字符串操作,您可以参考一些教程和博客文章,其中会介绍在Thymeleaf下常用的字符串操作。这些操作可以帮助您在生成HTML时,处理和调整字符串的内容,以及实现一些特定的需求。 另外,Thymeleaf也是基于Web标准的,特别是HTML5。它允许您创建完全符合验证规范的模板,并且在Spring Boot中,官方推荐使用Thymeleaf来替代JSP。这意味着Thymeleaf可以帮助您更好地构建和管理Web应用程序的前端部分,并且与后端的数据交互更加方便和灵活。 总结起来,在Thymeleaf进阶学习中,您可以参考Thymeleaf的GitHub主页、教程和指南,以及探索其在Web开发中的优势和特点。这些资源将帮助您更好地理解和应用Thymeleaf的高级功能,并提升您在使用Thymeleaf进行开发时的效率和技术水平。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Thymeleaf模板的学习一【基础知识】【java进阶编程】](https://blog.csdn.net/notlikeregist/article/details/90171063)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [thymeleaf进阶使用](https://blog.csdn.net/whatlookingfor/article/details/78353819)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值