Spring Controllers:我可以在调用每个@RequestMapping 方法之前调用一个方法吗? [英] Spring Controllers: Can I call a method before each @RequestMapping method is called?

查看:51
本文介绍了Spring Controllers:我可以在调用每个@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 Controllers:我可以在调用每个@RequestMapping 方法之前调用一个方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