Convert SimpleFormController example to @Controller in reference manual
This change is necessary since the SimpleFormController class no longer exists.
This commit is contained in:
parent
afa799b4f0
commit
ed06a6de26
|
@ -792,8 +792,8 @@ See also the `org.springframework.beans.support.ResourceEditorRegistrar` for an
|
||||||
`PropertyEditorRegistrar` implementation. Notice how in its implementation of the
|
`PropertyEditorRegistrar` implementation. Notice how in its implementation of the
|
||||||
`registerCustomEditors(..)` method, it creates new instances of each property editor.
|
`registerCustomEditors(..)` method, it creates new instances of each property editor.
|
||||||
|
|
||||||
The next example shows how to configure a `CustomEditorConfigurer` and inject an instance of our
|
The next example shows how to configure a `CustomEditorConfigurer` and inject an instance
|
||||||
`CustomPropertyEditorRegistrar` into it:
|
of our `CustomPropertyEditorRegistrar` into it:
|
||||||
|
|
||||||
[source,xml,indent=0,subs="verbatim,quotes"]
|
[source,xml,indent=0,subs="verbatim,quotes"]
|
||||||
----
|
----
|
||||||
|
@ -809,50 +809,51 @@ The next example shows how to configure a `CustomEditorConfigurer` and inject an
|
||||||
class="com.foo.editors.spring.CustomPropertyEditorRegistrar"/>
|
class="com.foo.editors.spring.CustomPropertyEditorRegistrar"/>
|
||||||
----
|
----
|
||||||
|
|
||||||
Finally (and in a bit of a departure from the focus of this chapter for those of you
|
Finally (and in a bit of a departure from the focus of this chapter) for those of you
|
||||||
using <<web.adoc#mvc, Spring's MVC web framework>>), using `PropertyEditorRegistrars` in
|
using <<web.adoc#mvc, Spring's MVC web framework>>, using a `PropertyEditorRegistrar` in
|
||||||
conjunction with data-binding `Controllers` (such as `SimpleFormController`) can be very
|
conjunction with data-binding web controllers can be very convenient. The following
|
||||||
convenient. The following example uses a `PropertyEditorRegistrar` in the
|
example uses a `PropertyEditorRegistrar` in the implementation of an `@InitBinder` method:
|
||||||
implementation of an `initBinder(..)` method:
|
|
||||||
|
|
||||||
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
||||||
.Java
|
.Java
|
||||||
----
|
----
|
||||||
public final class RegisterUserController extends SimpleFormController {
|
@Controller
|
||||||
|
public class RegisterUserController {
|
||||||
|
|
||||||
private final PropertyEditorRegistrar customPropertyEditorRegistrar;
|
private final PropertyEditorRegistrar customPropertyEditorRegistrar;
|
||||||
|
|
||||||
public RegisterUserController(PropertyEditorRegistrar propertyEditorRegistrar) {
|
RegisterUserController(PropertyEditorRegistrar propertyEditorRegistrar) {
|
||||||
this.customPropertyEditorRegistrar = propertyEditorRegistrar;
|
this.customPropertyEditorRegistrar = propertyEditorRegistrar;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void initBinder(HttpServletRequest request,
|
@InitBinder
|
||||||
ServletRequestDataBinder binder) throws Exception {
|
void initBinder(WebDataBinder binder) {
|
||||||
this.customPropertyEditorRegistrar.registerCustomEditors(binder);
|
this.customPropertyEditorRegistrar.registerCustomEditors(binder);
|
||||||
}
|
}
|
||||||
|
|
||||||
// other methods to do with registering a User
|
// other methods related to registering a User
|
||||||
}
|
}
|
||||||
----
|
----
|
||||||
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
||||||
.Kotlin
|
.Kotlin
|
||||||
----
|
----
|
||||||
|
@Controller
|
||||||
class RegisterUserController(
|
class RegisterUserController(
|
||||||
private val customPropertyEditorRegistrar: PropertyEditorRegistrar) : SimpleFormController() {
|
private val customPropertyEditorRegistrar: PropertyEditorRegistrar) {
|
||||||
|
|
||||||
protected fun initBinder(request: HttpServletRequest,
|
@InitBinder
|
||||||
binder: ServletRequestDataBinder) {
|
fun initBinder(binder: WebDataBinder) {
|
||||||
this.customPropertyEditorRegistrar.registerCustomEditors(binder)
|
this.customPropertyEditorRegistrar.registerCustomEditors(binder)
|
||||||
}
|
}
|
||||||
|
|
||||||
// other methods to do with registering a User
|
// other methods related to registering a User
|
||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
This style of `PropertyEditor` registration can lead to concise code (the implementation
|
This style of `PropertyEditor` registration can lead to concise code (the implementation
|
||||||
of `initBinder(..)` is only one line long) and lets common `PropertyEditor`
|
of the `@InitBinder` method is only one line long) and lets common `PropertyEditor`
|
||||||
registration code be encapsulated in a class and then shared amongst as many
|
registration code be encapsulated in a class and then shared amongst as many controllers
|
||||||
`Controllers` as needed.
|
as needed.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue