@@ -362,10 +362,9 @@ the knowledge of how to convert properties to the desired type. Read more about
362
362
A couple of examples where property editing is used in Spring:
363
363
364
364
* __setting properties on beans__ is done using `PropertyEditors`. When mentioning
365
- `java.lang.String` as the value of a property of some bean you're declaring in XML
366
- file, Spring will (if the setter of the corresponding property has a
367
- `Class`-parameter) use the `ClassEditor` to try to resolve the parameter to a `Class`
368
- object.
365
+ `String` as the value of a property of some bean you're declaring in XML file,
366
+ Spring will (if the setter of the corresponding property has `Class` parameter)
367
+ use the `ClassEditor` to try to resolve the parameter to a `Class` object.
369
368
* __parsing HTTP request parameters__ in Spring's MVC framework is done using all kinds
370
369
of `PropertyEditors` that you can manually bind in all subclasses of the
371
370
`CommandController`.
@@ -761,9 +760,9 @@ Consider `StringToInteger` as an example for a typical `Converter` implementatio
761
760
[[core-convert-ConverterFactory-SPI]]
762
761
=== ConverterFactory
763
762
764
- When you need to centralize the conversion logic for an entire class hierarchy, for
765
- example, when converting from String to java.lang. Enum objects, implement
766
- `ConverterFactory`:
763
+ When you need to centralize the conversion logic for an entire class hierarchy
764
+ (for example, when converting from ` String` to ` Enum` objects), you can implement
765
+ `ConverterFactory`, as the following example shows :
767
766
768
767
[source,java,indent=0]
769
768
[subs="verbatim,quotes"]
@@ -777,10 +776,10 @@ example, when converting from String to java.lang.Enum objects, implement
777
776
----
778
777
779
778
Parameterize S to be the type you are converting from and R to be the base type defining
780
- the __range__ of classes you can convert to. Then implement getConverter(Class<T>),
779
+ the __range__ of classes you can convert to. Then implement ` getConverter(Class<T>)` ,
781
780
where T is a subclass of R.
782
781
783
- Consider the `StringToEnum` ConverterFactory as an example:
782
+ Consider the `StringToEnumConverterFactory` as an example:
784
783
785
784
[source,java,indent=0]
786
785
[subs="verbatim,quotes"]
@@ -813,12 +812,13 @@ Consider the `StringToEnum` ConverterFactory as an example:
813
812
[[core-convert-GenericConverter-SPI]]
814
813
=== GenericConverter
815
814
816
- When you require a sophisticated Converter implementation, consider the GenericConverter
817
- interface. With a more flexible but less strongly typed signature, a GenericConverter
818
- supports converting between multiple source and target types. In addition, a
819
- GenericConverter makes available source and target field context you can use when
820
- implementing your conversion logic. Such context allows a type conversion to be driven
821
- by a field annotation, or generic information declared on a field signature.
815
+ When you require a sophisticated `Converter` implementation, consider using the
816
+ `GenericConverter` interface. With a more flexible but less strongly typed signature
817
+ than `Converter`, a `GenericConverter` supports converting between multiple source and
818
+ target types. In addition, a `GenericConverter` makes available source and target field
819
+ context that you can use when you implement your conversion logic. Such context lets a
820
+ type conversion be driven by a field annotation or by generic information declared on a
821
+ field signature. The following listing shows the interface definition of `GenericConverter`:
822
822
823
823
[source,java,indent=0]
824
824
[subs="verbatim,quotes"]
@@ -833,22 +833,22 @@ by a field annotation, or generic information declared on a field signature.
833
833
}
834
834
----
835
835
836
- To implement a GenericConverter, have getConvertibleTypes() return the supported
837
- source->target type pairs. Then implement convert(Object, TypeDescriptor,
838
- TypeDescriptor) to implement your conversion logic. The source TypeDescriptor provides
839
- access to the source field holding the value being converted. The target TypeDescriptor
840
- provides access to the target field where the converted value will be set.
836
+ To implement a ` GenericConverter` , have ` getConvertibleTypes()` return the supported
837
+ source->target type pairs. Then implement ` convert(Object, TypeDescriptor,
838
+ TypeDescriptor)` to contain your conversion logic. The source ` TypeDescriptor` provides
839
+ access to the source field that holds the value being converted. The target ` TypeDescriptor`
840
+ provides access to the target field where the converted value is to be set.
841
841
842
- A good example of a GenericConverter is a converter that converts between a Java Array
843
- and a Collection . Such an ArrayToCollectionConverter introspects the field that declares
844
- the target Collection type to resolve the Collection 's element type. This allows each
845
- element in the source array to be converted to the Collection element type before the
846
- Collection is set on the target field.
842
+ A good example of a ` GenericConverter` is a converter that converts between a Java array
843
+ and a collection . Such an ` ArrayToCollectionConverter` introspects the field that declares
844
+ the target collection type to resolve the collection 's element type. This lets each
845
+ element in the source array be converted to the collection element type before the
846
+ collection is set on the target field.
847
847
848
848
[NOTE]
849
849
====
850
- Because GenericConverter is a more complex SPI interface, only use it when you need it.
851
- Favor Converter or ConverterFactory for basic type conversion needs.
850
+ Because ` GenericConverter` is a more complex SPI interface, only use it when you need it.
851
+ Favor ` Converter` or ` ConverterFactory` for basic type conversion needs.
852
852
====
853
853
854
854
@@ -1044,11 +1044,11 @@ general __core.convert__ Converter SPI does not address such __formatting__ requ
1044
1044
directly. To directly address them, Spring 3 introduces a convenient Formatter SPI that
1045
1045
provides a simple and robust alternative to PropertyEditors for client environments.
1046
1046
1047
- In general, use the Converter SPI when you need to implement general-purpose type
1048
- conversion logic; for example, for converting between a java.util.Date and and
1049
- java.lang.Long. Use the Formatter SPI when you're working in a client environment, such
1050
- as a web application, and need to parse and print localized field values. The
1051
- ConversionService provides a unified type conversion API for both SPIs.
1047
+ In general, you can use the ` Converter` SPI when you need to implement general-purpose type
1048
+ conversion logic -- for example, for converting between a ` java.util.Date` and a `Long`.
1049
+ You can use the ` Formatter` SPI when you work in a client environment ( such as a web
1050
+ application) and need to parse and print localized field values. The `ConversionService`
1051
+ provides a unified type conversion API for both SPIs.
1052
1052
1053
1053
1054
1054
@@ -1088,22 +1088,22 @@ Where Formatter extends from the Printer and Parser building-block interfaces:
1088
1088
}
1089
1089
----
1090
1090
1091
- To create your own Formatter, simply implement the Formatter interface above .
1092
- Parameterize T to be the type of object you wish to format, for example,
1093
- `java.util.Date`. Implement the `print()` operation to print an instance of T for
1091
+ To create your own ` Formatter`, implement the ` Formatter` interface shown earlier .
1092
+ Parameterize `T` to be the type of object you wish to format -- for example,
1093
+ `java.util.Date`. Implement the `print()` operation to print an instance of `T` for
1094
1094
display in the client locale. Implement the `parse()` operation to parse an instance of
1095
- T from the formatted representation returned from the client locale. Your Formatter
1096
- should throw a ParseException or IllegalArgumentException if a parse attempt fails. Take
1097
- care to ensure your Formatter implementation is thread-safe.
1095
+ `T` from the formatted representation returned from the client locale. Your ` Formatter`
1096
+ should throw a ` ParseException` or an ` IllegalArgumentException` if a parse attempt fails. Take
1097
+ care to ensure that your ` Formatter` implementation is thread-safe.
1098
1098
1099
- Several Formatter implementations are provided in `format` subpackages as a convenience.
1100
- The `number` package provides a `NumberStyleFormatter`, `CurrencyStyleFormatter`, and
1101
- `PercentStyleFormatter` to format `java.lang. Number` objects using a `java.text.NumberFormat`.
1099
+ The `format` subpackages provide several `Formatter` implementations as a convenience.
1100
+ The `number` package provides `NumberStyleFormatter`, `CurrencyStyleFormatter`, and
1101
+ `PercentStyleFormatter` to format `Number` objects that use a `java.text.NumberFormat`.
1102
1102
The `datetime` package provides a `DateFormatter` to format `java.util.Date` objects with
1103
1103
a `java.text.DateFormat`. The `datetime.joda` package provides comprehensive datetime
1104
1104
formatting support based on the http://joda-time.sourceforge.net[Joda-Time library].
1105
1105
1106
- Consider `DateFormatter` as an example `Formatter` implementation:
1106
+ The following `DateFormatter` is an example `Formatter` implementation:
1107
1107
1108
1108
[source,java,indent=0]
1109
1109
[subs="verbatim,quotes"]
@@ -1230,10 +1230,11 @@ To trigger formatting, simply annotate fields with @NumberFormat:
1230
1230
==== Format Annotation API
1231
1231
1232
1232
A portable format annotation API exists in the `org.springframework.format.annotation`
1233
- package. Use @NumberFormat to format java.lang.Number fields. Use @DateTimeFormat to
1234
- format java.util.Date, java.util.Calendar, java.util.Long, or Joda-Time fields.
1233
+ package. You can use `@NumberFormat` to format `Number` fields such as `Double` and
1234
+ `Long`, and `@DateTimeFormat` to format `java.util.Date`, `java.util.Calendar`, `Long`
1235
+ (for millisecond timestamps) as well as JSR-310 `java.time` and Joda-Time value types.
1235
1236
1236
- The example below uses @DateTimeFormat to format a java.util.Date as a ISO Date
1237
+ The following example uses ` @DateTimeFormat` to format a ` java.util.Date` as an ISO Date
1237
1238
(yyyy-MM-dd):
1238
1239
1239
1240
[source,java,indent=0]
0 commit comments