通过RequestContextHolder获取HttpServletRequest

xiaoxiao2021-02-28  67

springMVC中,为了方便随时获取当前的request对象,可以通过RequestContextHolder的静态方法getRequestAttributes()获取Request相关的变量,如request, response等。

RequestAttributes ra = RequestContextHolder.getRequestAttributes(); HttpServletRequest request = ((ServletRequestAttributes)ra).getRequest();

通过这种方式,就不需要在接收或传递参数的时候通过定义HttpRequest或HttpResponse来获取参数了。

转载请注明原文地址: https://www.6miu.com/read-57184.html

最新回复(0)