在Spring MVC里,@ModelAttribute通常使用在Controller方法的参数注解中,用于解释model entity,但同时,也可以放在方法注解里。
如果把@ModelAttribute放在方法的注解上时,代表的是:该Controller的所有方法在调用前,先执行此@ModelAttribute方法。如下代码片段:
@ModelAttribute public void preRun() { System.out.println("Test Pre-Run"); } @RequestMapping(method=RequestMethod.GET) public String index() { return "login/index"; }在调用所有方法之前,都会先执行preRun()方法。