ThymeLeaf snippet runs on false th: if

I use Thymeleaf bundled with Spring-Boot. Here is the main template:

<div class="container">
    <table th:replace="fragments/resultTable" th:if="${results}">
        <tr>
            <th>Talent</th>
            <th>Score</th>
        </tr>
        <tr>
            <td>Confidence</td>
            <td>1.0</td>
        </tr>
    </table>
</div>

And he uses this snippet:

<table th:fragment="resultTable">
    <tr>
        <th>Talent</th>
        <th>Score</th>
    </tr>
    <tr th:each="talent : ${talents}">
        <td th:text="${talent}">Talent</td>
        <td th:text="${results.getScore(talent)}">1.0</td>
    </tr>
</table>

A fragment works only if there is a result object. That makes sense to me. Therefore, based on the syntax from the documentation, I added an operator th:ifto the main template file. However, I still get this error when I access the template without an object

Attempted to call method getScore(com.model.Talent) on null context object

Should the th:ifoperator not th:ifinterfere with access to this code?

The template still works fine when the result object is full, but how do I get a zero case for rendering without a table?

+6
2

, th: if.

http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#attribute-precedence

, , th: if . div, , div, th th: block, :

<div class="container">
    <th:block th:if="${results}">
        <table th:replace="fragments/resultTable">
            <tr>
                <th>Talent</th>
                <th>Score</th>
            </tr>
            <tr>
                <td>Confidence</td>
                <td>1.0</td>
            </tr>
        </table>
    </th:block>
</div>
+8

Thymeleaf 3.0 /, , - :

<table th:replace="${results} ? ~{fragments :: resultTable} : _">

https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#advanced-conditional-insertion-of-fragments

0

Source: https://habr.com/ru/post/1663674/


All Articles