Spring控制器:我可以在每个@RequestMapping方法被调用之前调用一个方法吗? [英] Spring Controllers: Can I call a method before each @RequestMapping method is called?
本文介绍了Spring控制器:我可以在每个@RequestMapping方法被调用之前调用一个方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些通用组件,这些组件始终存在于给定Controller类所服务的每个页面中.
I have some common components that are always present in every page served by a given Controller class.
在每个@RequestMapping方法的开头,我用这些通用组件填充模型.
At the beginning of each @RequestMapping method I populate the model with these common components.
有没有一种方法可以在每个控制器方法之前定义一个方法,以便我可以将所有复制/粘贴到一个地方?
Is there a way to define a method be called prior to each of the controller methods so that I can get all of this copy/paste into one place?
推荐答案
只需使用 @ModelAttribute
下面将在名称"foo"下向模型添加Foo实例
The below would add a Foo instance to the model under the name "foo"
@ModelAttribute("foo")
public Foo foo() {
return new Foo();
}
请参见 @ModelAttribute
文档
这篇关于Spring控制器:我可以在每个@RequestMapping方法被调用之前调用一个方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文