Skip to content

Commit 6e82bf0

Browse files
committed
Document list/map/array constructor data binding
Closes gh-32426
1 parent e48cbc5 commit 6e82bf0

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

framework-docs/modules/ROOT/pages/core/validation/beans-beans.adoc

+11-7
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,20 @@ The target class should have a single public constructor or a single non-public
2727
with arguments. If there are multiple constructors, then a default constructor if present
2828
is used.
2929

30-
By default, constructor parameter names are used to look up argument values, but you can
31-
configure a `NameResolver`. Spring MVC and WebFlux both rely to allow customizing the name
32-
of the value to bind through an `@BindParam` annotation on constructor parameters.
30+
By default, argument values are looked up via constructor parameter names. Spring MVC and
31+
WebFlux support a custom name mapping through the `@BindParam` annotation on constructor
32+
parameters or fields if present. If necessary, you can also configure a `NameResolver` on
33+
`DataBinder` to customize the argument name to use.
3334

3435
xref:beans-beans-conventions[Type conversion] is applied as needed to convert user input.
3536
If the constructor parameter is an object, it is constructed recursively in the same
3637
manner, but through a nested property path. That means constructor binding creates both
3738
the target object and any objects it contains.
3839

40+
Constructor binding supports `List`, `Map`, and array arguments either converted from
41+
a single string, e.g. comma-separated list, or based on indexed keys such as
42+
`accounts[2].name` or `account[KEY].name`.
43+
3944
Binding and conversion errors are reflected in the `BindingResult` of the `DataBinder`.
4045
If the target is created successfully, then `target` is set to the created instance
4146
after the call to `construct`.
@@ -90,13 +95,12 @@ details. The below table shows some examples of these conventions:
9095
| Indicates the nested property `name` of the property `account` that corresponds to
9196
(for example) the `getAccount().setName()` or `getAccount().getName()` methods.
9297

93-
| `account[2]`
98+
| `accounts[2]`
9499
| Indicates the _third_ element of the indexed property `account`. Indexed properties
95100
can be of type `array`, `list`, or other naturally ordered collection.
96101

97-
| `account[COMPANYNAME]`
98-
| Indicates the value of the map entry indexed by the `COMPANYNAME` key of the `account` `Map`
99-
property.
102+
| `accounts[KEY]`
103+
| Indicates the value of the map entry indexed by the `KEY` value.
100104
|===
101105

102106
(This next section is not vitally important to you if you do not plan to work with

framework-docs/modules/ROOT/pages/web/webflux/controller/ann-methods/modelattrib-method-args.adoc

+4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ NOTE: The `@BindParam` may also be placed on the fields that correspond to const
8181
parameters. While `@BindParam` is supported out of the box, you can also use a
8282
different annotation by setting a `DataBinder.NameResolver` on `DataBinder`
8383

84+
Constructor binding supports `List`, `Map`, and array arguments either converted from
85+
a single string, e.g. comma-separated list, or based on indexed keys such as
86+
`accounts[2].name` or `account[KEY].name`.
87+
8488
WebFlux, unlike Spring MVC, supports reactive types in the model, e.g. `Mono<Account>`.
8589
You can declare a `@ModelAttribute` argument with or without a reactive type wrapper, and
8690
it will be resolved accordingly to the actual value.

framework-docs/modules/ROOT/pages/web/webmvc/mvc-controller/ann-methods/modelattrib-method-args.adoc

+4
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ NOTE: The `@BindParam` may also be placed on the fields that correspond to const
116116
parameters. While `@BindParam` is supported out of the box, you can also use a
117117
different annotation by setting a `DataBinder.NameResolver` on `DataBinder`
118118

119+
Constructor binding supports `List`, `Map`, and array arguments either converted from
120+
a single string, e.g. comma-separated list, or based on indexed keys such as
121+
`accounts[2].name` or `account[KEY].name`.
122+
119123
In some cases, you may want access to a model attribute without data binding. For such
120124
cases, you can inject the `Model` into the controller and access it directly or,
121125
alternatively, set `@ModelAttribute(binding=false)`, as the following example shows:

0 commit comments

Comments
 (0)