为什么要评估这个Thymeleaf块?

时间:2015-04-23 17:08:04

标签: spring-security thymeleaf

我有以下的Thymeleaf块。当thedoc.addendum为空时,我看不到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为空时,如何阻止显示复选框?

1 个答案:

答案 0 :(得分:1)

您确定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>

但它只是一种解决方法。如果这项工作你应该为此创建一张票。