Model

可以使用 @ModelAttribute 注释:

You can use the @ModelAttribute annotation:

  • On a method argument in @RequestMapping methods to create or access an Object from the model and to bind it to the request through a WebDataBinder.

  • As a method-level annotation in @Controller or @ControllerAdvice classes that help to initialize the model prior to any @RequestMapping method invocation.

  • On a @RequestMapping method to mark its return value is a model attribute.

在本节中,讨论`@ModelAttribute`方式——前一列表中的第二个项目。一个控制器可以具有任意数量的`@ModelAttribute`方法。在同一控制器中的`@RequestMapping`方法之前将调用所有此类方法。一个`@ModelAttribute`方法也可以通过`@ControllerAdvice`共享于控制器之间。有关更多详细信息,请参阅有关Controller Advice的部分。

This section discusses @ModelAttribute methods — the second item in the preceding list. A controller can have any number of @ModelAttribute methods. All such methods are invoked before @RequestMapping methods in the same controller. A @ModelAttribute method can also be shared across controllers through @ControllerAdvice. See the section on Controller Advice for more details.

@ModelAttribute 方法具有灵活的方法签名。它们支持与 @RequestMapping 方法相同的大量参数,但自身除外,并且与请求正文无关。

@ModelAttribute methods have flexible method signatures. They support many of the same arguments as @RequestMapping methods, except for @ModelAttribute itself or anything related to the request body.

以下示例显示了一个 @ModelAttribute 方法:

The following example shows a @ModelAttribute method:

  • Java

  • Kotlin

@ModelAttribute
public void populateModel(@RequestParam String number, Model model) {
	model.addAttribute(accountRepository.findAccount(number));
	// add more ...
}
@ModelAttribute
fun populateModel(@RequestParam number: String, model: Model) {
	model.addAttribute(accountRepository.findAccount(number))
	// add more ...
}

以下示例仅添加一个属性:

The following example adds only one attribute:

  • Java

  • Kotlin

@ModelAttribute
public Account addAccount(@RequestParam String number) {
	return accountRepository.findAccount(number);
}
@ModelAttribute
fun addAccount(@RequestParam number: String): Account {
	return accountRepository.findAccount(number)
}

如果未明确指定名称,则根据 Object 类型选择默认名称(如 https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/Conventions.html[Conventions 的 javadoc 中所述)。可以使用重载的 addAttribute 方法或 @ModelAttribute(对于返回值)上的 name 属性,随时指定明确的名称。

When a name is not explicitly specified, a default name is chosen based on the Object type, as explained in the javadoc for Conventions. You can always assign an explicit name by using the overloaded addAttribute method or through the name attribute on @ModelAttribute (for a return value).

你还可以将 @ModelAttribute 用作 @RequestMapping 方法的方法级注释,在这种情况下,@RequestMapping 方法的返回值将解释为模型属性。通常不需要这样做,因为这是 HTML 控制器中的默认行为,除非返回值是 String,否则将被解释为视图名称。@ModelAttribute 也可以自定义模型属性名称,如下例所示:

You can also use @ModelAttribute as a method-level annotation on @RequestMapping methods, in which case the return value of the @RequestMapping method is interpreted as a model attribute. This is typically not required, as it is the default behavior in HTML controllers, unless the return value is a String that would otherwise be interpreted as a view name. @ModelAttribute can also customize the model attribute name, as the following example shows:

  • Java

  • Kotlin

@GetMapping("/accounts/{id}")
@ModelAttribute("myAccount")
public Account handle() {
	// ...
	return account;
}
@GetMapping("/accounts/{id}")
@ModelAttribute("myAccount")
fun handle(): Account {
	// ...
	return account
}