通常读取保存在外部配置文件的key-value键值对,保存在配置文件中的内容称为message
一般配置文件和模板同名。
使用#{key}的形式读取内容。
读取session中内容使用 ${session.key}
这种方式使用${…}方式读取,相当于
ctx.getVariable(“ today” ) //WebContext 数据来自ServletContext
读取session中的登录用户user
${session.user.name}相当于
((User) ctx.getVariable(“ session” ).get(“ user” )).getName();
可以使用多种方式:
${person.father.name}
${person[‘father’] [‘name’] }
${personsByName[‘Stephen Zucchini’] .age}
可以使用数组的索引
${personsArray[0] .name}
可以调用方法,并且可以传递参数
${person.createCompleteName()}
${person.createCompleteNameWithSeparator(‘- ‘)}
thymeleaf能识别ognl表达式,这样使语法更加灵活,使用 #obj 例如:
#ctx : the context object. 上下文对象
#vars: the context variables.上下文变量
#locale : the context locale.本地化
下面几种只能在Web Contexts中进行引用:
#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.
这样使用:
Established locale country: ${#locale.country}” >US.
除了基本对象,thymeleaf还提供了工具类对象,帮助我们提升性能
#execInfo : information about the template being processed.\
#messages : methods for obtaining externalized messages inside variables expressions, in the same way as they 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)
日期格式化:
Today is: ${#calendars.format(today, ‘dd MMMM yyyy’)}” >13 May 2011
区域上的表达式使用 *{…}
${}和*{}最大的区别,是${}是整个context的,而*{}是某个区域的,当*{}没有使用在某个区域上的时候,$和*是一样的。
什么是当前选择的Object呢?也就是使用th:object 的某个标签范围,就是这个object的作用范围。例如:
${session.user}” >Name: Sebastian.
Surname: Pepper.
Nationality: Saturn.
相当于下面的内容:
Name: Sebastian.
Surname: Pepper.
Nationality: Saturn.
当然,$和* 可以混合使用
${session.user}” >Name: Sebastian.
Surname: ${session.user.lastName}” >Pepper.
Nationality: Saturn.
在恰当的位置,当前区域的object对象,同时也可以使用 #object 来代替。
${session.user}” >Name: ${#object.firstName}” >Sebastian.
Surname: ${session.user.lastName}” >Pepper.
Nationality: Saturn.
语法:@{…} 新语法 th:href 使用的时候会把里面的值自动设置到 标签里面
处理类:org.thymeleaf.linkbuilder.ILinkBuilder
相对URL可以是下面的:
Page-relative: user/login.html 相对于页面
Context-relative: /itemdetails?id=3 (context name in server will be added automatically) 相对于context,也就是context name
Server-relative: \~/billing/processInvoice (allows calling URLs in another context (= application) in the same server. 相对于服务器
Protocol-relative URLs: //code.jquery.com/jquery-2.0.3.min.js 协议相对url
会自动进行参数处理: /details(orderId=${o.id}) 回转换成 /detail?orderId=
多个参数使用逗号隔开:
@{/order/process(execId=${execId}, execType=‘FAST’)}
以/开头的相对地址,会自动加上context name
语法:th:insert th:replace
可以是任意字符,使用单引号,可以使用\进行字符转义
template file
1492
同时可以进行运算
两个值: true false
${user.isAdmin()} == false” >null类型
${variable.something} == null” >符号类型
符号类型,不需要任何的引号,直接放到双引号里面
…追加字符串
直接使用 +
${user.name}” >
字符替换
直接把字符放到两个 || 里面。
${user.name}! | “ >
和下面的等价
${user.name} + ‘! ‘“ >
注意:只有变量表达式${}允许放在||里面
数学运算
- * / % 还有一些别名 div(/) mod(%)
比较运算符
> , < , >= and <= , == and ! = 其中大于小于可以使用< >
别名:
gt ( > ), lt ( < ), ge ( >= ), le( <= ), not ( ! ).Also eq ( == ), neq / ne ( ! = ).
条件表达式
${row.even}? ‘even’: ‘odd’” > …
如果返回空值,可以省略
${row.even}? ‘alt’” > …
如果有默认值:
27 如果不为null,值为自身*{age}
无操作 _
${user.name} ?: _” >no user authenticated
日期格式化
预处理表达式
像这样:__${expression}__