|
|
|
@ -1,5 +1,50 @@ |
|
|
|
|
|
|
|
[[beans-binding]] |
|
|
|
|
|
|
|
= Data Binding |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Data binding is useful for binding user input to a target object where user input is a map |
|
|
|
|
|
|
|
with property paths as keys, following xref:beans-beans-conventions[JavaBeans conventions]. |
|
|
|
|
|
|
|
`DataBinder` is the main class that supports this, and it provides two ways to bind user |
|
|
|
|
|
|
|
input: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- xref:beans-constructor-binding[Constructor binding] - bind user input to a public data |
|
|
|
|
|
|
|
constructor, looking up constructor argument values in the user input. |
|
|
|
|
|
|
|
- xref:beans-beans[Property binding] - bind user input to setters, matching keys from the |
|
|
|
|
|
|
|
the user input to properties of the target object structure. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
You can apply both constructor and property binding or only one. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[[beans-constructor-binding]] |
|
|
|
|
|
|
|
== Constructor Binding |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
To use constructor binding: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1. Create a `DataBinder` with `null` as the target object. |
|
|
|
|
|
|
|
2. Set `targetType` to the target class. |
|
|
|
|
|
|
|
3. Call `construct`. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The target class should have a single public constructor or a single non-public constructor |
|
|
|
|
|
|
|
with arguments. If there are multiple constructors, then a default constructor if present |
|
|
|
|
|
|
|
is used. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
By default, constructor parameter names are used to look up argument values, but you can |
|
|
|
|
|
|
|
configure a `NameResolver`. Spring MVC and WebFlux both rely to allow customizing the name |
|
|
|
|
|
|
|
of the value to bind through an `@BindParam` annotation on constructor parameters. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
xref:beans-beans-conventions[Type conversion] is applied as needed to convert user input. |
|
|
|
|
|
|
|
If the constructor parameter is an object, it is constructed recursively in the same |
|
|
|
|
|
|
|
manner, but through a nested property path. That means constructor binding creates both |
|
|
|
|
|
|
|
the target object and any objects it contains. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Binding and conversion errors are reflected in the `BindingResult` of the `DataBinder`. |
|
|
|
|
|
|
|
If the target is created successfully, then `target` is set to the created instance |
|
|
|
|
|
|
|
after the call to `construct`. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[[beans-beans]] |
|
|
|
[[beans-beans]] |
|
|
|
= Bean Manipulation and the `BeanWrapper` |
|
|
|
== Property Binding with `BeanWrapper` |
|
|
|
|
|
|
|
|
|
|
|
The `org.springframework.beans` package adheres to the JavaBeans standard. |
|
|
|
The `org.springframework.beans` package adheres to the JavaBeans standard. |
|
|
|
A JavaBean is a class with a default no-argument constructor and that follows |
|
|
|
A JavaBean is a class with a default no-argument constructor and that follows |
|
|
|
@ -26,7 +71,7 @@ perform actions on that bean, such as setting and retrieving properties. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[[beans-beans-conventions]] |
|
|
|
[[beans-beans-conventions]] |
|
|
|
== Setting and Getting Basic and Nested Properties |
|
|
|
=== Setting and Getting Basic and Nested Properties |
|
|
|
|
|
|
|
|
|
|
|
Setting and getting properties is done through the `setPropertyValue` and |
|
|
|
Setting and getting properties is done through the `setPropertyValue` and |
|
|
|
`getPropertyValue` overloaded method variants of `BeanWrapper`. See their Javadoc for |
|
|
|
`getPropertyValue` overloaded method variants of `BeanWrapper`. See their Javadoc for |
|
|
|
@ -192,7 +237,7 @@ Kotlin:: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[[beans-beans-conversion]] |
|
|
|
[[beans-beans-conversion]] |
|
|
|
== Built-in `PropertyEditor` Implementations |
|
|
|
== ``PropertyEditor``'s |
|
|
|
|
|
|
|
|
|
|
|
Spring uses the concept of a `PropertyEditor` to effect the conversion between an |
|
|
|
Spring uses the concept of a `PropertyEditor` to effect the conversion between an |
|
|
|
`Object` and a `String`. It can be handy |
|
|
|
`Object` and a `String`. It can be handy |
|
|
|
@ -378,7 +423,7 @@ Kotlin:: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[[beans-beans-conversion-customeditor-registration]] |
|
|
|
[[beans-beans-conversion-customeditor-registration]] |
|
|
|
=== Registering Additional Custom `PropertyEditor` Implementations |
|
|
|
=== Custom ``PropertyEditor``'s |
|
|
|
|
|
|
|
|
|
|
|
When setting bean properties as string values, a Spring IoC container ultimately uses |
|
|
|
When setting bean properties as string values, a Spring IoC container ultimately uses |
|
|
|
standard JavaBeans `PropertyEditor` implementations to convert these strings to the complex type of the |
|
|
|
standard JavaBeans `PropertyEditor` implementations to convert these strings to the complex type of the |
|
|
|
@ -521,7 +566,7 @@ Finally, the following example shows how to use `CustomEditorConfigurer` to regi |
|
|
|
---- |
|
|
|
---- |
|
|
|
|
|
|
|
|
|
|
|
[[beans-beans-conversion-customeditor-registration-per]] |
|
|
|
[[beans-beans-conversion-customeditor-registration-per]] |
|
|
|
==== Using `PropertyEditorRegistrar` |
|
|
|
=== `PropertyEditorRegistrar` |
|
|
|
|
|
|
|
|
|
|
|
Another mechanism for registering property editors with the Spring container is to |
|
|
|
Another mechanism for registering property editors with the Spring container is to |
|
|
|
create and use a `PropertyEditorRegistrar`. This interface is particularly useful when |
|
|
|
create and use a `PropertyEditorRegistrar`. This interface is particularly useful when |
|
|
|
|