From 5c8043dcea40c5ec30fd73a5bac5d3bec23890a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Mon, 8 Apr 2024 10:31:35 +0200 Subject: [PATCH] Use code includes and tabs in date/time format documentation See gh-22171 --- ...uring-formatting-globaldatetimeformat.adoc | 101 +----------------- .../ApplicationConfiguration.java | 58 ++++++++++ .../ApplicationConfiguration.kt | 52 +++++++++ .../ApplicationConfiguration.xml | 29 +++++ 4 files changed, 141 insertions(+), 99 deletions(-) create mode 100644 framework-docs/src/main/java/org/springframework/docs/core/validation/formatconfiguringformattingglobaldatetimeformat/ApplicationConfiguration.java create mode 100644 framework-docs/src/main/kotlin/org/springframework/docs/core/validation/formatconfiguringformattingglobaldatetimeformat/ApplicationConfiguration.kt create mode 100644 framework-docs/src/main/resources/org/springframework/docs/core/validation/formatconfiguringformattingglobaldatetimeformat/ApplicationConfiguration.xml diff --git a/framework-docs/modules/ROOT/pages/core/validation/format-configuring-formatting-globaldatetimeformat.adoc b/framework-docs/modules/ROOT/pages/core/validation/format-configuring-formatting-globaldatetimeformat.adoc index 82fc655f131..1b67d8467a1 100644 --- a/framework-docs/modules/ROOT/pages/core/validation/format-configuring-formatting-globaldatetimeformat.adoc +++ b/framework-docs/modules/ROOT/pages/core/validation/format-configuring-formatting-globaldatetimeformat.adoc @@ -11,106 +11,9 @@ formatters manually with the help of: * `org.springframework.format.datetime.standard.DateTimeFormatterRegistrar` * `org.springframework.format.datetime.DateFormatterRegistrar` -For example, the following Java configuration registers a global `yyyyMMdd` format: +For example, the following configuration registers a global `yyyyMMdd` format: -[tabs] -====== -Java:: -+ -[source,java,indent=0,subs="verbatim,quotes",role="primary"] ----- - @Configuration - public class AppConfig { - - @Bean - public FormattingConversionService conversionService() { - - // Use the DefaultFormattingConversionService but do not register defaults - DefaultFormattingConversionService conversionService = - new DefaultFormattingConversionService(false); - - // Ensure @NumberFormat is still supported - conversionService.addFormatterForFieldAnnotation( - new NumberFormatAnnotationFormatterFactory()); - - // Register JSR-310 date conversion with a specific global format - DateTimeFormatterRegistrar dateTimeRegistrar = new DateTimeFormatterRegistrar(); - dateTimeRegistrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyyMMdd")); - dateTimeRegistrar.registerFormatters(conversionService); - - // Register date conversion with a specific global format - DateFormatterRegistrar dateRegistrar = new DateFormatterRegistrar(); - dateRegistrar.setFormatter(new DateFormatter("yyyyMMdd")); - dateRegistrar.registerFormatters(conversionService); - - return conversionService; - } - } ----- - -Kotlin:: -+ -[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"] ----- - @Configuration - class AppConfig { - - @Bean - fun conversionService(): FormattingConversionService { - // Use the DefaultFormattingConversionService but do not register defaults - return DefaultFormattingConversionService(false).apply { - - // Ensure @NumberFormat is still supported - addFormatterForFieldAnnotation(NumberFormatAnnotationFormatterFactory()) - - // Register JSR-310 date conversion with a specific global format - val dateTimeRegistrar = DateTimeFormatterRegistrar() - dateTimeRegistrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyyMMdd")) - dateTimeRegistrar.registerFormatters(this) - - // Register date conversion with a specific global format - val dateRegistrar = DateFormatterRegistrar() - dateRegistrar.setFormatter(DateFormatter("yyyyMMdd")) - dateRegistrar.registerFormatters(this) - } - } - } ----- -====== - -If you prefer XML-based configuration, you can use a -`FormattingConversionServiceFactoryBean`. The following example shows how to do so: - -[source,xml,indent=0,subs="verbatim,quotes"] ----- - - - - - - - - - - - - - - - - - - - - - - - ----- +include-code::./ApplicationConfiguration[tag=snippet,indent=0] Note there are extra considerations when configuring date and time formats in web applications. Please see diff --git a/framework-docs/src/main/java/org/springframework/docs/core/validation/formatconfiguringformattingglobaldatetimeformat/ApplicationConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/core/validation/formatconfiguringformattingglobaldatetimeformat/ApplicationConfiguration.java new file mode 100644 index 00000000000..7f151848c9c --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/core/validation/formatconfiguringformattingglobaldatetimeformat/ApplicationConfiguration.java @@ -0,0 +1,58 @@ +/* + * Copyright 2002-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.core.validation.formatconfiguringformattingglobaldatetimeformat; + +import java.time.format.DateTimeFormatter; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.format.datetime.DateFormatter; +import org.springframework.format.datetime.DateFormatterRegistrar; +import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar; +import org.springframework.format.number.NumberFormatAnnotationFormatterFactory; +import org.springframework.format.support.DefaultFormattingConversionService; +import org.springframework.format.support.FormattingConversionService; + +// tag::snippet[] +@Configuration +public class ApplicationConfiguration { + + @Bean + public FormattingConversionService conversionService() { + + // Use the DefaultFormattingConversionService but do not register defaults + DefaultFormattingConversionService conversionService = + new DefaultFormattingConversionService(false); + + // Ensure @NumberFormat is still supported + conversionService.addFormatterForFieldAnnotation( + new NumberFormatAnnotationFormatterFactory()); + + // Register JSR-310 date conversion with a specific global format + DateTimeFormatterRegistrar dateTimeRegistrar = new DateTimeFormatterRegistrar(); + dateTimeRegistrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyyMMdd")); + dateTimeRegistrar.registerFormatters(conversionService); + + // Register date conversion with a specific global format + DateFormatterRegistrar dateRegistrar = new DateFormatterRegistrar(); + dateRegistrar.setFormatter(new DateFormatter("yyyyMMdd")); + dateRegistrar.registerFormatters(conversionService); + + return conversionService; + } +} +// end::snippet[] \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/core/validation/formatconfiguringformattingglobaldatetimeformat/ApplicationConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/core/validation/formatconfiguringformattingglobaldatetimeformat/ApplicationConfiguration.kt new file mode 100644 index 00000000000..c4688db9487 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/core/validation/formatconfiguringformattingglobaldatetimeformat/ApplicationConfiguration.kt @@ -0,0 +1,52 @@ +/* + * Copyright 2002-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.docs.core.validation.formatconfiguringformattingglobaldatetimeformat + +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.format.datetime.DateFormatter +import org.springframework.format.datetime.DateFormatterRegistrar +import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar +import org.springframework.format.number.NumberFormatAnnotationFormatterFactory +import org.springframework.format.support.DefaultFormattingConversionService +import org.springframework.format.support.FormattingConversionService +import java.time.format.DateTimeFormatter + +// tag::snippet[] +@Configuration +class ApplicationConfiguration { + + @Bean + fun conversionService(): FormattingConversionService { + // Use the DefaultFormattingConversionService but do not register defaults + return DefaultFormattingConversionService(false).apply { + + // Ensure @NumberFormat is still supported + addFormatterForFieldAnnotation(NumberFormatAnnotationFormatterFactory()) + + // Register JSR-310 date conversion with a specific global format + val dateTimeRegistrar = DateTimeFormatterRegistrar() + dateTimeRegistrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyyMMdd")) + dateTimeRegistrar.registerFormatters(this) + + // Register date conversion with a specific global format + val dateRegistrar = DateFormatterRegistrar() + dateRegistrar.setFormatter(DateFormatter("yyyyMMdd")) + dateRegistrar.registerFormatters(this) + } + } +} +// end::snippet[] \ No newline at end of file diff --git a/framework-docs/src/main/resources/org/springframework/docs/core/validation/formatconfiguringformattingglobaldatetimeformat/ApplicationConfiguration.xml b/framework-docs/src/main/resources/org/springframework/docs/core/validation/formatconfiguringformattingglobaldatetimeformat/ApplicationConfiguration.xml new file mode 100644 index 00000000000..7e5072e6302 --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/core/validation/formatconfiguringformattingglobaldatetimeformat/ApplicationConfiguration.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + +