Allow binding=false on @ModelAttribute
Issue: SPR-13402
This commit is contained in:
@@ -1704,6 +1704,31 @@ With a `BindingResult` you can check if errors were found in which case it's com
|
||||
render the same form where the errors can be shown with the help of Spring's `<errors>`
|
||||
form tag.
|
||||
|
||||
Note that in some cases it may be useful to gain access to an attribute in the
|
||||
model without data binding. For such cases you may inject the `Model` into the
|
||||
controller or alternatively use the `binding` flag on the annotation:
|
||||
|
||||
[source,java,indent=0]
|
||||
[subs="verbatim,quotes"]
|
||||
----
|
||||
@ModelAttribute
|
||||
public AccountForm setUpForm() {
|
||||
return new AccountForm();
|
||||
}
|
||||
|
||||
@ModelAttribute
|
||||
public Account findAccount(@PathVariable String accountId) {
|
||||
return accountRepository.findOne(accountId);
|
||||
}
|
||||
|
||||
@RequestMapping(path="update", method=POST)
|
||||
public String update(@Valid AccountUpdateForm form, BindingResult result,
|
||||
**@ModelAttribute(binding=false)** Account account) {
|
||||
|
||||
// ...
|
||||
}
|
||||
----
|
||||
|
||||
In addition to data binding you can also invoke validation using your own custom
|
||||
validator passing the same `BindingResult` that was used to record data binding errors.
|
||||
That allows for data binding and validation errors to be accumulated in one place and
|
||||
@@ -1747,6 +1772,7 @@ See <<validation-beanvalidation>> and <<validation>> for details on how to confi
|
||||
use validation.
|
||||
|
||||
|
||||
|
||||
[[mvc-ann-sessionattrib]]
|
||||
==== Using @SessionAttributes to store model attributes in the HTTP session between requests
|
||||
|
||||
|
||||
@@ -666,6 +666,7 @@ Spring 4.3 also improves the caching abstraction as follows:
|
||||
* `@ResponseStatus` supported on the class level and inherited on all methods.
|
||||
* New `@SessionAttribute` annotation for access to session attributes (see <<mvc-ann-sessionattrib-global, example>>).
|
||||
* New `@RequestAttribute` annotation for access to session attributes (see <<mvc-ann-requestattrib, example>>).
|
||||
* `@ModelAttribute` allows preventing data binding via `binding=false` attribute (see <<mvc-ann-modelattrib-method-args, reference>>).
|
||||
* `AsyncRestTemplate` supports request interception.
|
||||
|
||||
=== WebSocket Messaging Improvements
|
||||
|
||||
Reference in New Issue
Block a user