Thymeleaf是一个现代的服务器端Java模板引擎,适用于Web和独立环境,能够处理HTML,XML,JavaScript,CSS甚至纯文本。
官方文档:Tutorial: Using Thymeleaf
中文文档:10.属性优先权 · Using Thymeleaf 译文
内置工具对象文档:Tutorial: Using Thymeleaf
基础用法:
${x} will return a variable x stored into the Thymeleaf context or as a request attribute
${param.x} will return a request parameter called x (which might be multivalued).
${session.x} will return a session attribute called x.
${application.x} will return a servlet context attribute called x
基本的表达式对象:
#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).
控制块
1 | 引入片段 | th:insert th:replace |
2 | 迭代片段 | th:each |
3 | 条件评估 | th:if th:unless th:switch th:case |
4 | 局部变量定义 | th:object th:with |
5 | 一般属性修改 | th:attr th:attrprepend th:attrappend |
6 | 特殊属性修改 | th:value th:href th:src ... |
7 | 文本(标签体修改) | th:text th:utext |
8 | 片段规格 | th:fragment |
9 | 片段删除 | th:remove |