一般情況下,我們要想獲得當前請求的HttpServletRequest
對象,都是在Controller
中獲得或者從該層傳遞到下層,那麼如何在Controller
沒有傳遞該對象時獲取到呢,在spring中,就要利用RequestContextHolder
這個對象,代碼如下:
HttpServletRequest httpServletRequest =
((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();