Thymeleaf是一种Java XML / XHTML / HTML5模板引擎,能够在Web和非Web环境中使用。它更适合在基于MVC的Web应用程序的视图层提供XHTML / HTML5,但即便在脱机环境中,它也能够处理任何XML文件。它提供了完整的Spring Framework集成。 php
关于Spring推荐Thymeleaf的这种说法,我在Spring官方文档并无看到具体的说明,只是在和JSP比较的时候,说了JSP和Thymeleaf对比JSP的一些不足,而Thymeleaf只是做为其余模板引擎的一种表明。html
做为一款优秀的模板引擎,除了易用性、活跃的社区、健康快速的发展外,还有很是重要的一点就是性能了,那Thymeleaf 3 和 FreeMaker 的性能对比是怎么样的,后续文章会陆续更新。java
<!--more-->node
Thymeleaf的使用是由两部分组成的:标签 + 表达式,标签是Thymeleaf的语法结构,而表达式就是语法里的内容实现。git
经过标签 + 表达式,让数据和模板结合,最终转换成html代码,返回给用户。github
Thymeleaf基础使用分为三部分:golang
HTML代码:web
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>王磊的博客</title> </head> <body> <span th:text="${name}"></span> </body> </html>
Java代码:spring
@RequestMapping("/") public ModelAndView index() { ModelAndView modelAndView = new ModelAndView("/index"); modelAndView.addObject("name", "老王"); return modelAndView; }
最终效果: 老王
express
使用"th:text"是对内容的原样输出,使用“th:utext”能够进行html标签输出。
Java代码:
@RequestMapping("/eat") public ModelAndView eat() { ModelAndView modelAndView = new ModelAndView("/cat"); modelAndView.addObject("data", "<span style='color:red'>老王是吃货</span>"); return modelAndView; }
HTML代码:
<h4 th:text="'th:text '+${data}"></h4> <h4 th:utext="'th:utext '+${data}"></h4>
展现效果:
<span th:if="${age > 18}"> 成年 </span> <span th:unless="${age > 18}"> 未成年 </span>
th:if为知足条件的业务处理,th:unless正好相反,是除去的意思。
<div th:switch="${age}"> <span th:case="18">18岁</span> <span th:case="19">19岁</span> <spa th:case="*">其余</spa> </div>
注意: 默认选项使用th:case="*"
指定。
HTML代码:
<div th:each="name,item:${names}"> <span th:text="${item.count}"></span> <span th:text="${name}"></span> </div>
Java代码:
@RequestMapping("/") public ModelAndView index() { ArrayList<String> names = new ArrayList<>(); names.add("java"); names.add("golang"); names.add("nodejs"); ModelAndView modelAndView = new ModelAndView("/index"); modelAndView.addObject("names",names); return modelAndView; }
访问效果以下:
其中item为每行的详细值,key值以下:
footer.html页面代码:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>王磊的博客</title> </head> <body> <div th:fragment="copyright"> © 著做权归 老王 全部 </div> <div th:fragment="about"> 关于 </div> <div th:fragment="links"> CCTV </div> </body> </html>
声明了两个代码片断,copyright和about。
cat.html页面代码:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>王磊的博客</title> </head> <body> <div th:replace="footer :: copyright"></div> <div th:insert="footer :: about"></div> <div th:include="footer :: links"></div> </body> </html>
其中第一个div引用了footer.html 的 copyright 代码片断,第二个div引用了 footer.html 的 about 代码片断。
双冒号的理解: 其中使用“::”双冒号来完成对页面片断的引用,有点像php里面的语法,使用双冒号来表示对类的静态属性和方法进行直接引用。
执行效果以下图:
总结: 能够很清晰的看出th:insert、th:replace、th:include之间的区别,在因而否保留本身的主标签,th:include 在3.0以后已经不推荐使用了,可使用th:replace标签替代。
使用fragment咱们是能够在html代码中传参的,好比咱们定义了一个top.html其中有一个“欢迎XXX”的提示,而这我的名XXX就是须要动态传递的,这样咱们能够最大程度的完成代码的复用,这个时候就是一个很好的使用场景,咱们须要这样作。
页面main.html代码:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" > <head> <meta charset="UTF-8"> <title>王磊的博客</title> </head> <body> <div th:replace="footer :: webcome('老王')"></div> </body> </html>
页面top.html
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" > <head> <meta charset="UTF-8"> <title>王磊的博客</title> </head> <body> <div th:fragment="webcome(about)"> <span th:text="'欢迎:'+${about}"></span> </div> </body> </html>
最终的效果:
页面代码:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" > <head> <meta charset="UTF-8"> <title>王磊的博客</title> </head> <body> <div th:with="sum=4-2"> <span th:text="${sum}"></span> </div> </body> </html>
页面输出结果:2
th:remove用于html代码的删除,th:remove值有五个:
示例index.html代码以下:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>王磊的博客</title> </head> <body> <div id="all" th:remove="all"> <span>all</span> <span>1</span> </div> <div id="body" th:remove="body"> <span>body</span> <span>2</span> </div> <div id="tag" th:remove="tag"> <span>tag</span> <span>3</span> </div> <div id="all-but-first" th:remove="all-but-first"> <span>all-but-first</span> <span>4</span> </div> <div id="none" th:remove="none"> <span>none</span> <span>5</span> </div> </body> </html>
最终展现效果以下:
<div th:style="'color:'+${skinColor}">
<input type="button" value=" Click " th:onclick="'onsub()'">
<a th:href="${myhref}"></a>
<input th:value="${user.name}" />
<img th:src="${img}" />
<form th:action="@{/suburl}">
<form id="${fromid}">
<img th:attr="src=@{/img/stone.jpg},alt=${alt}" />
<div th:object="${user}">
变量表达式:${...}
选择变量表达式:*{...}
消息表达式:#{...}
连接表达式:@{...}
片断表达:~{...}
文字:'one text', 'Another one!',…
数字文字:0, 34, 3.0, 12.3,…
布尔文字:true, false
NULL文字:null
文字标记:one, sometext, main,…
字符串拼接:+
字面替换:|The name is ${name}|
二进制运算符:+, -, *, /, %
减号(一元运算符):-
二进制运算符:and, or
布尔否认(一元运算符):!, false
比较值:>, <, >=, <=
相等判断: ==, !=
若是-而后:(if) ? (then)
若是-而后-不然:(if) ? (then) : (else)
违约:(value) ?: (defaultvalue)
全部以上这些表达式均可以组合和嵌套,例如:
'User is of type ' + (${user.isAdmin()} ? 'Administrator' : (${user.type} ?: 'Unknown'))
变量表达式的使用,咱们前面的代码已经见到了,$是咱们日常开发中最经常使用的表达式,用于把后台Java类的动态数据,映射到页面,例如:
Java代码:
public ModelAndView index() { ModelAndView modelAndView = new ModelAndView("/cat"); modelAndView.addObject("data", "我是老王"); return modelAndView; }
HTML代码:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>王磊的博客</title> </head> <body> <span th:text="${data}"></span> </body> </html>
最终效果:
选择表达式至关于选择了一个对象,在使用的时候不在须要这个对象的前缀,直接使用属性的key进行内容展现,代码以下:
<div th:object="${goods}"> <span th:text="${goods.name}"></span> <span th:text="*{price}"></span> <span th:text="${#dates.format(goods.createTime, 'yyyy-MM-dd HH:mm:ss')}"></span> </div>
最终效果:
iMac 7999.0 2018-08-10 14:03:51
总结: *{price} = ${goods.price}只是省去了“goods.”前缀,效果都是同样的。
用于转换url,代码以下:
<a th:href="@{/footer(id=666,name=laowang)}">连接</a>
最终呈现的效果:
<a href="/footer?id=666&name=laowang">连接</a>
连接表达式,能够传递参数,用逗号分隔。
服务器根相对路径:@{~/path/to/something}
文本操做分为两个:文本拼加、文本替换
文本拼加:
<span th:text="'我叫'+${name}"></span>
文本替换:
文本替换的语法:|内容${tag}|
<span th:text="|我叫${name},是一名开发工程师。|"></span>
<tr th:class="${row.even}? 'even' : 'odd'">
<p th:text="${val}">...</p> <p th:text="${{val}}">...</p>
结果:
<p>1234567890</p> <p>1,234,567,890</p>
虽然标准的标签几乎能够知足全部的业务场景,但某些状况咱们更喜欢直接写入HTML文本,例如:
<p>Hello, [[${name}]]</p>
嵌入文本有两种写法“[[...]]”和“[(...)]”,分别的做用就像th:text 和 th:utext 同样,例如:
<p> [[${name}]] </p> <p> [(${name})] </p>
看到的效果是这样的:
表达式里面的对象能够帮助咱们处理要展现的内容,好比表达式的工具类dates能够格式化时间,这些内置类的熟练使用,可让咱们使用Thymeleaf的效率提升不少。
#ctx
: 操做当前上下文.#vars:
操做上下文变量.#request
: (仅适用于Web项目) HttpServletRequest
对象.#response
: (仅适用于Web项目) HttpServletResponse
对象.#session
: (仅适用于Web项目) HttpSession
对象.#servletContext
: (仅适用于Web项目) ServletContext
对象.#execInfo
: 操做模板的工具类,包含了一些模板信息,好比:${#execInfo.templateName}
.#uris
: url处理的工具#conversions
: methods for executing the configured conversion service (if any).#dates
: 方法来源于 java.util.Date
对象,用于处理时间,好比:格式化.#calendars
: 相似于 #dates
, 可是来自于 java.util.Calendar
对象.#numbers
: 用于格式化数字.#strings
: methods for String
objects: contains, startsWith, prepending/appending, etc.#objects
: 普通的object对象方法.#bools
: 判断bool类型的工具.#arrays
: 数组操做工具.#lists
: 列表操做数据.#sets
: Set操做工具.#maps
: Map操做工具.#aggregates
: 操做数组或集合的工具.每一个类中的具体方法,点击查看:https://www.thymeleaf.org/doc...
先上效果图:
IDEA默认是开启了Thymeleaf 插件支持的,若是不放心须要验证,请访问:https://www.jetbrains.com/hel...
但仅仅是配置上面的效果,依然是没法正常使用的,缘由是你要在html中声明 Thymeleaf 命名空间 xmlns:th="http://www.thymeleaf.org"
,完整代码以下:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h2 th:text="${hi}"></h2> </body> </html>
其中关键的代码是:
xmlns:th="http://www.thymeleaf.org"
这样当你在代码输入“th:”的时候就会看到 Thymeleaf 的全部标签了。
在正式集成Thymeleaf引擎以前,先来看下目录结构如图:
除去包名,咱们来解释一下这些目录表明的含义:
接下来咱们具体分别来看具体的步骤。
<!--thymeleaf模板--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
# 启用缓存:建议生产开启 spring.thymeleaf.cache=false # 建议模版是否存在 spring.thymeleaf.check-template-location=true # Content-Type 值 spring.thymeleaf.servlet.content-type=text/html # 是否启用 spring.thymeleaf.enabled=true # 模版编码 spring.thymeleaf.encoding=utf-8 # 应该从解析中排除的视图名称列表(用逗号分隔) spring.thymeleaf.excluded-view-names= # 模版模式 spring.thymeleaf.mode=HTML5 # 模版存放路径 spring.thymeleaf.prefix=classpath:/templates/ # 模版后缀 spring.thymeleaf.suffix=.html
配置项 | 类型 | 默认值 | 建议值 | 说明 |
---|---|---|---|---|
spring.thymeleaf.enabled | bool | true | 默认 | 是否启用 |
spring.thymeleaf.mode | String | HTML | 默认 | 模板类型,能够设置为HTML5 |
spring.thymeleaf.cache | bool | true | 默认 | 是否启用缓存,生成环境建议设置为true |
spring.thymeleaf.prefix | String | classpath:/templates/ | 默认 | 模版存放路径 |
spring.thymeleaf.suffix | String | .html | 默认 | 模版后缀 |
spring.thymeleaf.servlet.content-type | String | text/html | 默认 | Content-Type 值 |
spring.thymeleaf.encoding | String | - | utf-8 | 模版编码 |
咱们在controller文件夹建立index.java,代码以下:
package com.hello.springboot.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/") public class Index { @RequestMapping("/") public ModelAndView index() { ModelAndView modelAndView = new ModelAndView("/index"); modelAndView.addObject("name", "王磊的博客"); return modelAndView; } }
关键代码解读:
咱们在resources/templates下建立index.html,代码以下:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>王磊的博客</title> </head> <body> <span th:text="${name}"></span> </body> </html>
启动调试,在浏览器输入:http://localhost:8080/
效果以下:
相关代码GitHub:https://github.com/vipstone/s...
thymeleaf官方文档 Thymeleaf :https://www.thymeleaf.org/doc...
thymeleaf官方文档 Spring + Thymeleaf :https://www.thymeleaf.org/doc...