文章目录[隐藏]
一、Thymeleaf语法
标签
在HTML页面上使用Thymeleaf标签,Thymeleaf 标签能够动态地替换掉静态内容,使页面动态展示。为了大家更直观的认识Thymeleaf,下面展示一个在HTML文件中嵌入了Thymeleaf的页面文件,示例代码如下:
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <link rel="stylesheet" type="text/css" media="all" href="../../css/gtvg.css" rel="external nofollow" th:href="@{/css/gtvg.css}" rel="external nofollow" /> <title>Title</title> </head> <body> <p th:text="${hello}">欢迎进入Thymeleaf的学习</p> </body> </html>
thymelef常用标签
标签 | 说明 |
th:insert | 布局标签,替换内容到引入的文件 |
th:replace | 页面片段包含(类似JSP中的include标签) |
th:each | 元素遍历(类似JSP中的c:forEach标签) |
th:if | 条件判断,如果为真 |
th:unless | 条件判断,如果为假 |
th:switch | 条件判断,进行选择性匹配 |
th:case | 条件判断,进行选择性匹配 |
th:value | 属性值修改,指定标签属性值 |
th:href | 用于设定链接地址 |
th:src | 用于设定链接地址 |
th:text | 用于指定标签显示的文本内容 |
标准表达式
说明 | 表达式语法 |
变量表达式 | ${…} |
选择变量表达式 | *{…} |
消息表达式 | #{…} |
链接URL表达式 | @{…} |
片段表达式 | ~{…} |