企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 前言 修改application,这样在访问页面时需要加上该前缀. 访问路径变成 [http://127.0.0.1:8899/kimgao/template/thymeleaf](http://127.0.0.1:8899/kimgao/template/thymeleaf) ~~~ server: port: 8899 # web应用服务端口 servlet: context-path: /kimgao ~~~ ![](https://img.kancloud.cn/7d/6b/7d6bf7d0668743c06e649f4ad80e5646_618x252.png) 修改一下TemplateController,添加user信息 ~~~ Map<String,String> user = new HashMap<>(); user.put("id","1"); user.put("username","kim"); user.put("password","123456"); model.addAttribute("user",user); ~~~ ![](https://img.kancloud.cn/e3/fc/e3fc7ae2127c2262fd9d9e2bdcae5488_980x819.png) ## 一、基础语法 ### 变量表达式`${}` 使用方法:直接使用`th:xx = "${}"`获取对象属性 。例如: ~~~ <form id="userForm"> <input id="id" name="id" th:value="${user.id}"/> <input id="username" name="username" th:value="${user.username}"/> <input id="password" name="password" th:value="${user.password}"/> </form> <div th:text="hello"></div> <div th:text="${articles[0].author}"></div> ~~~ ### 选择变量表达式`*{}` 使用方法:首先通过`th:object`获取对象,然后使用`th:xx = "*{}"`获取对象属性。(可读性较差) ~~~ <form id="userForm" th:object="${user}"> <input id="id" name="id" th:value="*{id}"/> <input id="username" name="username" th:value="*{username}"/> <input id="password" name="password" th:value="*{password}"/> </form> ~~~ ### 链接表达式`@{}` 使用方法:通过链接表达式`@{}`直接拿到应用路径,然后拼接静态资源路径。例如: ~~~ <script th:src="@{/webjars/jquery/jquery.js}"></script> <link th:href="@{/webjars/bootstrap/css/bootstrap.css}" rel="stylesheet" type="text/css"> ~~~ 这样无论application如何修改都不需要修改html ![](https://img.kancloud.cn/d2/06/d2061f8026bb6eb2dbb296c4b8cb9a6c_597x136.png) ![](https://img.kancloud.cn/8b/4f/8b4fca131fb312c52b499234a3b59866_575x187.png) ### 其它表达式 在基础语法中,默认支持字符串连接、数学运算、布尔逻辑和三目运算等。例如: ~~~ <input name="name" th:value="${'I am '+(user.name!=null?user.name:'NoBody')}"/> ~~~ ## 二、迭代循环 想要遍历`List`集合很简单,配合`th:each`即可快速完成迭代。例如遍历用户列表: ~~~ <div th:each="article:${articles}" > 作者:<input th:value="${article.author}"/> 标题:<input th:value="${article.title}"/> 内容:<input th:value="${article.content}"/> </div> ~~~ 在集合的迭代过程还可以获取状态变量,只需在变量后面指定状态变量名即可,状态变量可用于获取集合的下标/序号、总数、是否为单数/偶数行、是否为第一个/最后一个。例如: ~~~ <div th:each="article,stat:${articles}" th:class="${stat.even}?'even':'odd'"> 下标:<input th:value="${stat.index}"/> 序号:<input th:value="${stat.count}"/> 作者:<input th:value="${article.author}"/> 标题:<input th:value="${article.title}"/> 内容:<input th:value="${article.content}"/> </div> ~~~ ~~~ <table class=""> <tr> <td>下标</td> <td>序号</td> <td>作者</td> <td>教程名称</td> <td>内容</td> </tr> <!-- 用thymeleaf语法遍历articles列表--> <tr th:each="item,stat : ${articles}" th:style="${stat.even}?'background-color:blue':'background-color:red'"> <td th:text="${stat.index}"></td> <td th:text="${stat.count}"></td> <td th:text="${item.author}"></td> <td th:text="${item.title}"></td> <td th:text="${item.content}"></td> </tr> </table> ~~~ 可实现表格隔行变色 ![](https://img.kancloud.cn/bf/ac/bfacbc6568ff9859bb997b173816ea96_798x142.png) **二.迭代下标变量用法:** 状态变量定义在一个th:每个属性和包含以下数据: 1. 当前迭代索引,从0开始。这是索引属性。index 2. 当前迭代索引,从1开始。这是统计属性。count 3. 元素的总量迭代变量。这是大小属性。 size 4. iter变量为每个迭代。这是目前的财产。 current 5. 是否当前迭代是奇数还是偶数。这些even/odd的布尔属性。 6. 是否第一个当前迭代。这是first布尔属性。 7. 是否最后一个当前迭代。这是last布尔属性。 ## 三、条件判断 条件判断通常用于动态页面的初始化,例如: ~~~ <div th:if="${articles}"> <div>的确存在..</div> </div> ~~~ 如果想取反则使用unless 例如: ~~~ <div th:unless="${articles}"> <div>不存在..</div> </div> ~~~ 举例:设置stat.index等于0的时候显示 ![](https://img.kancloud.cn/6e/9c/6e9c58483d2b885c7796b615b75ce61c_1063x405.png) 可以看到在第一行的时候显示了,第二行没有显示。 ![](https://img.kancloud.cn/71/36/7136092dd914e16bf6542addb937019d_988x131.png)