操作方法:带有Thymeleaf和Spring Boot的Java 8日期和时间
如果您碰巧使用Spring Boot和Thymeleaf,并且需要在視圖中格式化Java 8 Date&Time對象,則可以使用thymeleaf-extras-java8time –用于Java 8 Date&Time API的Thymeleaf模塊。
向現有的基于Maven或Gradle的Spring Boot項目中添加thymeleaf-extras-java8time就像添加依賴項并使用模板引擎注冊新方言一樣容易。
對于Maven,將以下依賴項添加到現有的POM中:
<dependency><groupId>org.thymeleaf.extras</groupId><artifactId>thymeleaf-extras-java8time</artifactId><version>2.1.0.RELEASE</version> </dependency>完成后,下一步就是將方言添加到模板引擎中。 使用Spring Boot,您需要在應用程序上下文中定義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是,在表達式求值期間將temporals對象作為實用程序對象添加到上下文。 這意味著它可以用于OGNL或SpringEL表達式評估:
The time is: <strong th:text="${#temporals.format(now, 'dd/MMM/yyyy HH:mm')}">31/12/2015 15:00</strong>temporals屬性提供了許多實用的方法來使用java.time.Temporal :格式化,訪問屬性和創建新對象。 有關GitHub上擴展和時temporals本身檢出項目頁面的更多信息: thymeleaf-extras-java8time
注意 :Spring Boot和Thymeleaf項目設置在此博客文章中有更詳細的描述: 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
總結
以上是生活随笔為你收集整理的操作方法:带有Thymeleaf和Spring Boot的Java 8日期和时间的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 蓝牙键盘安卓操作技巧(蓝牙键盘 安卓)
- 下一篇: 五皇后问题 java_Java的5个古怪