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?
A @RequestMapping on the class level is not required. Without it, all paths are simply absolute, and not relative.
The @RequestMapping annotation can be applied to class-level and/or method-level in a controller. The class-level annotation maps a specific request path or pattern onto a controller. You can then apply additional method-level annotations to make mappings more specific to handler methods.
In the code snippet above, the method element of the @RequestMapping annotations indicates the HTTP method type of the HTTP request. All the handler methods will handle requests coming to the same URL ( /home), but will depend on the HTTP method being used.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With