@ModelAttribute注解的用法

发布时间 2024-01-01 22:04:33作者: 残城碎梦

在SpringMVC的Controller中使用@ModelAttribute时,其位置包括下面三种:

  • 应用在方法上
  • 应用在方法的参数上
  • 应用在方法上,并且方法也使用了@RequestMapping

应用在方法上

首先说明一下,被@ModelAttribute注解的方法会在Controller每个方法执行之前都执行,因此对于一个Controller中包含多个URL的时候,要谨慎使用。