Thymeleaf基础
一.标准表达式语法
Thymeleaf属性允许将它们的值设置为或包含表达式,由于它们使用的方言,我们将其称为标准表达式。这些表达式可以有五种类型:
-
${...}
: 变量表达式。 -
*{...}
: 选择表达式。 -
#{...}
: 消息 (i18n) 表达式。 -
@{...}
: 链接 (URL) 表达式。 -
~{...}
: 片段表达式。1.变量表达式
前端
<span th:text="${username}"></span> <span th:text="${user.name}"></span>
后端
@RequestMapping("/demo1") public String to_demo1(Model m) { m.addAttribute("username","user1"); m.addAttribute("user",new Account("KangKang", "康康", "e10adc3949ba59abbe56e", "超级管理员", "17777777777")); return "demo1"; }
2.选择表达式
前端
<div th:object="${user}"> <span th:text="*{account}">...</span> <span th:text="*{accountType}">...</span> </div>
后端
@RequestMapping("/demo1") public String to_demo1(Model m) { m.addAttribute("user",new Account("KangKang", "康康", "e10adc3949ba59abbe56e", "超级管理员", "17777777777")); return "demo1"; }
3.链接表达式
前端
<a th:href="@{/demo1}">...</a>