Issue
I have some common components that are always present in every page served by a given Controller class.
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?
Solution
Just annotate a method with @ModelAttribute
The below would add a Foo instance to the model under the name "foo"
@ModelAttribute("foo")
public Foo foo() {
return new Foo();
}
See the @ModelAttribute
documentation
Answered By - ptomli
Answer Checked By - David Marino (JavaFixing Volunteer)