Spring Boot 技巧與解決辦法

Thymeleaf 日期渲染

Thymeleaf 對 LocalData、LocalDateTime 對象不能直接獲取其值渲染到頁面,要依靠其模板引擎提供的方法。

添加依賴

<!-- Thymeleaf 擴展,支持對 java 日期的操作 -->
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-java8time</artifactId>
        </dependency>

使用 #temporals.format#dates.format 格式化時間對象

<div class="col-md-9">
    <input type="date" class="form-control" id="contract-start-time"
        name="contract-start-time"
        th:value="${null != sale and null != sale.contractStartTime}?${#temporals.format(sale.contractStartTime, 'yyyy-MM-dd')}:''"
        placeholder="協議開始時間"/>
</div>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章