Thymeleaf 基本用法总结

一、引用命名空间 <html xmlns:th="http://www.thymeleaf.org">

在html中引入此命名空间,可避免编辑器出现html验证错误,虽然加不加命名空间对Thymeleaf的功能没有任何影响。

二、输出内容

2.1  <p th:text="#{home.welcome}">Welcome to our grocery store!</p>

说明:

1. th:text  用来将内容输出到所在标签的body中。

2. #{home.welcome} 用来引入数据home对象中的 welcome属性。

3. 可以用th:utext 用来显示“unescaped ” 的html内容。

2.2    <p>Today is: <span th:text="${today}">13 February 2011</span></p>

说明:${today} 用来引用 today 变量

三、访问对象

${param.x} 返回名为x 的 request参数。(可能有多个值)

${session.x} 返回名为x的Session参数。

${application.x} 返回名为 servlet context 的参数。

四、基本语法

4.1  #{home.welcome} --  访问数据

4.2  #{home.welcome(${session.user.name})}  -- 格式化数据 当 home.welcome 为 "abcdegf{0}"  类似这种内容时。(多个参数以逗句分隔)。

4.3  ${today} --- 访问变量

4.4  访问基本对象

#ctx: the context object.
#vars: the context variables.
#locale: the context locale.
#request: (only in Web Contexts) the HttpServletRequest object.
#response: (only in Web Contexts) the HttpServletResponse object.
#session: (only in Web Contexts) the HttpSession object.
#servletContext: (only in Web Contexts) the ServletContext object.

其它公共对象参考: http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#appendix-a-expression-basic-objects

4.5  日期的输出

<span th:text="${#calendars.format(today,'dd MMMM yyyy')}">13 May 2011</span>

4.6  星号语法

<div th:object="${session.user}">
<p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div>

4.7  输出URL

<a href="product/list.html" th:href="@{/product/list}">Product List</a>

<a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>

4.8  使用代码段

<div th:insert="~{commons :: main}">...</div>

4.9  直接输出内容

<span th:text="'working web application'"> -- 输出字符

<span th:text="2013 + 2">  -- 输出数据表达式

<div th:if="${user.isAdmin()} == false">  --输出布尔表达式

<span th:text="'Welcome to our application, ' + ${user.name} + '!'"> -- 带变量的

4.10 条件表达式

<tr th:class="${row.even}? 'even' : 'odd'">
...  
</tr>

<tr th:class="${row.even}? 'alt'">
...省略 false 结果的表达方式
</tr>

<div th:object="${session.user}">
...省略 true 结果的表达方式
<p>Age: <span th:text="*{age}?: '(no age specified)'">27</span>.</p>
</div>

<span th:text="${user.name} ?: _">no user authenticated</span> --不做任何处理时用下划线 _ 表示

4.11  格式化

<td th:text="${{user.lastAccessDate}}">...</td> --${{.}}  调用默认的格式化器来输出结果。

4.12  预处理

<p th:text="${__#{article.text('textVar')}__}">Some text here...</p>

说明:thymeleaf 的处理模板内容的顺序与书写顺序无关,只能通过  __${expression}__ ,来将需要先一步计算出来后面          要用的变量指定为优化处理。

五、设置 Attribute 值

5.1 设置任何Attribute 的方法

<input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/>   --设置单个

<img src="../../images/gtvglogo.png"  th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" />  --一次设置多个

5.2 设置一些内置的Attribute的方法

<li><a href="product/list.html" th:href="@{/product/list}">Product List</a></li>

<form action="subscribe.html" th:action="@{/subscribe}">

<input type="submit" value="Subscribe!" th:value="#{subscribe.submit}"/>

<img src="../../images/gtvglogo.png"  th:src="@{/images/gtvglogo.png}" th:alt-title="#{logo}" /> -- 一次设置多个(alt title)的方法

其它的可用属性:http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#setting-value-to-specific-attributes

