Spring Thymeleaf SPEL - 如果null不畏缩

时间:2018-01-24 22:37:11

标签: spring hibernate thymeleaf spring-el

我有以下代码,除了检查值是否为null之外,一切运行正常。我知道返回的值是null,但它仍然无法工作。我已经尝试在{}内外使用== null无效。

也许它与hibernate如何返回值有关?当我打印出从db返回的对象时,它表示为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>

2 个答案:

答案 0 :(得分:0)

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

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

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

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

答案 1 :(得分:0)

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

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

因此,对象的简单初始化是正确的,然后是db请求,即

@Bean
public MqttPahoClientFactory mqttClientFactory() {
    DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
    factory.setServerURIs("tcp://localhost:1883");
    factory.setUserName("guest");
    factory.setPassword("guest");
    return factory;
}

// publisher

@Bean
public IntegrationFlow mqttOutFlow() {
    return IntegrationFlows.from(CharacterStreamReadingMessageSource.stdin(),
                    e -> e.poller(Pollers.fixedDelay(1000)))
            .transform(p -> p + " sent to MQTT")
            .handle(mqttOutbound())
            .get();
}

@Bean
public MessageHandler mqttOutbound() {
    MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler("siSamplePublisher", mqttClientFactory());
    messageHandler.setAsync(true);
    messageHandler.setDefaultTopic("siSampleTopic");
    return messageHandler;
}

// consumer

@Bean
public IntegrationFlow mqttInFlow() {
    return IntegrationFlows.from(mqttInbound())
            .transform(p -> p + ", received from MQTT")
            .handle(logger())
            .get();
}

private LoggingHandler logger() {
    LoggingHandler loggingHandler = new LoggingHandler("INFO");
    loggingHandler.setLoggerName("siSample");
    return loggingHandler;
}

@Bean
public MessageProducerSupport mqttInbound() {
    MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter("siSampleConsumer",
            mqttClientFactory(), "siSampleTopic");
    adapter.setCompletionTimeout(5000);
    adapter.setConverter(new DefaultPahoMessageConverter());
    adapter.setQos(1);
    return adapter;
}