Spring MVC:所有请求中的通用参数 [英] Spring MVC : Common param in all requests

查看:150
本文介绍了Spring MVC:所有请求中的通用参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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?

目前,我以这种方式处理它:

  • ... controllerMethod( @RequestParam字符串requiredParam ,...)
  • ,然后通过调用其方法将此参数传递给服务层

    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

    推荐答案

    @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屋!

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