diff --git a/spring-framework-reference/src/validation.xml b/spring-framework-reference/src/validation.xml index cb437faca508cf32f8b549d7bf20d0bae5f3986f..8f7847b70917dbf6e945baf25c87f49cf30ff84e 100644 --- a/spring-framework-reference/src/validation.xml +++ b/spring-framework-reference/src/validation.xml @@ -58,8 +58,7 @@ Let's consider a small data object: - -]]> +]]> Finally, and in a bit of a departure from the focus of this chapter, for those of you using Spring's MVC web @@ -878,8 +878,7 @@ public final class CustomPropertyEditorRegistrar implements PropertyEditorRegist The SPI to implement type conversion logic is simple and strongly typed: - { @@ -901,15 +900,15 @@ public interface Converter { Consider StringToInteger as an example Converter implementation: - package org.springframework.core.convert.support; + { public Integer convert(String source) { return Integer.valueOf(source); } -} +}]]>
@@ -920,8 +919,7 @@ final class StringToInteger implements Converter<String, Integer> { java.lang.Enum objects, implement ConverterFactory: - { @@ -937,8 +935,7 @@ public interface ConverterFactory { Consider the StringToEnum ConverterFactory as an example: - { @@ -973,8 +970,7 @@ final class StringToEnumConverterFactory implements ConverterFactory - - +}]]> To implement a GenericConverter, have getConvertibleTypes() return the supported source->target type pairs. Then implement @@ -1017,8 +1012,7 @@ public interface GenericConverter { ConditionalGenericConverter is an subinterface of GenericConverter that allows you to define such custom matching criteria: - - To register a default ConversionService with Spring, add the following bean definition with id conversionService: - ]]> + ]]> A default ConversionService can convert between strings, numbers, @@ -1098,11 +1091,11 @@ public interface ConversionService { either of the Converter, ConverterFactory, or GenericConverter interfaces. - + - + ]]> @@ -1124,8 +1117,7 @@ public interface ConversionService { To work with a ConversionService instance programatically, simply inject a reference to it like you would for any other bean: - The Formatter SPI to implement field formatting logic is simple and strongly typed: - extends Printer, Parser { -}]]> - +}]]> Where Formatter extends from the Printer and Parser building-block interfaces: - { + + { String print(T fieldValue, Locale locale); -}]]> - - + + { T parse(String clientValue, Locale locale) throws ParseException; -}]]> - +}]]> To create your own Formatter, simply implement the Formatter interface above. Parameterize T to be the type of object you wish to @@ -1225,8 +1214,7 @@ public interface Parser { Consider DateFormatter as an example Formatter implementation: - { @@ -1270,8 +1258,7 @@ public final class DateFormatter implements Formatter { or annotation. To bind an Annotation to a formatter, implement AnnotationFormatterFactory: - { @@ -1296,12 +1283,13 @@ public interface AnnotationFormatterFactory { the @NumberFormat Annotation to a formatter. This annotation allows either a number style or pattern to be specified: - { + { public Set> getFieldTypes() { return new HashSet>(asList(new Class[] { - Short.class, Integer.class, Long.class, Float.class, Double.class, BigDecimal.class, BigInteger.class })); + Short.class, Integer.class, Long.class, Float.class, + Double.class, BigDecimal.class, BigInteger.class })); } public Printer getPrinter(NumberFormat annotation, Class fieldType) { @@ -1312,7 +1300,8 @@ public final class NumberFormatAnnotationFormatterFactory implements AnnotationF return configureFormatterFrom(annotation, fieldType); } - private Formatter configureFormatterFrom(NumberFormat annotation, Class fieldType) { + private Formatter configureFormatterFrom(NumberFormat annotation, + Class fieldType) { if (!annotation.pattern().isEmpty()) { return new NumberFormatter(annotation.pattern()); } else { @@ -1330,8 +1319,7 @@ public final class NumberFormatAnnotationFormatterFactory implements AnnotationF To trigger formatting, simply annotate fields with @NumberFormat: - The example below uses @DateTimeFormat to format a java.util.Date as a ISO Date (yyyy-MM-dd): - Review the FormatterRegistry SPI below: - To rely on default formatting rules, no custom configuration is required in your Spring MVC config XML: - + - -]]> +]]> With this one-line of configuation, default formatters for Numbers and Date types will be installed, including support for the @@ -1436,8 +1420,7 @@ public interface FormatterRegistry { To inject a ConversionService instance with custom formatters and converters registered, set the conversion-service attribute: - + - + - + ]]> @@ -1482,8 +1466,7 @@ public interface FormatterRegistry { To illustrate, consider a simple PersonForm model with two properties: - @@ -1491,8 +1474,7 @@ public class PersonForm { JSR-303 allows you to define declarative validation constraints against such properties: - Use the LocalValidatorFactoryBean to configure a default JSR-303 Validator as a Spring bean: - ]]> - + ]]> The basic configuration above will trigger JSR-303 to initialize using its default bootstrap mechanism. A JSR-303 provider, such as @@ -1550,22 +1531,19 @@ public class PersonForm { Inject a reference to javax.validation.Validator if you prefer to work with the JSR-303 API directly: - -import javax.validation.Validator; + + private Validator validator;]]> Inject a reference to org.springframework.validation.Validator if your bean requires the Spring Validation API: - ConstraintValidator implementation that uses Spring for dependency injection: - - // get BindingResult that includes any validation errors -BindingResult results = binder.getBindingResult(); - +BindingResult results = binder.getBindingResult();
@@ -1697,8 +1672,7 @@ public class MyController { callback. This allows you to configure a Validator instance per @Controller class: - - + The Spring MVC configuration required to enable JSR-303 support is shown below: - +