Skip to content

Commit 29790d5

Browse files
committed
Update docs on how a @ModelAttribute is sourced
Closes gh-26873
1 parent 355d394 commit 29790d5

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

src/docs/asciidoc/web/webmvc.adoc

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,33 +2622,41 @@ query parameters and form fields. The following example shows how to do so:
26222622
.Java
26232623
----
26242624
@PostMapping("/owners/{ownerId}/pets/{petId}/edit")
2625-
public String processSubmit(@ModelAttribute Pet pet) { } <1>
2625+
public String processSubmit(@ModelAttribute Pet pet) {
2626+
// method logic...
2627+
}
26262628
----
2627-
<1> Bind an instance of `Pet`.
26282629

26292630
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
26302631
.Kotlin
26312632
----
26322633
@PostMapping("/owners/{ownerId}/pets/{petId}/edit")
2633-
fun processSubmit(@ModelAttribute pet: Pet): String { } // <1>
2634+
fun processSubmit(@ModelAttribute pet: Pet): String {
2635+
// method logic...
2636+
}
26342637
----
2635-
<1> Bind an instance of `Pet`.
2636-
2637-
The `Pet` instance above is resolved as follows:
2638-
2639-
* From the model if already added by using <<mvc-ann-modelattrib-methods>>.
2640-
* From the HTTP session by using <<mvc-ann-sessionattributes>>.
2641-
* From a URI path variable passed through a `Converter` (see the next example).
2642-
* From the invocation of a default constructor.
2643-
* From the invocation of a "`primary constructor`" with arguments that match to Servlet
2644-
request parameters. Argument names are determined through JavaBeans
2645-
`@ConstructorProperties` or through runtime-retained parameter names in the bytecode.
26462638

2647-
While it is common to use a <<mvc-ann-modelattrib-methods>> to populate the model with
2648-
attributes, one other alternative is to rely on a `Converter<String, T>` in combination
2649-
with a URI path variable convention. In the following example, the model attribute name,
2650-
`account`, matches the URI path variable, `account`, and the `Account` is loaded by passing
2651-
the `String` account number through a registered `Converter<String, Account>`:
2639+
The `Pet` instance above is sourced in one of the following ways:
2640+
2641+
* Retrieved from the model where it may have been added by a
2642+
<<mvc-ann-modelattrib-methods,@ModelAttribute method>>.
2643+
* Retrieved from the HTTP session if the model attribute was listed in
2644+
the class-level <<mvc-ann-sessionattributes>> annotation.
2645+
* Obtained through a `Converter` where the model attribute name matches the name of a
2646+
request value such as a path variable or a request parameter (see next example).
2647+
* Instantiated using its default constructor.
2648+
* Instantiated through a "`primary constructor`" with arguments that match to Servlet
2649+
request parameters. Argument names are determined through JavaBeans
2650+
`@ConstructorProperties` or through runtime-retained parameter names in the bytecode.
2651+
2652+
One alternative to using a <<mvc-ann-modelattrib-methods,@ModelAttribute method>> to
2653+
supply it or relying on the framework to create the model attribute, is to have a
2654+
`Converter<String, T>` to provide the instance. This is applied when the model attribute
2655+
name matches to the name of a request value such as a path variable or a request
2656+
parameter, and there is a `Converter` from `String` to the model attribute type.
2657+
In the following example, the model attribute name is `account` which matches the URI
2658+
path variable `account`, and there is a registered `Converter<String, Account>` which
2659+
could load the `Account` from a data store:
26522660

26532661
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
26542662
.Java

0 commit comments

Comments
 (0)