thymeleaf的常见用法

1,th:属性名="",就可以直接修改控件的属性,比如

    <input th:type="button" th:name="xinxin" th:value="caddice"/>等等...

2,th:each="xx,xxStat:${后台传来的值}",th:text="${xx.属性}"可以把传来的集合遍历出来,比如

<table>
    <tr>
        <td colspan="2">人员信息列表</td>
    </tr>
    <tr th:each="person,personStat:${persons}">
        <td th:text="${person.name}"></td>
        <td th:text="${person.age}"></td>
    </tr>
</table>

    这样就能看到一个列表了。personStat里面装的是当前person的一些状态信息,比如角标之类的。不需要的话可以直接写成<tr th:each="person:${persons}">

3,th:object="${对象}",th:field="*{属性}"可以直接用属性名调用对象的值,比如

<table th:object="${person}">
    <tr>
        <td colspan="2">个人信息</td>
    </tr>
    <tr>
        <td>姓名:</td>
        <td th:field="*{name}"></td>
    </tr>
    <tr>
        <td>年龄:</td>
        <td th:field="*{age}"></td>
    </tr>
</table>

    这样可以把person的任意属性提出来

*注:要灵活运用,尤其是$与*符号:如果是从后台传过来的,要用$;如果是th:object这种对象里的,则用*

4,th:checked="${}",这玩意在选择框里用,大括号里写int的时候,会把对应的选中。也可以写条件,满足的时候选中

    下面是我项目中的一个用法,是修改信息时候的页面。因为级联关系,所以不像主表里的属性在页面显示出来的时候直接就能选中,所以我在里面加了一个判断

<tr>
  <td>学位:</td>
  <td><input type="radio" th:name="${educationStat.current}" th:checked="${education.degree.name()==degree.name()}" th:each="degree:${degree}" th:text="${degree.getDefaultName()}" th:value="${degree.name()}"/></td>
</tr>

    为了保持每条教育经历中学位的name都不能一样,所以我用了Stat里的属性。

    th:checked中判断,当数据和枚举中的值相同时,选中

5,th:if这个就是纯判断了,我只是试过一下,就不举例子了。如果条件是false的话,整个dom中thymeleaf的用法就都不执行了

原文地址:https://www.cnblogs.com/brook1223/p/5067365.html