Skip to content

Commit dc39e35

Browse files
committed
Doc example for custom Formatter as alternative to PropertyEditor in @InitBinder method
Issue: SPR-7773
1 parent 9799df3 commit dc39e35

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/asciidoc/web-mvc.adoc

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,8 +1831,8 @@ arguments of annotated handler methods.
18311831

18321832
Such init-binder methods support all arguments that `@RequestMapping` supports, except
18331833
for command/form objects and corresponding validation result objects. Init-binder
1834-
methods must not have a return value. Thus, they are usually declared as `void`. Typical
1835-
arguments include `WebDataBinder` in combination with `WebRequest` or
1834+
methods must not have a return value. Thus, they are usually declared as `void`.
1835+
Typical arguments include `WebDataBinder` in combination with `WebRequest` or
18361836
`java.util.Locale`, allowing code to register context-specific editors.
18371837

18381838
The following example demonstrates the use of `@InitBinder` to configure a
@@ -1852,7 +1852,27 @@ The following example demonstrates the use of `@InitBinder` to configure a
18521852
}
18531853
18541854
// ...
1855+
}
1856+
----
1857+
1858+
Alternatively, as of Spring 4.2, consider using `addCustomFormatter` to specify
1859+
`Formatter` implementations instead of `PropertyEditor` instances. This is
1860+
particularly useful if you happen to have a `Formatter`-based setup in a shared
1861+
`FormattingConversionService` as well, with the same approach to be reused for
1862+
controller-specific tweaking of the binding rules.
1863+
1864+
[source,java,indent=0]
1865+
[subs="verbatim,quotes"]
1866+
----
1867+
@Controller
1868+
public class MyFormController {
18551869
1870+
**@InitBinder**
1871+
public void initBinder(WebDataBinder binder) {
1872+
binder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
1873+
}
1874+
1875+
// ...
18561876
}
18571877
----
18581878

0 commit comments

Comments
 (0)