Spring Thymeleaf SPEL - 如果 null 无效

Spring Thymeleaf SPEL - if null not wroking

我有下面的代码,除了检查值是否为 null 之外,一切都运行良好。我知道返回的值为空,但它仍然不起作用。我试过在 {} 内外都设置 == null 但无济于事。

也许这与hibernate返回值的方式有关?当我打印出从数据库返回的对象时,它说 null。

<div th:each="timecardLast: ${timecardLast}">
<a th:href="@{/timecardin}">
        <div th:if="${timecardLast.status == null}" style="width: 100%" class="waves-effect card-panel green darken-1 z-depth-4">
            <div class="card-content center-align">
                <i class="medium material-icons white-text">timer</i>
                <h5 class="white-text">SIGN IN TO WORK</h5>
            </div>
        </div>
    </a>
        <a th:href="@{/timecardin}">
        <div th:if="${timecardLast.status} == 1" style="width: 100%" class="waves-effect card-panel green darken-1 z-depth-4">
            <div class="card-content center-align">
                <i class="medium material-icons white-text">timer</i>
                <h5 class="white-text">SIGN IN TO WORK</h5>
            </div>
        </div>
    </a>
            <a th:href="@{/timecradin}">
        <div th:if="${timecardLast.status} == 2" style="width: 100%" class="waves-effect card-panel deep-orange darken-2 z-depth-4">
            <div class="card-content center-align">
                <i class="medium material-icons white-text">timer</i>
                <h5 class="white-text">SIGN IN TO WORK</h5>
            </div>
        </div>
    </a>
            <a th:href="@{/timecardout}">
        <div th:if="${timecardLast.status} == 0" style="width: 100%" class="waves-effect card-panel deep-orange darken-2 z-depth-4">
            <div class="card-content center-align">
                <i class="medium material-icons white-text">timer_off</i>
                <h5 class="white-text">SIGN OUT OF WORK</h5>
            </div>
        </div>
    </a>
    </div>

Thymeleaf 中列表的语法是命名元素和命名列表。你有同一个东西的两个名字。所以你可能想要:

<div th:each="timecard: ${timecardList}">

...
    <div th:if="${timecard.status == null}"...>
...

将名为 timecardList 的对象列表添加到模型后。

退后一步,看看我是如何创建对象的。我没有先初始化对象,即

Timecard timecardLast = timecardService.getLastTimecardByIdusers(staff);

因此,对象的简单初始化正确地完成了数据库请求之后的工作,即

timecardLast = new Timecard();
timecardLast = timecardService.getLastTimecardByIdusers(staff);