Thymeleaf初使用

本文介绍了Thymeleaf的基础用法,包括认识Thymeleaf的特性,如作为Spring MVC的模版引擎,静态模板运行及动态内容替换。讨论了布局定义,如th:include和th:replace的区别,并展示了条件判断If/Unless、for循环、URL处理和内联js的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

认识:

      最近项目后台架构采用springboot在spring4.0中推荐使用thymeleaf来做前端模版引擎,Thymeleaf是一个java库,是XMLHTML5等格式的模板引擎,VelocityFreeMarker相似,可以用于Web项目和非Web项目。

       Thymeleaf可以作为Spring MVC的可选模块,也可以直接作为JSP的替代。Thymeleaf提供了两个版本,标准和spring标准两种方言,并且可以通过创建自定义方言进行扩展。thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。Thymeleaf的主要目标在于提供一种可被浏览器正确显示的、格式良好的模板创建方式,因此也可以用作静态建模。

布局:

定义代码片段

<footer th:fragment="copy"> 
&copy; 2016
</footer>
 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

在页面任何地方引入:

<body> 
  <div th:include="footer :: copy"></div>
  <div th:replace="footer :: copy"></div>
 </body>
 
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

th:include 和 th:replace区别,include只是加载,replace是替换

返回的HTML如下:

<body> 
   <div> &copy; 2016 </div> 
  <footer>&copy; 2016 </footer> 
</body>
 
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

下面是一个常用的后台页面布局,将整个页面分为头部,尾部、菜单栏、隐藏栏,点击菜单只改变content区域的页面

<body class="layout-fixed">
  <div th:fragment="navbar"  class="wrapper"  role="navigation">
    <div th:replace="fragments/header :: header">Header</div>
    <div th:replace="fragments/left :: left">left</div>
    <div th:replace="fragments/sidebar :: sidebar">sidebar</div>
    <div layout:fragment="content" id="content" ></div>
    <div th:replace="fragments/footer :: footer">footer</div>
  </div>
</body>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

任何页面想使用这样的布局值只需要替换中见的 content模块即可

 <html xmlns:th="http://www.thymeleaf.org" layout:decorator="layout">
   <body>
      <section layout:fragment="content">
    ...
 
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

也可以在引用模版的时候传参

<head th:include="layout :: htmlhead" th:with="title='Hello'"></head>
 
  • 1
  • 1

layout 是文件地址,如果有文件夹可以这样写 fileName/layout:htmlhead 
htmlhead 是指定义的代码片段 如 th:fragment="copy"

常见用法:

1、赋值、字符串拼接

 <p  th:text="${collect.description}">description</p>
 <span th:text="'Welcome to our application, ' + ${user.name} + '!'">
 
  • 1
  • 2
  • 1
  • 2

字符串拼接还有另外一种简洁的写法

<span th:text="|Welcome to our application, ${user.name}!|">
 
  • 1
  • 1

2、条件判断 If/Unless

Thymeleaf中使用th:if和th:unless属性进行条件判断,下面的例子中,<a>标签只有在th:if中条件成立时才显示:

<a th:if="${myself=='yes'}" > </i> </a>
<a th:unless=${session.user != null} th:href="@{/login}" >Login</a>
 
  • 1
  • 2
  • 1
  • 2

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

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

3、for 循环

  <tr  th:each="collect,iterStat : ${collects}"> 
     <th scope="row" th:text="${collect.id}">1</th>
     <td >
        <img th:src="${collect.webLogo}"/>
     </td>
     <td th:text="${collect.url}">Mark</td>
     <td th:text="${collect.title}">Otto</td>
     <td th:text="${collect.description}">@mdo</td>
     <td th:text="${terStat.index}">index</td>
 </tr>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

iterStat称作状态变量,属性有:

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

4、URL

URL在Web应用模板中占据着十分重要的地位,需要特别注意的是Thymeleaf对于URL的处理是通过语法@{…}来处理的。 
如果需要Thymeleaf对URL进行渲染,那么务必使用th:href,th:src等属性,下面是一个例子

<!-- Will produce 'http://localhost:8080/standard/unread' (plus rewriting) -->
 <a  th:href="@{/standard/{type}(type=${type})}">view</a>

<!-- Will produce '/gtvg/order/3/details' (plus rewriting) -->
<a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

设置背景

<div th:style="'background:url(' + @{/<path-to-image>} + ');'"></div>
 
  • 1
  • 1

根据属性值改变背景

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

几点说明:

  • 上例中URL最后的(orderId=${o.id}) 表示将括号内的内容作为URL参数处理,该语法避免使用字符串拼接,大大提高了可读性
  • @{...}表达式中可以通过{orderId}访问Context中的orderId变量
  • @{/order}是Context相关的相对路径,在渲染时会自动添加上当前Web应用的Context名字,假设context名字为app,那么结果应该是/app/order

5、内联js

内联文本:[[…]]内联文本的表示方式,使用时,必须先用th:inline=”text/JavaScript/none”激活,th:inline可以在父级标签内使用,甚至作为body的标签。内联文本尽管比th:text的代码少,不利于原型显示。

<script th:inline="javascript">
/*<![CDATA[*/
...
var username = /*[[${sesion.user.name}]]*/ 'Sebastian';
var size = /*[[${size}]]*/ 0;
...
/*]]>*/
</script>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

js附加代码:

/*[+
var msg = 'This is a working application';
+]*/
 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

js移除代码:

/*[- */
var msg = 'This is a non-working template';
/* -]*/


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值