Spring MVC:所有请求中的通用参数 [英] Spring MVC : Common param in all requests
问题描述
我的Spring MVC Web应用程序中有很多控制器,并且有一个参数mandatoryParam
可以说,在对Web应用程序的所有请求中都必须包含该参数.
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.
现在,我想使该参数值可用于我的Web层和服务层中的所有方法.如何有效处理这种情况?
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:
推荐答案
@ControllerAdvice("net.myproject.mypackage")
public class MyControllerAdvice {
@ModelAttribute
public void myMethod(@RequestParam String mandatoryParam) {
// Use your mandatoryParam
}
}
对net.myproject.mypackage
程序包中对任何控制器的每个请求,都会调用
myMethod()
. (在Spring 4.0之前,您无法定义程序包.@ControllerAdvice
应用于所有控制器.)
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.
这篇关于Spring MVC:所有请求中的通用参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!