thymeleaf
-
依赖:直接可以模板导入
-
名称空间:xmlns:th=“http://www.thymeleaf.org”
-
标准表达式语法:
${...}
变量表达式,Variable Expressions@{...}
链接表达式,Link URL Expressions#{...}
消息表达式,Message Expressions- 消息表达式一般用于国际化的场景。
~{...}
代码块表达式,Fragment Expressions- th:insert:将代码块片段整个插入到使用了th:insert的HTML标签中
- th:replace:将代码块片段整个替换使用了th:replace的HTML标签中
- th:include:将代码块片段包含的内容插入到使用了th:include的HTML标签中
*{...}
选择变量表达式,Selection Variable Expressions
-
基础用法:
-
th:text:文本替换
-
th:utext:支持html的文本替换
-
th:value:属性赋值
-
th:each:遍历循环元
-
<div th:each="message : ${thEach}"> <p th:text="${message}" /> </div> <!-- 遍历整个div-p,不推荐-->
-
<div> <p th:text="${message}" th:each="message : ${thEach}" /> </div> <!--只遍历p,推荐使用-->
-
-
th:if:判断条件,类似的还有th:unless,th:switch,th:case
-
<!--th:if 条件判断,类似的有th:switch,th:case,优先级仅次于th:each, 其中#strings是变量表达式的内置方法--> <p th:text="${thIf}"th:if="${not #strings.isEmpty(thIf)}"></p>
-
-
th:insert:代码块引入,类似的还有th:replace,th:include,常用于公共代码块提取的场景
-
<!--th:insert 把代码块插入当前div中,优先级最高,类似的有th:replace,th:include,~{} :代码块表达式 --> <div th:insert="~{grammar/common::thCommon}"></div>
-
-
th:fragment:定义代码块,方便被th:insert引用
-
th:object:声明变量,一般和*{}一起配合使用,达到偷懒的效果。
-
<!--th:object 声明变量,和*{} 一起使用--> <div th:object="${thObject}"> <p>ID: <span th:text="*{id}" /></p><!--th:text="${thObject.id}"--> <p>TH: <span th:text="*{thName}" /></p><!--${thObject.thName}--> <p>DE: <span th:text="*{desc}" /></p><!--${thObject.desc}--> </div>
-
th:attr:设置标签属性,多个属性可以用逗号分隔
-
-