如何:带有Thymeleaf和Spring Boot的Java 8日期和时间
如果您碰巧使用Spring Boot和Thymeleaf,并且需要在視圖中格式化Java 8 Date&Time對象,則可以使用thymeleaf-extras-java8time –用于Java 8 Date&Time API的Thymeleaf模塊。
向現(xiàn)有的基于Maven或Gradle的Spring Boot項(xiàng)目中添加thymeleaf-extras-java8time就像添加依賴項(xiàng)并使用模板引擎注冊新方言一樣容易。
對于Maven,將以下依賴項(xiàng)添加到現(xiàn)有的POM中:
<dependency><groupId>org.thymeleaf.extras</groupId><artifactId>thymeleaf-extras-java8time</artifactId><version>2.1.0.RELEASE</version> </dependency>完成后,下一步就是將方言添加到模板引擎。 使用Spring Boot,您需要在應(yīng)用程序上下文中定義一個類型為org.thymeleaf.extras.java8time.dialect.Java8TimeDialect的bean。 所有類型為org.thymeleaf.dialect.IDialect bean org.thymeleaf.dialect.IDialect注入到Spring Boot的ThymeleafAutoConfiguration并自動添加到Thymeleaf的SpringTemplateEngine 。
@SpringBootApplication public class Application {@Beanpublic Java8TimeDialect java8TimeDialect() {return new Java8TimeDialect();}public static void main(String[] args) {SpringApplication.run(Application.class);} }Java8TimeDialect是,在表達(dá)式求值期間將temporals對象作為實(shí)用程序?qū)ο筇砑拥缴舷挛摹?這意味著它可以在OGNL或SpringEL表達(dá)式評估中使用:
The time is: <strong th:text="${#temporals.format(now, 'dd/MMM/yyyy HH:mm')}">31/12/2015 15:00</strong>temporals java.time.Temporal提供了許多實(shí)用的方法來與java.time.Temporal一起java.time.Temporal :格式化,訪問屬性和創(chuàng)建新對象。 有關(guān)GitHub上擴(kuò)展和時temporals本身檢出項(xiàng)目頁面的更多信息: thymeleaf-extras-java8time
注意 :Spring Boot和Thymeleaf項(xiàng)目設(shè)置在此博客文章中有更詳細(xì)的描述: Spring Boot和Thymeleaf與Maven
- 本博客文章中使用的源代碼: https : //github.com/kolorobot/spring-boot-thymeleaf
翻譯自: https://www.javacodegeeks.com/2015/11/how-to-java-8-date-time-with-thymeleaf-and-spring-boot.html
總結(jié)
以上是生活随笔為你收集整理的如何:带有Thymeleaf和Spring Boot的Java 8日期和时间的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我个人的CRUD故事-或我如何来到CUB
- 下一篇: 拜托,Java。 最终是否支持多行字符串