diff --git a/spring-framework-reference/src/validation.xml b/spring-framework-reference/src/validation.xml index 9454a793c5b..83c646372b6 100644 --- a/spring-framework-reference/src/validation.xml +++ b/spring-framework-reference/src/validation.xml @@ -1024,13 +1024,54 @@ public class Money { When referenced by a @Formatted annotation, a Formatter implementation must declare a public default constructor. -
+
Custom Format Annotations Field-specific formatting can be triggered by annotating model properties. - To bind a custom annotation to a Formatter instance, implement a AnnotationFormatterFactory: + To bind a custom annotation to a Formatter instance, simply annotate the annotation as @Formatted: + + + Then, to trigger formatting, simply annotate a model property with the annotation: + + + + + Custom annotations like @Currency can also be annotated with JSR-303 constraint annotations to specify declarative validation constraints. + For example: + + + + + Given the example above, on form postback any @Currency properties will first be parsed by CurrencyFormatter, then validated by CurrencyValidator. + +
+ AnnotationFormatterFactory + + If your custom annotation has attributes to configure Formatter instance behavior by property, implement a AnnotationFormatterFactory: + + { @@ -1039,14 +1080,12 @@ public interface AnnotationFormatterFactory { } ]]> - - - The example implementation below binds a custom @DecimalFormat annotation to a Number Formatter instance. - This particular annotation allows the format pattern to be configured. - - + + The example implementation below binds a @DecimalFormat instance to a Formatter instance. + This particular annotation allows the NumberFormat pattern to be configured. + + { Formatter getFormatter(DecimalFormat annotation) { @@ -1056,13 +1095,11 @@ public class DecimalAnnotationFormatterFactory implements AnnotationFormatterFac } } ]]> - - - Then, to trigger, simply annotate a property as a @DecimalFormat in your model: - - + + Then, to trigger, simply annotate a property as a @DecimalFormat in your model: + + - + +
FormatterRegistry SPI - Formatters are typically registered in a FormatterRegistry. + Formatters can be registered in a FormatterRegistry. A DataBinder uses this registry to resolve the Formatter to use for a specific field. This allows you to configure default Formatting rules centrally, rather than duplicating such configuration across your UI Controllers. For example, you might want to enforce that all Date fields are formatted a certain way, or fields with a specific annotation are formatted in a certain way. @@ -1085,6 +1123,8 @@ public class MyModel { Review the FormatterRegistry SPI below: formatter); @@ -1121,7 +1161,7 @@ public interface FormatterRegistry { - + @@ -1136,6 +1176,7 @@ public interface FormatterRegistry { ]]> + When using the @Formatted annotation, no explicit Formatter or AnnotationFormatterFactory registration is required. See the JavaDocs for GenericFormatterRegistry for more configuration options.