此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.2.0! |
@ModelAttribute
这@ModelAttribute
method parameter annotation 将请求参数绑定到模型上
对象。例如:
-
Java
-
Kotlin
@PostMapping("/owners/{ownerId}/pets/{petId}/edit")
public String processSubmit(@ModelAttribute Pet pet) { (1)
// method logic...
}
1 | 绑定到Pet . |
@PostMapping("/owners/{ownerId}/pets/{petId}/edit")
fun processSubmit(@ModelAttribute pet: Pet): String { (1)
// method logic...
}
1 | 绑定到Pet . |
这Pet
instance 可以是:
-
从模型访问,其中它可以通过 @ModelAttribute 方法添加。
-
如果 model 属性列在 类级别
@SessionAttributes
注解。 -
通过
Converter
如果 Model 属性名称与 请求值,例如 path 变量或 request 参数(示例如下)。 -
通过默认构造函数实例化。
-
通过具有与 Servlet 匹配的参数的 “primary constructor” 实例化 request 参数。参数名称是通过运行时保留的参数确定的 名称。
如上所述,一个Converter<String, T>
可用于获取 Model 对象,如果
模型属性名称与请求值的名称匹配,例如路径变量或
request 参数,并且有一个兼容的Converter<String, T>
.在下面的示例中,
模型属性名称account
matches URI 路径变量account
,并且有一个
注册Converter<String, Account>
这可能会从 Persistence Store 中检索它:
-
Java
-
Kotlin
@PutMapping("/accounts/{account}")
public String save(@ModelAttribute("account") Account account) { (1)
// ...
}
@PutMapping("/accounts/{account}")
fun save(@ModelAttribute("account") account: Account): String { (1)
// ...
}
默认情况下,构造函数和属性数据绑定都适用。然而 模型对象设计需要仔细考虑,出于安全原因,它是 建议使用专为 Web 绑定定制的对象,或应用 构造函数绑定。如果仍必须使用属性绑定,则应设置 allowedFields 模式以限制可以设置的属性。有关此内容的更多详细信息 和示例配置,请参阅模型设计。
使用构造函数绑定时,您可以通过@BindParam
注解。例如:
-
Java
-
Kotlin
class Account {
private final String firstName;
public Account(@BindParam("first-name") String firstName) {
this.firstName = firstName;
}
}
class Account(@BindParam("first-name") val firstName: String)
这@BindParam 也可以放置在与 constructor 对应的字段上
参数。而@BindParam 支持开箱即用,您还可以使用
different 注解,方法是将DataBinder.NameResolver 上DataBinder
|
在某些情况下,您可能希望访问没有数据绑定的 model 属性。对于这样的
cases,您可以注入Model
放入控制器并直接访问它,或者,
或者,将@ModelAttribute(binding=false)
,如下例所示:
-
Java
-
Kotlin
@ModelAttribute
public AccountForm setUpForm() {
return new AccountForm();
}
@ModelAttribute
public Account findAccount(@PathVariable String accountId) {
return accountRepository.findOne(accountId);
}
@PostMapping("update")
public String update(AccountForm form, BindingResult result,
@ModelAttribute(binding=false) Account account) { (1)
// ...
}
1 | 设置@ModelAttribute(binding=false) . |
@ModelAttribute
fun setUpForm(): AccountForm {
return AccountForm()
}
@ModelAttribute
fun findAccount(@PathVariable accountId: String): Account {
return accountRepository.findOne(accountId)
}
@PostMapping("update")
fun update(form: AccountForm, result: BindingResult,
@ModelAttribute(binding = false) account: Account): String { (1)
// ...
}
1 | 设置@ModelAt\tribute(binding=false) . |
如果数据绑定导致错误,则默认情况下MethodArgumentNotValidException
被提升,
但您也可以添加BindingResult
参数紧邻@ModelAttribute
为了在 controller 方法中处理此类错误。例如:
-
Java
-
Kotlin
@PostMapping("/owners/{ownerId}/pets/{petId}/edit")
public String processSubmit(@ModelAttribute("pet") Pet pet, BindingResult result) { (1)
if (result.hasErrors()) {
return "petForm";
}
// ...
}
1 | 添加BindingResult 在@ModelAttribute . |
@PostMapping("/owners/{ownerId}/pets/{petId}/edit")
fun processSubmit(@ModelAttribute("pet") pet: Pet, result: BindingResult): String { (1)
if (result.hasErrors()) {
return "petForm"
}
// ...
}
1 | 添加BindingResult 在@ModelAttribute . |
-
Java
-
Kotlin
@PostMapping("/owners/{ownerId}/pets/{petId}/edit")
public String processSubmit(@Valid @ModelAttribute("pet") Pet pet, BindingResult result) { (1)
if (result.hasErrors()) {
return "petForm";
}
// ...
}
1 | 验证Pet 实例。 |
@PostMapping("/owners/{ownerId}/pets/{petId}/edit")
fun processSubmit(@Valid @ModelAttribute("pet") pet: Pet, result: BindingResult): String { (1)
if (result.hasErrors()) {
return "petForm"
}
// ...
}
1 | 验证Pet 实例。 |
如果没有BindingResult
参数@ModelAttribute
然后MethodArgumentNotValueException
会引发验证错误。但是,如果方法
验证适用,因为其他参数具有@jakarta.validation.Constraint
附注
然后HandlerMethodValidationException
而是引发。有关更多详细信息,请参阅验证部分。
用@ModelAttribute 是可选的。默认情况下,任何不是简单
由 BeanUtils#isSimpleProperty 确定的值类型,并且未由任何其他参数解析器解析的 value 类型被视为隐式@ModelAttribute . |
使用 GraalVM 编译为原生镜像时,隐式的@ModelAttribute 上述支持不允许对相关数据进行适当的提前推理
绑定反射提示。因此,建议显式注释
method 参数替换为@ModelAttribute 用于 GraalVM 原生映像。 |