1、引入thymeleaf;导入jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐thymeleaf</artifactId>
</dependency>
2.1.6
切换thymeleaf版本
<properties>
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<!‐‐ 布局功能的支持程序 thymeleaf3主程序 layout2以上版本 ‐‐>
<!‐‐ thymeleaf2 layout1‐‐>
<thymeleaf‐layout‐dialect.version>2.2.2</thymeleaf‐layout‐dialect.version>
</properties>
模板引擎封装的规则
只要我们把html放在templates下面,模板引擎就可以帮我们渲染
2、使用thymeleaf:掌握语法,html页面引入<html lang="en" xmlns:th="http://www.thymeleaf.org">可以有提示
1、语法规则:
th:insert 片段包含,引入片段的相当于jsp标签的c:include
th:replace
th:include
th:each 遍历
th:if 条件判断
th:unless
th:switch
th:case
th:object 变量声明
th:with
th:attr 任意属性修改,prepend前面添加,append后面追加
th:attrprepend
th:attrappend
th:value 指定属性的修改
th:href
th:src
...
th:text 获取文本,转义
th:utext 获取文本,不转义
th:fragment 声明片段
th:remove 移除
2、表达式:
${}
#{}
*{}
@{}
~{}
-------------------------------------------------------------------------------------------------------------
${}:底层即使ognl表达式
(字面量,对象或map,数组或集合)
(1) 获取对象的属性,调用方法
(2) 使用内置的基本对象
(3) 使用内置的一些工具对象
eg:比如:
${person.father.name}//对象的属性
${person['father']['name']}
//map取值
${countriesByCode.ES}
${personsByName['Stephen Zucchini'].age}
//集合取值
//调用方法
${person.createCompleteName()}
${person.createCompleteNameWithSeparator('-')}
内置的基本对象:比如${#session.foo},${#locale.country}
#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.
内置的工具对象:比如:
#execInfo : information about the template being processed.
#messages : methods for obtaining externalized messages inside variables
expressions, in the
same way as they would be obtained using #{…} syntax.
#uris : methods for escaping parts of URLs/URIs
#conversions : methods for executing the configured conversion service
(if any).
#dates : methods for java.util.Date objects: formatting, component
extraction, etc.
#calendars : analogous to #dates , but for java.util.Calendar objects.
#numbers : methods for formatting numeric objects.
#strings : methods for String objects: contains, startsWith,
prepending/appending, etc.
#objects : methods for objects in general.
#bools : methods for boolean evaluation.
#arrays : methods for arrays.
#lists : methods for lists.
#sets : methods for sets.
#maps : methods for maps.
#aggregates : methods for creating aggregates on arrays or collections.
#ids : methods for dealing with id attributes that might be repeated (for
example, as a
result of an iteration).
--------------------------------------------------------------------------------------------------------
*{}与${}功能类似,只是将获取的数据存在*里面,后面要获取的时候就可以用*{}获取存在*中的数据,补充:配合 th:object="${session.user}:
<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>
---------------------------------------------------------------------------------------------------------------------------
#{}获取国际化的内容
---------------------------------------------------------------------------------------------------------------------------
@{}定义url的
括号里面是参数,多个参数用逗号隔开
@{/order/process(execId=${execId},execType='FAST')}
---------------------------------------------------------------------------------------------------
~{...}:片段引用表达式
<div th:insert="~{commons :: main}">...</div>