Spring MVC : Common param in all requests
http-request-parameters, spring, spring-mvc
Solution
@ControllerAdvice("net.myproject.mypackage")
public class MyControllerAdvice {
@ModelAttribute
public void myMethod(@RequestParam String mandatoryParam) {
// Use your mandatoryParam
}
}
`myMethod()` will be called for every request to any controller in the `net.myproject.mypackage` package. (Before Spring 4.0, you could not define a package. `@ControllerAdvice` applied to all controllers).
See the Spring Reference for more details on `@ModelAttribute` methods.
Problem
I have many controllers in my Spring MVC web application and there is a param `mandatoryParam` let's say which has to be present in all the requests to the web application. Now I want to make that param-value available to all the methods in my web-layer and service-layer. How can I handle this scenario effectively? Currently I am handling it in this way: - ... controllerMethod(@RequestParam String mandatoryParam, ...) - and, then passing this param to service layer by calling it's method