1、依赖包添加
另外,需要在html文件中需添加html标签:
在application.yml中添加:
官网文档http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html。
2、表达式用法
2.1 简单的表达式:
-
变量表达式:
${...}
;
其中,“13 february 2011”只是对today
变量的说明或例子,不管today
的值是否为空,today
都不会被其替换掉。若要使用默认表达式,可用:
-
选择变量表达式:
*{...}
;
等价于:
-
消息变量表达式:
#{...}
;1.消息变量表达式同OGNL表达方式。
2.#表达基本对象
-- #ctx:上下文对象。
-- #vars: 上下文变量。
-- #locale:上下文的语言环境。
-- #request(仅在web上下文)的HttpServletRequest对象。
-- #response(仅在web上下文)的HttpServletResponse对象。
-- #session(仅在web上下文)的HttpSession对象。
-- #servletContext(仅在web上下文)的ServletContext对象。
注意:请求参数param、session属性、以及application全局属性在引用的时候,可以省略#
符号。
3.#引用工具类的方法
-
链接URL表达式:
@{...}
;
相对于页面的url:user/login.html
相对于上下文的url:/itemdetails?id=3
相对于服务器的url: ~/billing/processInvoic
相对于协议类的url://code.jquery.com/jquery-2.0.3.min.js
含有请求参数的url:@{/order/{orderId}/details(orderId=${orderId})}
-
片段表达式:
~{...}
。
片段表达式来表示标记片段和移动它们周围模板的简便方法。这使我们能够复制它们,将它们传递到其他模板作为参数,等等。常见的用途使用是片段的插入th:insert(在后面的章节有片段定义及引用的例子):
2.2文本表达式
文字:'one text','Anotherone!',... ;
数字:0,34,3.0,12.3,...;
布尔文字:true,false;
null文本: null;
文字token:one,sometext,main,...;
文字token指的是由字母(A-Z和a-z),数字(0-9),方括号([ ]),点(.),连字符(-)和下划线(_)等组成的字符串,并且没有空格,没有标点符号。
2.3 文本操作:
字符串连接: +
文字替换:|The name is ${name}|
等价于:
2.4 算术运算:
二元运算符:+,-,*,/,%
取负(一元运算符):-;
2.5 布尔操作:
二元运算符:and,or;
布尔否定(一元运算符):!,not;
2.6 比较和相等:
比较:>,<,>=,<=(gt,lt,ge,le);
等价于:
也等价于:
相等运算符:==,!=(eq,ne);
2.7 条件表达式:
如果- 则:( if) ? (then )
如果- 则 - 否则:( if) ? (then) : (else ) 默认: ( value) ?:(defaultvalue )
2.8 特殊符号:无操作:_ 。
所有这些运算可以被组合并嵌套。
3、迭代用法 th:each
4、分支用法th:case
5、条件用法th:if
6、片段表达式用法
有些组件如页脚、标题等组件经常在多个页面中使用,为了能够代码复用,我们可以在/WEB-INF/templates
目录下定义一个模板,以供其他地方引用。下面举个页脚的例子:文件目录为/WEB-INF/templates/footer.html,具体代码如下。
这就定义了一个名为copy的代码片段。可以参考下面方式引用它。
如果想使代码片段的参数可定制化,可以参考以下方式。