This commit is contained in:
Keith Donald 2009-09-03 16:30:46 +00:00
parent a03ca86372
commit 9b2f161b7e
1 changed files with 61 additions and 20 deletions

View File

@ -1024,11 +1024,52 @@ public class Money {
When referenced by a @Formatted annotation, a Formatter implementation must declare a public default constructor. When referenced by a @Formatted annotation, a Formatter implementation must declare a public default constructor.
</para> </para>
</section> </section>
<section id="ui-format-AnnotationFormatterFactory"> <section id="ui-format-CustomFormatAnnotations">
<title>Custom Format Annotations</title> <title>Custom Format Annotations</title>
<para> <para>
Field-specific formatting can be triggered by annotating model properties. 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:
</para>
<programlisting language="java"><![CDATA[
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Formatted(CurrencyFormatter.class)
public @interface Currency {
}
]]>
</programlisting>
<para>
Then, to trigger formatting, simply annotate a model property with the annotation:
</para>
<programlisting language="java"><![CDATA[
public class MyModel {
@Currency
private BigDecimal amount;
}
]]>
</programlisting>
<para>
Custom annotations like @Currency can also be annotated with JSR-303 constraint annotations to specify declarative validation constraints.
For example:
</para>
<programlisting language="java"><![CDATA[
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Formatted(CurrencyFormatter.class)
@Constraint(validatedBy = CurrencyValidator.class)
public @interface Currency {
}
]]>
</programlisting>
<para>
Given the example above, on form postback any @Currency properties will first be parsed by CurrencyFormatter, then validated by CurrencyValidator.
</para>
<section id="ui-format-AnnotationFormatterFactory">
<title>AnnotationFormatterFactory</title>
<para>
If your custom annotation has attributes to configure Formatter instance behavior by property, implement a AnnotationFormatterFactory:
</para> </para>
<programlisting language="java"><![CDATA[ <programlisting language="java"><![CDATA[
package org.springframework.ui.format; package org.springframework.ui.format;
@ -1041,12 +1082,10 @@ public interface AnnotationFormatterFactory<A extends Annotation, T> {
]]> ]]>
</programlisting> </programlisting>
<para> <para>
The example implementation below binds a custom @DecimalFormat annotation to a Number Formatter instance. The example implementation below binds a @DecimalFormat instance to a Formatter instance.
This particular annotation allows the format pattern to be configured. This particular annotation allows the NumberFormat pattern to be configured.
</para> </para>
<programlisting language="java"><![CDATA[ <programlisting language="java"><![CDATA[
package example.format;
public class DecimalAnnotationFormatterFactory implements AnnotationFormatterFactory<DecimalFormat, Number> { public class DecimalAnnotationFormatterFactory implements AnnotationFormatterFactory<DecimalFormat, Number> {
Formatter<Number> getFormatter(DecimalFormat annotation) { Formatter<Number> getFormatter(DecimalFormat annotation) {
@ -1061,8 +1100,6 @@ 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:
</para> </para>
<programlisting language="java"><![CDATA[ <programlisting language="java"><![CDATA[
package example.app;
public class MyModel { public class MyModel {
@DecimalFormat("#,###") @DecimalFormat("#,###")
@ -1072,10 +1109,11 @@ public class MyModel {
]]> ]]>
</programlisting> </programlisting>
</section> </section>
</section>
<section id="ui-format-FormatterRegistry-SPI"> <section id="ui-format-FormatterRegistry-SPI">
<title>FormatterRegistry SPI</title> <title>FormatterRegistry SPI</title>
<para> <para>
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. 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. 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. 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: Review the FormatterRegistry SPI below:
</para> </para>
<programlisting language="java"><![CDATA[ <programlisting language="java"><![CDATA[
package org.springframework.ui.format;
public interface FormatterRegistry { public interface FormatterRegistry {
void add(Formatter<?> formatter); void add(Formatter<?> formatter);
@ -1121,7 +1161,7 @@ public interface FormatterRegistry {
<bean class="org.springframework.ui.format.GenericFormatterRegistry"> <bean class="org.springframework.ui.format.GenericFormatterRegistry">
<property name="formatters"> <property name="formatters">
<list> <list>
<!-- Register formatter beans here --> <!-- Register Formatter beans here -->
</list> </list>
</property> </property>
<property name="annotationFormatterFactories"> <property name="annotationFormatterFactories">
@ -1136,6 +1176,7 @@ public interface FormatterRegistry {
</bean>]]> </bean>]]>
</programlisting> </programlisting>
<para> <para>
When using the @Formatted annotation, no explicit Formatter or AnnotationFormatterFactory registration is required.
See the JavaDocs for GenericFormatterRegistry for more configuration options. See the JavaDocs for GenericFormatterRegistry for more configuration options.
</para> </para>
</section> </section>