为什么要对 Thymeleaf 块进行评估?

Why does this Thymeleaf block get evaluated?

我有以下 Thymeleaf 块。当 thedoc.addendum 为 null 时,我没有看到 thedoc.addendemTitlethedoc.addendum(正确),但 span:sec 块中的复选框得到评估。

<div th:if="${thedoc.addendum != null}">
<dt th:text="${thedoc.addendumTitle}">ADDENDUM</dt>
<dd>
    <div class="col-xs-10">
    <span th:text="${thedoc.addendum}">Custom addendum here</span>
    </div>
    <span sec:authorize="hasAnyRole('ROLE_A','ROLE_B','ROLE_C','ROLE_D')">
    <!-- THIS GETS EVALUATED --->
    <div class="col-xs-2">
        <div class="checkbox">
        <label>
            <input type="checkbox" name="ackAddendum" id="ackAddendum"/>
            <i class="fa fa-square-o"></i>
            <span class="text-success">I agree</span>
        </label>
        </div>
    </div>
    <!-- /END EVALUATED -->
    </span>
</dd>
</div>

我还尝试向 <span:sec> 标记添加非空检查,并向 <div class="checkbox"> 标记添加非空检查,但它仍然评估复选框。

如果 sec:authorize 标记优先于外部 th:if,当 thedoc.addendum 为 null 时,如何防止显示复选框?

你确定thedoc.addendumTitle不是空的吗?您可以添加 <div th:text="${thedoc.addendum}"></div><div th:text="${thedoc.addendumTitle}"></div> 并查看输出结果吗?我想 thedoc.addendum 永远不会为空,它只是一个空字符串。所以你应该使用 #strings.isEmpty().

否则你可以使用下面的

<div th:if="${thedoc.addendum != null}">
<dt th:text="${thedoc.addendumTitle}">ADDENDUM</dt>
<dd>
    <div class="col-xs-10">
    <span th:text="${thedoc.addendum}">Custom addendum here</span>
    </div>
    <span sec:authorize="hasAnyRole('ROLE_A','ROLE_B','ROLE_C','ROLE_D')">
    <!-- THIS GETS EVALUATED --->
    <th:block th:if="${thedoc.addendum != null}">
    <div class="col-xs-2">
        <div class="checkbox">
        <label>
            <input type="checkbox" name="ackAddendum" id="ackAddendum"/>
            <i class="fa fa-square-o"></i>
            <span class="text-success">I agree</span>
        </label>
        </div>
    </div>
    </th:block>
    <!-- /END EVALUATED -->
    </span>
</dd>
</div>

但这只是一种解决方法。如果这项工作有效,您应该为此创建一个票证。