5.3 设置html里没有指的任何属性的语法

<span th:whatever="${user.name}">...</span>   ---whatever 可以换成任何你想设的属性

六、循环输出的语法

6.1 基本循环

<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>

6.2 循环状态的使用

<table>
<tr>
<th>NAME</th>
<th>PRICE</th>
<th>IN STOCK</th>
</tr>
<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>
</table>

关于状态的其它信息的使用详细参考:http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#keeping-iteration-status

七、条件判断

7.1 if 和 unless

<a href="comments.html" th:href="@{/comments(prodId=${prod.id})}" th:unless="${#lists.isEmpty(prod.comments)}">view</a>

<a href="comments.html"  th:href="@{/product/comments(prodId=${prod.id})}"   th:if="${not #lists.isEmpty(prod.comments)}">view</a>

7.2 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>
<p th:case="*">User is some other thing</p>    --默认的 case 相当于default
</div>

八、模板 include

8.1 定义和引用代码块

定义代码块

<!DOCTYPE html>

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

<body>

<div th:fragment="copy">
&copy; 2011 The Good Thymes Virtual Grocery
</div>

</body>

</html>

引用代码块

<body>

...

<div th:insert="~{footer :: copy}"></div>

</body>

引用未用fragment 标注的代码块

<div id="copy-section">
&copy; 2011 The Good Thymes Virtual Grocery
</div>

<body>

...

<div th:insert="~{footer :: #copy-section}"></div>

</body>

8.2 th:insert th:replace th:include 之间的区别

th:insert  --- 插入代码块    th:replace -- 替换代码块会替换掉容器标签   th:include ---- 和insert相似但只会插入fragment标注body内的内容。

8.3  带参数的代码段

<div th:fragment="frag (onevar,twovar)">
<p th:text="${onevar} + ' - ' + ${twovar}">...</p>
</div>

<div th:replace="::frag (${value1},${value2})">...</div>
     <div th:replace="::frag (onevar=${value1},twovar=${value2})">...</div>

九、局部变量的使用示例

<div th:with="firstPer=${persons[0]}">
<p>
The name of the first person is <span th:text="${firstPer.name}">Julius Caesar</span>.
</p>
</div>

<div th:with="firstPer=${persons[0]},secondPer=${persons[1]}">
<p>
The name of the first person is <span th:text="${firstPer.name}">Julius Caesar</span>.
</p>
<p>
But the name of the second person is 
<span th:text="${secondPer.name}">Marcus Antonius</span>.
</p>
</div>

十、注释

<!-- ... -->  

十一、说明

以上只列出Thymeleaf了简要常用的语法和使用方式,更多详情的说明和规则请参见:http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#introducing-thymeleaf

