@@ -792,8 +792,8 @@ See also the `org.springframework.beans.support.ResourceEditorRegistrar` for an
792
792
`PropertyEditorRegistrar` implementation. Notice how in its implementation of the
793
793
`registerCustomEditors(..)` method, it creates new instances of each property editor.
794
794
795
- The next example shows how to configure a `CustomEditorConfigurer` and inject an instance of our
796
- `CustomPropertyEditorRegistrar` into it:
795
+ The next example shows how to configure a `CustomEditorConfigurer` and inject an instance
796
+ of our `CustomPropertyEditorRegistrar` into it:
797
797
798
798
[source,xml,indent=0,subs="verbatim,quotes"]
799
799
----
@@ -809,50 +809,51 @@ The next example shows how to configure a `CustomEditorConfigurer` and inject an
809
809
class="com.foo.editors.spring.CustomPropertyEditorRegistrar"/>
810
810
----
811
811
812
- Finally (and in a bit of a departure from the focus of this chapter for those of you
813
- using <<web.adoc#mvc, Spring's MVC web framework>>), using `PropertyEditorRegistrars` in
814
- conjunction with data-binding `Controllers` (such as `SimpleFormController`) can be very
815
- convenient. The following example uses a `PropertyEditorRegistrar` in the
816
- implementation of an `initBinder(..)` method:
812
+ Finally (and in a bit of a departure from the focus of this chapter) for those of you
813
+ using <<web.adoc#mvc, Spring's MVC web framework>>, using a `PropertyEditorRegistrar` in
814
+ conjunction with data-binding web controllers can be very convenient. The following
815
+ example uses a `PropertyEditorRegistrar` in the implementation of an `@InitBinder` method:
817
816
818
817
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
819
818
.Java
820
819
----
821
- public final class RegisterUserController extends SimpleFormController {
820
+ @Controller
821
+ public class RegisterUserController {
822
822
823
823
private final PropertyEditorRegistrar customPropertyEditorRegistrar;
824
824
825
- public RegisterUserController(PropertyEditorRegistrar propertyEditorRegistrar) {
825
+ RegisterUserController(PropertyEditorRegistrar propertyEditorRegistrar) {
826
826
this.customPropertyEditorRegistrar = propertyEditorRegistrar;
827
827
}
828
828
829
- protected void initBinder(HttpServletRequest request,
830
- ServletRequestDataBinder binder) throws Exception {
829
+ @InitBinder
830
+ void initBinder(WebDataBinder binder) {
831
831
this.customPropertyEditorRegistrar.registerCustomEditors(binder);
832
832
}
833
833
834
- // other methods to do with registering a User
834
+ // other methods related to registering a User
835
835
}
836
836
----
837
837
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
838
838
.Kotlin
839
839
----
840
+ @Controller
840
841
class RegisterUserController(
841
- private val customPropertyEditorRegistrar: PropertyEditorRegistrar) : SimpleFormController() {
842
+ private val customPropertyEditorRegistrar: PropertyEditorRegistrar) {
842
843
843
- protected fun initBinder(request: HttpServletRequest,
844
- binder: ServletRequestDataBinder ) {
844
+ @InitBinder
845
+ fun initBinder( binder: WebDataBinder ) {
845
846
this.customPropertyEditorRegistrar.registerCustomEditors(binder)
846
847
}
847
848
848
- // other methods to do with registering a User
849
+ // other methods related to registering a User
849
850
}
850
851
----
851
852
852
853
This style of `PropertyEditor` registration can lead to concise code (the implementation
853
- of `initBinder(..)` is only one line long) and lets common `PropertyEditor`
854
- registration code be encapsulated in a class and then shared amongst as many
855
- `Controllers` as needed.
854
+ of the `@InitBinder` method is only one line long) and lets common `PropertyEditor`
855
+ registration code be encapsulated in a class and then shared amongst as many controllers
856
+ as needed.
856
857
857
858
858
859
0 commit comments