|
6 | 6 | By default, formatters for various number and date types are installed, along with support
|
7 | 7 | for customization via `@NumberFormat` and `@DateTimeFormat` on fields.
|
8 | 8 |
|
9 |
| -To register custom formatters and converters in Java config, use the following: |
| 9 | +To register custom formatters and converters, use the following: |
10 | 10 |
|
11 |
| -[tabs] |
12 |
| -====== |
13 |
| -Java:: |
14 |
| -+ |
15 |
| -[source,java,indent=0,subs="verbatim,quotes",role="primary"] |
16 |
| ----- |
17 |
| - @Configuration |
18 |
| - @EnableWebMvc |
19 |
| - public class WebConfig implements WebMvcConfigurer { |
20 |
| -
|
21 |
| - @Override |
22 |
| - public void addFormatters(FormatterRegistry registry) { |
23 |
| - // ... |
24 |
| - } |
25 |
| - } |
26 |
| ----- |
27 |
| -
|
28 |
| -Kotlin:: |
29 |
| -+ |
30 |
| -[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"] |
31 |
| ----- |
32 |
| - @Configuration |
33 |
| - @EnableWebMvc |
34 |
| - class WebConfig : WebMvcConfigurer { |
35 |
| -
|
36 |
| - override fun addFormatters(registry: FormatterRegistry) { |
37 |
| - // ... |
38 |
| - } |
39 |
| - } |
40 |
| ----- |
41 |
| -====== |
42 |
| - |
43 |
| -To do the same in XML config, use the following: |
44 |
| - |
45 |
| -[source,xml,indent=0,subs="verbatim,quotes"] |
46 |
| ----- |
47 |
| - <?xml version="1.0" encoding="UTF-8"?> |
48 |
| - <beans xmlns="http://www.springframework.org/schema/beans" |
49 |
| - xmlns:mvc="http://www.springframework.org/schema/mvc" |
50 |
| - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
51 |
| - xsi:schemaLocation=" |
52 |
| - http://www.springframework.org/schema/beans |
53 |
| - https://www.springframework.org/schema/beans/spring-beans.xsd |
54 |
| - http://www.springframework.org/schema/mvc |
55 |
| - https://www.springframework.org/schema/mvc/spring-mvc.xsd"> |
56 |
| -
|
57 |
| - <mvc:annotation-driven conversion-service="conversionService"/> |
58 |
| -
|
59 |
| - <bean id="conversionService" |
60 |
| - class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> |
61 |
| - <property name="converters"> |
62 |
| - <set> |
63 |
| - <bean class="org.example.MyConverter"/> |
64 |
| - </set> |
65 |
| - </property> |
66 |
| - <property name="formatters"> |
67 |
| - <set> |
68 |
| - <bean class="org.example.MyFormatter"/> |
69 |
| - <bean class="org.example.MyAnnotationFormatterFactory"/> |
70 |
| - </set> |
71 |
| - </property> |
72 |
| - <property name="formatterRegistrars"> |
73 |
| - <set> |
74 |
| - <bean class="org.example.MyFormatterRegistrar"/> |
75 |
| - </set> |
76 |
| - </property> |
77 |
| - </bean> |
78 |
| -
|
79 |
| - </beans> |
80 |
| ----- |
| 11 | +include-code::./WebConfiguration[tag=snippet,indent=0] |
81 | 12 |
|
82 | 13 | By default Spring MVC considers the request Locale when parsing and formatting date
|
83 | 14 | values. This works for forms where dates are represented as Strings with "input" form
|
84 | 15 | fields. For "date" and "time" form fields, however, browsers use a fixed format defined
|
85 | 16 | in the HTML spec. For such cases date and time formatting can be customized as follows:
|
86 | 17 |
|
87 |
| -[tabs] |
88 |
| -====== |
89 |
| -Java:: |
90 |
| -+ |
91 |
| -[source,java,indent=0,subs="verbatim,quotes",role="primary"] |
92 |
| ----- |
93 |
| - @Configuration |
94 |
| - @EnableWebMvc |
95 |
| - public class WebConfig implements WebMvcConfigurer { |
96 |
| -
|
97 |
| - @Override |
98 |
| - public void addFormatters(FormatterRegistry registry) { |
99 |
| - DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar(); |
100 |
| - registrar.setUseIsoFormat(true); |
101 |
| - registrar.registerFormatters(registry); |
102 |
| - } |
103 |
| - } |
104 |
| ----- |
105 |
| -
|
106 |
| -Kotlin:: |
107 |
| -+ |
108 |
| -[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"] |
109 |
| ----- |
110 |
| - @Configuration |
111 |
| - @EnableWebMvc |
112 |
| - class WebConfig : WebMvcConfigurer { |
113 |
| -
|
114 |
| - override fun addFormatters(registry: FormatterRegistry) { |
115 |
| - val registrar = DateTimeFormatterRegistrar() |
116 |
| - registrar.setUseIsoFormat(true) |
117 |
| - registrar.registerFormatters(registry) |
118 |
| - } |
119 |
| - } |
120 |
| ----- |
121 |
| -====== |
| 18 | +include-code::./DateTimeWebConfiguration[tag=snippet,indent=0] |
122 | 19 |
|
123 | 20 | NOTE: See xref:core/validation/format.adoc#format-FormatterRegistrar-SPI[the `FormatterRegistrar` SPI]
|
124 | 21 | and the `FormattingConversionServiceFactoryBean` for more information on when to use
|
|
0 commit comments