thymeleaf的常见用法

1,th:属性名="",就能够直接修改控件的属性,好比dom

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

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

<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="*{属性}"能够直接用属性名调用对象的值,好比blog

<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的任意属性提出来get

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

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

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

<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里的属性。class

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

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

相关文章
相关标签/搜索