Spring Boot/View/Thymeleaf/テンプレート/table を出力する

Spring Boot/View/Thymeleaf/テンプレート/table を出力する

単に出力するだけなのだが注意する点がある。

おそらくこのような構成のタグを書くとは思うのだが、これは正常に動作しない

<table>
    <tr>
        <th>id</th>
        <th>name</th>
        <th>age</th>
    </tr>
    <th:block th:each="hoge : ${hogeList}" >
        <tr>
            <td>[[${hoge.id}]]</td>
            <td>[[${hoge.name}]]</td>
            <td>[[${hoge.age}]]</td>
        </tr>
    </th:block>
</table>

この原因は Thymeleaf が単に字面上のテキスト操作をしているわけでなく、一旦 HTML を解釈しているというところにある。 その解釈後の HTML によっては記述がうまく働かないということがおきる。

↑を正常に動作させるにはこのように記述する必要がある

<table>
    <thead>
        <tr>
            <th>id</th>
            <th>name</th>
            <th>age</th>
        </tr>
    </thead>
    <tbody>
        <th:block th:each="hoge : ${hogeList}" >
            <tr>
                <td>[[${hoge.id}]]</td>
                <td>[[${hoge.name}]]</td>
                <td>[[${hoge.age}]]</td>
            </tr>
        </th:block>
    </tbody>
</table>

Thymeleaf の記述とはまったく関係ないのだが、thead tbody タグを補うと正常に動作する。

java/spring/spring_boot/view/thymeleaf/template/rendering_table.txt · 最終更新: 2018-12-10 11:48 by ore