Thymeleaf 基本用法总结的更多相关文章

  1. springboot配置server相关配置&整合模板引擎Freemarker、thymeleaf&thymeleaf基本用法&thymeleaf 获取项目路径 contextPath 与取session中信息

    1.Springboot配置server相关配置(包括默认tomcat的相关配置) 下面的配置也都是模板,需要的时候在application.properties配置即可 ############## ...

  2. Thymeleaf基本用法

    1.Thymeleaf简介 官方网站:https://www.thymeleaf.org/index.html Thymeleaf是用来开发Web和独立环境项目的现代服务器端Java模板引擎. 2.特 ...

  3. thymeleaf的常见用法

    1,th:属性名="",就可以直接修改控件的属性,比如 <input th:type="button" th:name="xinxin" ...

  4. Thymeleaf【快速入门】

    前言:突然发现自己给自己埋了一个大坑,毕设好难..每一个小点拎出来都能当一个小题目(手动摆手..),没办法自己选的含着泪也要把坑填完..先一点一点把需要补充的知识学完吧.. Thymeleaf介绍 稍 ...

  5. SpringMVC整合Thymeleaf

    Thymeleaf的介绍 进行JavaWeb开发时主要用到的是JSP,传统的JSP需要在页面中加入大量的JSTL标签,这些标签只能运行在服务器中,前端开发人员维护这些页面比较困难,页面加载速度也比较慢 ...

  6. 前端渲染模板(一):Thymeleaf

    一.使用 本篇文章将以SpringBoot为框架来介绍Thymeleaf的用法. 1 资源文件的约定目录结构  Maven的资源文件目录:/src/java/resources spring-boot ...

  7. 【SpringBoot】转载 springboot使用thymeleaf完成数据的页面展示

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/weixin_36380516/artic ...

  8. 转--Spring MVC : Java模板引擎 Thymeleaf (三)

    原文:http://www.csdn.com/html/topnews201408/49/1349.htm 下面以构造一个表单开始,讲解 Thymeleaf的用法.为了演示方便,还是以经典的注册为例. ...

  9. SpringBoot 整合 Thymeleaf & 如何使用后台模板快速搭建项目

    如果你和我一样,是一名 Java 道路上的编程男孩,其实我不太建议你花时间学 Thymeleaf,当然他的思想还是值得借鉴的.但是他的本质在我看来就是 Jsp 技术的翻版(Jsp 现在用的真的很少很少 ...

随机推荐

  1. 2019ICPC上海网络赛A 边分治+线段树

    题目: 给定一棵树, 带边权. 现在有2种操作: 1.修改第i条边的权值. 2.询问u到其他一个任意点的最大距离是多少. 解法:边分治+线段树 首先我们将所有的点修改和边修改都存在对应的边里面. 然后 ...

  2. laravel5.6操作数据curd写法(查询构建器)

    laravel5.6 数据库操作-查询构建器 <?php //laravel5.6 语法 demo示例 namespace App\Http\Controllers;//命名该控制App空间下名 ...

  3. php面试专题---11、开发环境及配置考点

    php面试专题---11.开发环境及配置考点 一.总结 一句话总结: 了解php运行原理及常见的配置项 1.版本控制软件? 集中式:CVS和SVN 分布式:Git 2.请简述CGI.FastCGI和P ...

  4. ST表——————一失足成千古恨系列2

    在此先祝自己这个系列写的越少越好qwq(保证不超过4篇(flag已立)) 考试原题:(绝壁是看完复联出的) 第一反应:线段树??不对,是st表.嗯,没错.哎,st表咋写来着??完了凉了. 结果:写暴搜 ...

  5. ElasticSearch ClusterBlockException[blocked by: [FORBIDDEN/12/index read-only / allow delete (api)]锁定状态,无法插入数据

    PUT /twitter/_settings { "index.blocks.read_only_allow_delete": null } 官网给出的解决办法

  6. iOS 创建.xcworkspace文件

    首先创建一个HelloWorld工程,步骤如下:iOS开发环境搭建 及 编写1个hello world 然后: 1. 终端run $ cd到.xcodeproj同级文件夹 2. 终端run $ pod ...

  7. 从有状态应用(Session)到无状态应用(JWT),以及 SSO 和 OAuth2

    不管用哪种方式认证用户,都可能被中间人攻击窃取 SessionID 或 Token,从而发生 CSRF 攻击.解决方式就是全站 HTTPS.现在 Let's Encrypt 已经支持免费的通配符 HT ...

  8. 【ABAP系列】SAP ABAP-模块 字符串操作基础知识

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP-模块 字符串操 ...

  9. 跨域资源共享(CORS)-漏洞整理

    绕过方法整理 绕过 - 仅对域名校验 #POC #"Access-Control-Allow-Origin: https://xx.co & Access-Control-Allow ...

  10. CSAPP:局部性原理

    一个编写良好的计算机程序常常具有良好的局部性(locality).局部性通常有两种不同的形式:时间局部性(temporal locality)和空间局部性(spatial locality).在一个具 ...